Closed dosisod closed 1 year ago
It seems httpx does not provide a way to custom json encoder. We should do this type transform before using httpx.
We can simply add a wrapper to json dict using pydantic's default encoder pydantic_encoder
.
This wrapper may be added to both _request
and _arequest
function to take effect. PR welcome 👍🏻
The following snippet:
Fails with the following exception(s):
When you look at the code for the
async_check
function:https://github.com/yanyongyu/githubkit/blob/bc1eb965f02c88a22cf9da8bdb7a13adb1a73719/githubkit/rest/checks.py#L274
You will see that the
.dict()
method is called, which will return a Python dict, not a JSON-safe dict (which is what thejson
variable implies). This "json" object gets propogated all the way tohttpx
, and when it tries to JSON encode it, it fails.Some ideas for how to fix this:
.json()
function, though it returns a string and not adict
. We could replace every call tojson.dict(...)
withloads(json.json(...))
, whereloads
is thejson.loads
method from the stdlib, though there are a lot of API calls we would have to do this for, and it would introduce some overhead.dict
object that stringifiesdatetime
objects using.isoformat()
. By adding said function right here (and to all the other API helper functions): https://github.com/yanyongyu/githubkit/blob/bc1eb965f02c88a22cf9da8bdb7a13adb1a73719/githubkit/core.py#L346-L356 We could stringify all datetime objects without changing every API function. There might be other object types other than datetimes which we might also want to support, datetimes just happened to be the ones that where giving me trouble.I really like this library, but this bug is keeping me from using it in production :cry:. I wouldn't mind opening a PR for this, it sounds easy enough, assuming we go with option 2.