Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from alma/feature/rearrange-credentials-creation
Browse files Browse the repository at this point in the history
Configure credentials after request is fully built
  • Loading branch information
b-m-alm authored Nov 15, 2022
2 parents 32f46a3 + 29fc863 commit 395a000
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 20.8b1
rev: 22.3.0
hooks:
- id: black

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions alma_client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 2 additions & 8 deletions alma_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
38 changes: 21 additions & 17 deletions alma_client/request.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 5 additions & 14 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 395a000

Please sign in to comment.