diff --git a/src/dataclass_rest/__init__.py b/src/dataclass_rest/__init__.py index 6f444a2..064320e 100644 --- a/src/dataclass_rest/__init__.py +++ b/src/dataclass_rest/__init__.py @@ -5,4 +5,4 @@ ] from .http_request import File -from .rest import rest, get, put, post, patch, delete +from .rest import delete, get, patch, post, put, rest diff --git a/src/dataclass_rest/base_client.py b/src/dataclass_rest/base_client.py index 0de8b4f..8272430 100644 --- a/src/dataclass_rest/base_client.py +++ b/src/dataclass_rest/base_client.py @@ -1,7 +1,8 @@ from adaptix import Retort from .client_protocol import ( - ClientProtocol, FactoryProtocol, + ClientProtocol, + FactoryProtocol, ) diff --git a/src/dataclass_rest/boundmethod.py b/src/dataclass_rest/boundmethod.py index f69021a..f291807 100644 --- a/src/dataclass_rest/boundmethod.py +++ b/src/dataclass_rest/boundmethod.py @@ -1,11 +1,11 @@ from abc import ABC, abstractmethod from inspect import getcallargs from logging import getLogger -from typing import Dict, Any, Callable, Optional, NoReturn, Type +from typing import Any, Callable, Dict, NoReturn, Optional, Type -from .client_protocol import ClientProtocol, ClientMethodProtocol +from .client_protocol import ClientMethodProtocol, ClientProtocol from .exceptions import MalformedResponse -from .http_request import HttpRequest, File +from .http_request import File, HttpRequest from .methodspec import MethodSpec logger = getLogger(__name__) diff --git a/src/dataclass_rest/client_protocol.py b/src/dataclass_rest/client_protocol.py index eb8e11f..d3bd359 100644 --- a/src/dataclass_rest/client_protocol.py +++ b/src/dataclass_rest/client_protocol.py @@ -1,5 +1,11 @@ from typing import ( - Protocol, Any, Optional, Callable, Type, runtime_checkable, TypeVar, + Any, + Callable, + Optional, + Protocol, + Type, + TypeVar, + runtime_checkable, ) from .http_request import HttpRequest diff --git a/src/dataclass_rest/http/aiohttp.py b/src/dataclass_rest/http/aiohttp.py index 6d64e38..e8e400f 100644 --- a/src/dataclass_rest/http/aiohttp.py +++ b/src/dataclass_rest/http/aiohttp.py @@ -4,15 +4,22 @@ from aiohttp import FormData from aiohttp.client import ( - ClientResponse, ClientSession, ClientError as AioHttpClientError, + ClientError as AioHttpClientError, +) +from aiohttp.client import ( + ClientResponse, + ClientSession, ) -from ..base_client import BaseClient -from ..boundmethod import AsyncMethod -from ..exceptions import ( - ClientError, ClientLibraryError, ServerError, MalformedResponse, +from dataclass_rest.base_client import BaseClient +from dataclass_rest.boundmethod import AsyncMethod +from dataclass_rest.exceptions import ( + ClientError, + ClientLibraryError, + MalformedResponse, + ServerError, ) -from ..http_request import HttpRequest +from dataclass_rest.http_request import HttpRequest class AiohttpMethod(AsyncMethod): diff --git a/src/dataclass_rest/http/requests.py b/src/dataclass_rest/http/requests.py index 2082d51..6b17484 100644 --- a/src/dataclass_rest/http/requests.py +++ b/src/dataclass_rest/http/requests.py @@ -2,14 +2,17 @@ from json import JSONDecodeError from typing import Any, Optional, Tuple -from requests import Session, Response, RequestException +from requests import RequestException, Response, Session -from ..base_client import BaseClient -from ..boundmethod import SyncMethod -from ..exceptions import ( - ClientLibraryError, ClientError, ServerError, MalformedResponse, +from dataclass_rest.base_client import BaseClient +from dataclass_rest.boundmethod import SyncMethod +from dataclass_rest.exceptions import ( + ClientError, + ClientLibraryError, + MalformedResponse, + ServerError, ) -from ..http_request import HttpRequest, File +from dataclass_rest.http_request import File, HttpRequest class RequestsMethod(SyncMethod): diff --git a/src/dataclass_rest/http_request.py b/src/dataclass_rest/http_request.py index c5bc073..b7ba4e0 100644 --- a/src/dataclass_rest/http_request.py +++ b/src/dataclass_rest/http_request.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Any, Dict, Union, IO, Optional +from typing import IO, Any, Dict, Optional, Union @dataclass diff --git a/src/dataclass_rest/method.py b/src/dataclass_rest/method.py index 66612e7..23c11fa 100644 --- a/src/dataclass_rest/method.py +++ b/src/dataclass_rest/method.py @@ -25,7 +25,7 @@ def __set_name__(self, owner, name): f"No type for bound method is specified. " f"Provide either `{owner.__name__}.method_class` attribute or " f"`method_class=` argument for decorator " - f"on your `{name}` method" + f"on your `{name}` method", ) def __get__( diff --git a/src/dataclass_rest/methodspec.py b/src/dataclass_rest/methodspec.py index bf42c61..83d4702 100644 --- a/src/dataclass_rest/methodspec.py +++ b/src/dataclass_rest/methodspec.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Type, Callable, List +from typing import Any, Callable, Dict, List, Type class MethodSpec: diff --git a/src/dataclass_rest/parse_func.py b/src/dataclass_rest/parse_func.py index 3c02e8f..20dbac9 100644 --- a/src/dataclass_rest/parse_func.py +++ b/src/dataclass_rest/parse_func.py @@ -1,6 +1,6 @@ import string -from inspect import getfullargspec, FullArgSpec, isclass -from typing import Callable, List, Sequence, Any, Type, TypedDict, Dict +from inspect import FullArgSpec, getfullargspec, isclass +from typing import Any, Callable, Dict, List, Sequence, Type, TypedDict from .http_request import File from .methodspec import MethodSpec diff --git a/tests/requests/conftest.py b/tests/requests/conftest.py index 9ee4254..849f945 100644 --- a/tests/requests/conftest.py +++ b/tests/requests/conftest.py @@ -3,6 +3,7 @@ from dataclass_rest.http import requests + @pytest.fixture def session(): return requests.Session() @@ -10,5 +11,7 @@ def session(): @pytest.fixture def mocker(session): - with requests_mock.Mocker(session=session, case_sensitive=True) as session_mock: + with requests_mock.Mocker( + session=session, case_sensitive=True, + ) as session_mock: yield session_mock diff --git a/tests/requests/test_factory.py b/tests/requests/test_factory.py index 4a5d762..d6a5ebc 100644 --- a/tests/requests/test_factory.py +++ b/tests/requests/test_factory.py @@ -1,7 +1,7 @@ from dataclasses import dataclass from enum import Enum -from adaptix import Retort, NameStyle, name_mapping +from adaptix import NameStyle, Retort, name_mapping from dataclass_rest import patch from dataclass_rest.http.requests import RequestsClient @@ -43,7 +43,7 @@ def _init_response_body_factory(self) -> Retort: @patch("/post/") def post_x(self, long_param: str, body: RequestBody) -> ResponseBody: - raise NotImplementedError() + raise NotImplementedError mocker.patch( url="http://example.com/post/?LONG.PARAM=hello", @@ -52,8 +52,11 @@ def post_x(self, long_param: str, body: RequestBody) -> ResponseBody: ) client = Api(base_url="http://example.com", session=session) result = client.post_x( - long_param="hello", body=RequestBody(int_param=42, selection=Selection.ONE), + long_param="hello", + body=RequestBody(int_param=42, selection=Selection.ONE), ) assert result == ResponseBody(int_param=1, selection=Selection.TWO) assert mocker.called_once - assert mocker.request_history[0].json() == {"intParam": 42, "selection": "ONE"} + + resp = mocker.request_history[0].json() + assert resp == {"intParam": 42, "selection": "ONE"} diff --git a/tests/requests/test_params.py b/tests/requests/test_params.py index 50a8573..bd8405b 100644 --- a/tests/requests/test_params.py +++ b/tests/requests/test_params.py @@ -1,19 +1,22 @@ from dataclasses import dataclass -from typing import Optional +from typing import List, Optional + +import requests +import requests_mock from dataclass_rest import get, post from dataclass_rest.http.requests import RequestsClient -def test_methods(session, mocker): +def test_methods(session: requests.Session, mocker: requests_mock.Mocker): class Api(RequestsClient): @get("/get") - def get_x(self) -> list[int]: - raise NotImplementedError() + def get_x(self) -> List[int]: + raise NotImplementedError @post("/post") - def post_x(self) -> list[int]: - raise NotImplementedError() + def post_x(self) -> List[int]: + raise NotImplementedError mocker.get("http://example.com/get", text="[1,2]", complete_qs=True) mocker.post("http://example.com/post", text="[1,2,3]", complete_qs=True) @@ -22,11 +25,11 @@ def post_x(self) -> list[int]: assert client.post_x() == [1, 2, 3] -def test_path_params(session, mocker): +def test_path_params(session: requests.Session, mocker: requests_mock.Mocker): class Api(RequestsClient): @post("/post/{id}") - def post_x(self, id) -> list[int]: - raise NotImplementedError() + def post_x(self, id) -> List[int]: + raise NotImplementedError mocker.post("http://example.com/post/1", text="[1]", complete_qs=True) mocker.post("http://example.com/post/2", text="[1,2]", complete_qs=True) @@ -35,15 +38,24 @@ def post_x(self, id) -> list[int]: assert client.post_x(2) == [1, 2] -def test_query_params(session, mocker): +def test_query_params(session: requests.Session, mocker: requests_mock.Mocker): class Api(RequestsClient): @post("/post/{id}") - def post_x(self, id: str, param: Optional[int]) -> list[int]: - raise NotImplementedError() - - mocker.post("http://example.com/post/x?", text="[0]", complete_qs=True) - mocker.post("http://example.com/post/x?param=1", text="[1]", complete_qs=True) - mocker.post("http://example.com/post/x?param=2", text="[1,2]", complete_qs=True) + def post_x(self, id: str, param: Optional[int]) -> List[int]: + raise NotImplementedError + + mocker.post( + url="http://example.com/post/x?", + text="[0]", complete_qs=True, + ) + mocker.post( + url="http://example.com/post/x?param=1", + text="[1]", complete_qs=True, + ) + mocker.post( + url="http://example.com/post/x?param=2", + text="[1,2]", complete_qs=True, + ) client = Api(base_url="http://example.com", session=session) assert client.post_x("x", None) == [0] assert client.post_x("x", 1) == [1] @@ -56,13 +68,16 @@ class RequestBody: y: str -def test_body(session, mocker): +def test_body(session: requests.Session, mocker: requests_mock.Mocker): class Api(RequestsClient): @post("/post/") def post_x(self, body: RequestBody) -> None: - raise NotImplementedError() + raise NotImplementedError - mocker.post("http://example.com/post/", text="null", complete_qs=True) + mocker.post( + url="http://example.com/post/", + text="null", complete_qs=True, + ) client = Api(base_url="http://example.com", session=session) assert client.post_x(RequestBody(x=1, y="test")) is None assert mocker.called_once diff --git a/tests/test_init.py b/tests/test_init.py index 42eedeb..e2226e8 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,7 +1,7 @@ from dataclasses import dataclass import pytest -from adaptix import Retort, NameStyle, name_mapping +from adaptix import NameStyle, Retort, name_mapping from requests import Session from dataclass_rest import get @@ -17,7 +17,10 @@ class Todo: def test_sync(): class RealClient(RequestsClient): def __init__(self): - super().__init__("https://jsonplaceholder.typicode.com/", Session()) + super().__init__( + "https://jsonplaceholder.typicode.com/", + Session(), + ) def _init_request_body_factory(self) -> Retort: return Retort(recipe=[