Skip to content

Commit

Permalink
Use headers properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Hallett committed Oct 26, 2023
1 parent 758702b commit 8756a10
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
24 changes: 16 additions & 8 deletions clientele/generators/standard/templates/async_methods.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 2 additions & 2 deletions clientele/generators/standard/templates/basic_client.jinja2
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions clientele/generators/standard/templates/bearer_client.jinja2
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion clientele/generators/standard/templates/client.jinja2
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

client = httpx.{{client_type}}(headers=c.additional_headers())
client_headers = c.additional_headers()
client = httpx.{{client_type}}(headers=client_headers)
16 changes: 12 additions & 4 deletions clientele/generators/standard/templates/sync_methods.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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)
30 changes: 19 additions & 11 deletions tests/async_test_client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
22 changes: 15 additions & 7 deletions tests/test_client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 8756a10

Please sign in to comment.