Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add wrapper to prep data for JSON encoding #22

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

import httpx
from pydantic.json import pydantic_encoder

from .response import Response
from .config import Config, get_config
Expand Down Expand Up @@ -237,7 +238,7 @@ def _request(
content=content,
data=data,
files=files,
json=json,
json=self._convert(json),
headers=headers,
cookies=cookies,
)
Expand Down Expand Up @@ -269,7 +270,7 @@ async def _arequest(
content=content,
data=data,
files=files,
json=json,
json=self._convert(json),
headers=headers,
cookies=cookies,
)
Expand Down Expand Up @@ -298,6 +299,18 @@ def _check(
raise RequestFailed(rep)
return Response(response, response_model)

def _convert(self, obj: Any) -> Any:
if isinstance(obj, dict):
return {k: self._convert(v) for k, v in obj.items()}

if isinstance(obj, (list, tuple)):
return [self._convert(item) for item in obj]

if obj is None or isinstance(obj, (int, float, str, bool)):
return obj

return pydantic_encoder(obj)

# sync request and check
def request(
self,
Expand Down