Skip to content

Commit d5cac55

Browse files
committed
Allow datetime strings, and custom datetime formatting
1 parent 004cc05 commit d5cac55

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

openapi_python_client/templates/client.py.jinja

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import attr
66
class Client:
77
""" A class for keeping track of data related to the API """
88

9+
10+
911
base_url: str
1012
cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1113
headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1214
timeout: float = attr.ib(5.0, kw_only=True)
1315
token: str = None
1416
verify: Optional[bool] = False
17+
date_format: str = '%Y-%m-%d'
18+
datetime_format: str = '%Y-%m-%dT%H:%M:%S.%f%z'
1519

1620
def get_headers(self) -> Dict[str, str]:
1721
""" Get headers to be used in all endpoints """

openapi_python_client/templates/property_templates/date_property.py.jinja

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ isoparse({{ source }}).date()
1212

1313
{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
1414
{% if property.required %}
15-
{{ destination }} = {{ source }}.isoformat() {% if property.nullable %}if {{ source }} else None {%endif%}
15+
{{ destination }} = {{ source }}.strftime(self.client.date_format) {% if property.nullable %}if {{ source }} else None {%endif%}
1616
{% else %}
1717
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
18-
if not isinstance({{ source }}, Unset):
18+
if not isinstance({{ source }}, (Unset, str)):
1919
{% if property.nullable %}
20-
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
20+
{{ destination }} = {{ source }}.strftime(self.client.date_format) if {{ source }} else None
2121
{% else %}
22-
{{ destination }} = {{ source }}.isoformat()
22+
{{ destination }} = {{ source }}.strftime(self.client.date_format)
2323
{% endif %}
2424
{% endif %}
2525
{% endmacro %}

openapi_python_client/templates/property_templates/datetime_property.py.jinja

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ isoparse({{ source }})
1313
{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
1414
{% if property.required %}
1515
{% if property.nullable %}
16-
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
16+
{{ destination }} = {{ source }}.strftime(self.client.datetime_format) if {{ source }} else None
1717
{% else %}
18-
{{ destination }} = {{ source }}.isoformat()
18+
{{ destination }} = {{ source }}.strftime(self.client.datetime_format)
1919
{% endif %}
2020
{% else %}
2121
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
22-
if not isinstance({{ source }}, Unset):
22+
if not isinstance({{ source }}, (Unset, str)):
2323
{% if property.nullable %}
24-
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
24+
{{ destination }} = {{ source }}.strftime(self.client.datetime_format) if {{ source }} else None
2525
{% else %}
26-
{{ destination }} = {{ source }}.isoformat()
26+
{{ destination }} = {{ source }}.strftime(self.client.datetime_format)
2727
{% endif %}
2828
{% endif %}
2929
{% endmacro %}

0 commit comments

Comments
 (0)