From 8756a10b7d4ae54dadaf2281432f239ae1d23eae Mon Sep 17 00:00:00 2001 From: Paul Hallett Date: Thu, 26 Oct 2023 13:16:20 +1300 Subject: [PATCH] Use headers properly --- .../standard/templates/async_methods.jinja2 | 24 ++++++++++----- .../standard/templates/basic_client.jinja2 | 4 +-- .../standard/templates/bearer_client.jinja2 | 6 ++-- .../standard/templates/client.jinja2 | 3 +- .../standard/templates/sync_methods.jinja2 | 16 +++++++--- tests/async_test_client/http.py | 30 ++++++++++++------- tests/test_client/http.py | 22 +++++++++----- 7 files changed, 69 insertions(+), 36 deletions(-) diff --git a/clientele/generators/standard/templates/async_methods.jinja2 b/clientele/generators/standard/templates/async_methods.jinja2 index 9923600..57979db 100644 --- a/clientele/generators/standard/templates/async_methods.jinja2 +++ b/clientele/generators/standard/templates/async_methods.jinja2 @@ -2,25 +2,33 @@ async def get(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP GET request""" - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.get(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.get(parse_url(url)) async def post(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP POST request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.post(parse_url(url), json=json_data, headers=headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.post(parse_url(url), json=json_data) async def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP PUT request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.put(parse_url(url), json=json_data, headers=headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.put(parse_url(url), json=json_data) async def delete(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP DELETE request""" - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.delete(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.delete(parse_url(url)) diff --git a/clientele/generators/standard/templates/basic_client.jinja2 b/clientele/generators/standard/templates/basic_client.jinja2 index 909e53e..b8bf1d8 100644 --- a/clientele/generators/standard/templates/basic_client.jinja2 +++ b/clientele/generators/standard/templates/basic_client.jinja2 @@ -1,2 +1,2 @@ - -client = httpx.{{client_type}}(auth=(c.get_user_key(), c.get_pass_key()), headers=c.additional_headers()) +client_headers = c.additional_headers() +client = httpx.{{client_type}}(auth=(c.get_user_key(), c.get_pass_key()), headers=client_headers) diff --git a/clientele/generators/standard/templates/bearer_client.jinja2 b/clientele/generators/standard/templates/bearer_client.jinja2 index c629df7..4d3d25b 100644 --- a/clientele/generators/standard/templates/bearer_client.jinja2 +++ b/clientele/generators/standard/templates/bearer_client.jinja2 @@ -1,6 +1,6 @@ auth_key = c.get_bearer_token() -headers = c.additional_headers() -headers.update(Authorization=f'Bearer {auth_key}') -client = httpx.{{client_type}}(headers=headers) +client_headers = c.additional_headers() +client_headers.update(Authorization=f'Bearer {auth_key}') +client = httpx.{{client_type}}(headers=client_headers) diff --git a/clientele/generators/standard/templates/client.jinja2 b/clientele/generators/standard/templates/client.jinja2 index 0182eca..f48446d 100644 --- a/clientele/generators/standard/templates/client.jinja2 +++ b/clientele/generators/standard/templates/client.jinja2 @@ -1,2 +1,3 @@ -client = httpx.{{client_type}}(headers=c.additional_headers()) +client_headers = c.additional_headers() +client = httpx.{{client_type}}(headers=client_headers) diff --git a/clientele/generators/standard/templates/sync_methods.jinja2 b/clientele/generators/standard/templates/sync_methods.jinja2 index 13ac3ce..1f5dfba 100644 --- a/clientele/generators/standard/templates/sync_methods.jinja2 +++ b/clientele/generators/standard/templates/sync_methods.jinja2 @@ -2,21 +2,29 @@ def get(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP GET request""" - return client.get(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + return client.get(parse_url(url), headers=client_headers) def post(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP POST request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - return client.post(parse_url(url), json=json_data, headers=headers) + return client.post(parse_url(url), json=json_data, headers=client_headers) def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP PUT request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - return client.put(parse_url(url), json=json_data, headers=headers) + return client.put(parse_url(url), json=json_data, headers=client_headers) def delete(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP DELETE request""" - return client.delete(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + return client.delete(parse_url(url), headers=client_headers) diff --git a/tests/async_test_client/http.py b/tests/async_test_client/http.py index 9767d8c..870ef71 100644 --- a/tests/async_test_client/http.py +++ b/tests/async_test_client/http.py @@ -117,32 +117,40 @@ def handle_response(func, response): } auth_key = c.get_bearer_token() -headers = c.additional_headers() -headers.update(Authorization=f"Bearer {auth_key}") -client = httpx.AsyncClient(headers=headers) +client_headers = c.additional_headers() +client_headers.update(Authorization=f"Bearer {auth_key}") +client = httpx.AsyncClient(headers=client_headers) async def get(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP GET request""" - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.get(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.get(parse_url(url)) async def post(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP POST request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.post(parse_url(url), json=json_data, headers=headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.post(parse_url(url), json=json_data) async def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP PUT request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.put(parse_url(url), json=json_data, headers=headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.put(parse_url(url), json=json_data) async def delete(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP DELETE request""" - async with httpx.AsyncClient(headers=headers) as async_client: - return await async_client.delete(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + async with httpx.AsyncClient(headers=client_headers) as async_client: + return await async_client.delete(parse_url(url)) diff --git a/tests/test_client/http.py b/tests/test_client/http.py index 7a259a3..1e95808 100644 --- a/tests/test_client/http.py +++ b/tests/test_client/http.py @@ -117,28 +117,36 @@ def handle_response(func, response): } auth_key = c.get_bearer_token() -headers = c.additional_headers() -headers.update(Authorization=f"Bearer {auth_key}") -client = httpx.Client(headers=headers) +client_headers = c.additional_headers() +client_headers.update(Authorization=f"Bearer {auth_key}") +client = httpx.Client(headers=client_headers) def get(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP GET request""" - return client.get(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + return client.get(parse_url(url), headers=client_headers) def post(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP POST request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - return client.post(parse_url(url), json=json_data, headers=headers) + return client.post(parse_url(url), json=json_data, headers=client_headers) def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP PUT request""" + if headers: + client_headers.update(headers) json_data = json.loads(json.dumps(data, default=json_serializer)) - return client.put(parse_url(url), json=json_data, headers=headers) + return client.put(parse_url(url), json=json_data, headers=client_headers) def delete(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: """Issue an HTTP DELETE request""" - return client.delete(parse_url(url), headers=headers) + if headers: + client_headers.update(headers) + return client.delete(parse_url(url), headers=client_headers)