diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a1810ea..fe477df 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: - id: trailing-whitespace - repo: https://github.com/psf/black - rev: 20.8b1 + rev: 22.3.0 hooks: - id: black diff --git a/Makefile b/Makefile index 5848ef0..2eb3dbe 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ flake8: install-dev $(VENV)/bin/flake8 setup.py alma tests mypy: install-dev - $(VENV)/bin/mypy --ignore-missing-imports --scripts-are-modules alma + $(VENV)/bin/mypy --ignore-missing-imports --scripts-are-modules --implicit-optional alma build-requirements: $(VIRTUALENV) $(TEMPDIR) diff --git a/alma_client/async_client.py b/alma_client/async_client.py index 126ce2e..dfcecad 100644 --- a/alma_client/async_client.py +++ b/alma_client/async_client.py @@ -9,14 +9,8 @@ async def process_request(req): - request = httpx.Request( - req.method, - req.url, - headers=req.headers, - cookies=req.cookies, - params=req.params, - json=req.body, - ) + + request = req.to_httpx() async with httpx.AsyncClient() as client: resp = await client.send(request) diff --git a/alma_client/client.py b/alma_client/client.py index 234ff97..ccef6e2 100644 --- a/alma_client/client.py +++ b/alma_client/client.py @@ -20,14 +20,8 @@ def process_request(req): - request = httpx.Request( - req.method, - req.url, - headers=req.headers, - cookies=req.cookies, - params=req.params, - json=req.body, - ) + + request = req.to_httpx() with httpx.Client() as client: resp = client.send(request) diff --git a/alma_client/request.py b/alma_client/request.py index 670c5ea..799b5df 100644 --- a/alma_client/request.py +++ b/alma_client/request.py @@ -1,18 +1,8 @@ -from functools import wraps +import json -from .paginated_results import PaginatedResults - - -def configure_credentials(func): - def decorator(f): - @wraps(f) - def decorated(request, *args, **kwargs): - request.context.credentials.configure(request) - return f(request, *args, **kwargs) +import httpx - return decorated - - return decorator(func) +from .paginated_results import PaginatedResults class RequestError(Exception): @@ -58,22 +48,36 @@ def expectPaginatedList(self, cls, next_page): self.response_processor = lambda response: PaginatedResults(response.json, cls, next_page) return self - @configure_credentials def get(self): self.method = "GET" return self - @configure_credentials def post(self): self.method = "POST" return self - @configure_credentials def put(self): self.method = "PUT" return self - @configure_credentials def delete(self): self.method = "DELETE" return self + + @property + def data(self): + return json.dumps(self.body) + + def to_httpx(self): + self.context.credentials.configure(self) + headers = self.headers.copy() + headers["Content-Type"] = "application/json" + req = httpx.Request( + self.method, + self.url, + headers=headers, + cookies=self.cookies, + params=self.params, + content=self.data, + ) + return req diff --git a/setup.py b/setup.py index b3ca196..9466938 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ # This call to setup() does all the work setup( name="alma-client", - version="3.1.0.dev0", + version="3.2.0", description="Python API client for the Alma Installments API", long_description=f"{README}\n\n{CHANGELOG}", long_description_content_type="text/markdown", diff --git a/tests/test_request.py b/tests/test_request.py index 3c4ad96..9abc6a1 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -10,19 +10,10 @@ def setUp(self) -> None: self.credentials = client.context.credentials self.request = Request(client.context, "https://url.com") - def assert_method_calls_configure(self, method): + def test_credentials_configure_is_called_on_request_build(self): with mock.patch("alma_client.credentials.ApiKeyCredentials.configure") as mocked_configure: - getattr(self.request, method)() + self.request.get() + mocked_configure.assert_not_called() + # only upon building the httpx Req are the credentials configured + _ = self.request.to_httpx() mocked_configure.assert_called_once_with(self.request) - - def test_credentials_configure_is_called_on_get(self): - self.assert_method_calls_configure("get") - - def test_credentials_configure_is_called_on_post(self): - self.assert_method_calls_configure("post") - - def test_credentials_configure_is_called_on_put(self): - self.assert_method_calls_configure("put") - - def test_credentials_configure_is_called_on_delete(self): - self.assert_method_calls_configure("delete") diff --git a/tox.ini b/tox.ini index 82a1007..49bedac 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ python = [testenv] commands = - mypy --ignore-missing-imports --scripts-are-modules alma_client + mypy --ignore-missing-imports --scripts-are-modules --implicit-optional alma_client pytest tests --cov-report term-missing --cov alma_client {posargs} # --cov-fail-under 100 deps = -rtest-requirements.txt