Skip to content

Commit d3b900d

Browse files
Serialize body using Pydantic json serializer
Request default serializer is incompatible with datetime
1 parent a10d725 commit d3b900d

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

helloasso_api_wrapper/clients/checkout_intents_management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ def init_a_checkout(
3333
return self.api.callAndSerialize(
3434
f"/organizations/{organization_slug}/checkout-intents",
3535
InitCheckoutResponse,
36-
json=init_checkout_body.model_dump(),
36+
body=init_checkout_body,
3737
method="POST",
3838
)

helloasso_api_wrapper/clients/directory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_all_forms(
2727
"pageSize": page_size,
2828
"continuationToken": continuation_token,
2929
},
30-
json=list_forms_request.model_dump(),
30+
body=list_forms_request,
3131
method="POST",
3232
)
3333

@@ -48,6 +48,6 @@ def get_all_organizations(
4848
"pageSize": page_size,
4949
"continuationToken": continuation_token,
5050
},
51-
json=list_organizations_request_body.model_dump(),
51+
body=list_organizations_request_body,
5252
method="POST",
5353
)

helloasso_api_wrapper/clients/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def create_a_simplified_event(
2323
return self.api.callAndSerialize(
2424
f"/organizations/{organization_slug}/forms/{form_type}/action/quick-create",
2525
FormQuickCreateModel,
26-
json=quick_form_create_request_body.model_dump(),
26+
body=quick_form_create_request_body,
2727
method="POST",
2828
)
2929

helloasso_api_wrapper/ha_apiv5_extension.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,30 @@ def callAndSerialize(
6161
model: type[Model],
6262
params: dict | None = None,
6363
method: str | None = "GET",
64-
data: dict | None = None,
65-
json: dict | None = None,
64+
body: BaseModel | None = None,
6665
headers: dict | None = None,
6766
include_auth: bool = True,
6867
) -> Model:
68+
"""
69+
Call the request using HaApiV5 and then serialize the response.
70+
71+
The `body` will be serialized to json using Pydantic and passed to HaAPIV5.
72+
"""
73+
74+
# The default json serializer used by Requests does not handle datetime
75+
# We will use Pydantic serializer instead then pass the body as `data`, including an "application/json" header
76+
# See https://github.com/psf/requests/issues/3947
77+
data = body.model_dump_json() if body else None
78+
if headers is None:
79+
headers = {}
80+
headers["Content-type"] = "application/json"
81+
6982
sub_path = "/v5" + sub_path
7083
response = self.call(
7184
sub_path,
7285
params=params,
7386
method=method,
7487
data=data,
75-
json=json,
7688
headers=headers,
7789
include_auth=include_auth,
7890
).json()

0 commit comments

Comments
 (0)