Skip to content

Commit

Permalink
Add custom client name
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Sep 8, 2022
1 parent cadea15 commit fcc80c7
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 252 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ pets: list[Pet] | EmptyBody = await client.findPetsByStatus(status="available")
```
- Make changes
- Execute `make clients-for-tests && make test-clients`
- Execute `make clients-for-examples`
140 changes: 61 additions & 79 deletions examples/petstore/client_async.py

Large diffs are not rendered by default.

140 changes: 61 additions & 79 deletions examples/petstore/client_sync.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pythogen"
version = "0.0.27"
version = "0.0.28"
description = "Generator of python HTTP-clients from OpenApi specification."
homepage = "https://github.com/artsmolin/pythogen"
repository = "https://github.com/artsmolin/pythogen"
Expand Down
3 changes: 1 addition & 2 deletions pythogen/templates/client/httpx-method.j2
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@

if response.status_code == {{ code }}:
{% if code | int >= 400 -%}
client_name = "{{ name | replace('Client', '') | lower }}"
method = "{{ method }}"
if response.content is None:
content = None
else:
content = response.content[:500]

self.log_error(client_name, method, url, params, content, headers_)
self.log_error(self.client_name, method, url, params, content, headers_)

{% endif -%}
return {{ mapper }}
Expand Down
4 changes: 2 additions & 2 deletions pythogen/templates/client/httpx-request-metrics.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{%- endif %}
except Exception as exc:
if self.metrics_integration:
self.metrics_integration.on_request_error("{{ name | replace('Client', '') | lower }}", exc, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}")
self.metrics_integration.on_request_error(self.client_name, exc, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}")
raise exc
{#
https://www.python-httpx.org/api/#response
Expand All @@ -16,4 +16,4 @@
total_seconds() to correctly get the total elapsed seconds.
#}
if self.metrics_integration:
self.metrics_integration.on_request_success("{{ name | replace('Client', '') | lower }}", response, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}")
self.metrics_integration.on_request_success(self.client_name, response, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}")
8 changes: 5 additions & 3 deletions pythogen/templates/httpx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,22 @@ class {{ classname(model.id) }}(BaseModel):


class BasicAuth(BaseModel):
username: str
password: str
username: str
password: str


class {{ name }}:
def __init__(
self,
base_url: str,
timeout: int = 5,
client_name: str = "{{ name | replace('Client', '') | lower }}",
{%- if sync %}
client: Optional[httpx.Client] = None,
{%- else %}
client: Optional[httpx.AsyncClient] = None,
{%- endif %}
headers: Dict[str, str] = None,
headers: Optional[Dict[str, str]] = None,
tracer_integration: Optional[BaseTracerIntegration] = None,
metrics_integration: Optional[BaseMetricsIntegration] = None,
):
Expand All @@ -311,6 +312,7 @@ class {{ name }}:
self.headers = headers or {}
self.tracer_integration = tracer_integration
self.metrics_integration=metrics_integration
self.client_name = client_name

{#- get items begin #}
{% with items=get.items(), method='get'%}
Expand Down
86 changes: 43 additions & 43 deletions tests/clients/async_client.py

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions tests/clients/sync_client.py

Large diffs are not rendered by default.

0 comments on commit fcc80c7

Please sign in to comment.