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

feat: add ability to define own loads function for meatie_aiohttp client #25

Closed
wants to merge 1 commit into from

Conversation

dswistowski
Copy link

@dswistowski dswistowski commented Jul 30, 2024

i did consider to add loads as parameter to Client init, but because it's impact return types of endpoint - added class method

Comment on lines +36 to +38
@classmethod
def loads(cls) -> JSONDecoder:
return DEFAULT_JSON_DECODER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you modify the way the decoder is being handled? The current approach with a class method requires users who would like to change the DEFAULT_JSON_DECODER to use inheritance.

Instead, could you add the loads argument to constructor with the default decoder used by aiohttp? This way it will hopefully simplify the change of the default serializer if needed. It should also make it more clear, since the argument will be present in the constructor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think loads should be a constructor parameter. The decision on how JSON should be parsed is the client creator's decision, not the client user's.
Changing the loads might make returning types from methods wrong or broken.

It's possible to introduce metaclass and add loads as a constrictor parameter for meatie_*.Client class:

from meatie_aiohttp import Client

class MyClient(Client, loads=custom_loads):
   ....

but this solution confuses library users much more than adding class method and ability to override it

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Damian,

I revised carefully how the mainstream Python HTTP client libraries (aiohttp, requests, and xhttp) handle the deserialization of JSON or text in the HTTP response body.

I summarised it below:

aiohttp supports replacing json.loads and encoding
httpx supports passing additional parameters to json.loads
requests support passing additional parameters to json.loads and encoding

Consequently, every library takes a slightly different approach. Only aiohttp library allows using a custom JSON decoder that doesn't come from the Python standard library.

Aiming to keep Meatie independent of the HTTP client library I introduced the body parameter that allows specifying a custom function that extracts JSON from the HTTP response (#28). It enables custom JSON processing on a per-endpoint basis. The analogous mechanism is implemented for extracting text. I added this feature for all HTTP clients supported by Meatie.

Essentially, the client developer decides how JSON should be processed, which is aligned with what you postulate.

As a digression, I was experimenting with metaclasses before the first public release of the Meatie. Metaclasses were poorly supported by mypy and the IntelliJ code checker. Almost every method of a Meatie client was highlighted incorrectly as having a wrong type. Therefore, I decided to abandon it in favor of the current implementation.

Thank you for the feature proposal and implementation for aiohttp. I hope the final implementation supports your use case.

pmateusz added a commit that referenced this pull request Aug 4, 2024
credits to @dswistowski for suggesting the feature and writing proposal for aiohttp #25
@dswistowski dswistowski closed this Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants