/
Russid
/
python_advanced
Обзор
Документация
Войти
/
Russid
/
python_advanced
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
module_18_documentation/materials/rest_app_example/clients.py
35 строк
900 B
Alexander Kharlamov
18 модуль
09 дек 2022, 22:03
09 дек 2022, 22:03
b67d87c
Код
Авторство
О чём код?
import json import requests import logging logging.basicConfig(level=logging.DEBUG) class BookClient: URL: str = 'http://0.0.0.0:5000/api/books' TIMEOUT: int = 5 def __init__(self): self.session = requests.Session() def get_all_books(self) -> dict: response = self.session.get(self.URL, timeout=self.TIMEOUT) return response.json() def add_new_book(self, data: dict): response = self.session.post(self.URL, json=data, timeout=self.TIMEOUT) if response.status_code == 201: return response.json() else: raise ValueError('Wrong params. Response message: {}'.format(response.json())) if __name__ == '__main__': client = BookClient() client.session.post( client.URL, data=json.dumps({'title': '123', 'author': 'name'}), headers={'content-type': 'application/json'} )