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

Custom response objects #398

Merged
merged 6 commits into from
Oct 6, 2024
Merged

Custom response objects #398

merged 6 commits into from
Oct 6, 2024

Conversation

novitae
Copy link
Contributor

@novitae novitae commented Sep 27, 2024

This is a PR related to #397

Here is the test I did:

>>> from curl_cffi.requests import AsyncSession, Session, Response
>>> import asyncio
>>> 
>>> class MyResponse(Response):
...     def __init__(self, *args, **kwargs):
...         super().__init__(*args, **kwargs)
...     @property
...     def status(self):
...         return self.status_code
... 
>>> async def main():
...     response = await AsyncSession(response_factory=MyResponse).get("https://www.lego.com")
...     print("AsyncSession with MyResponse as custom factory ->", type(response), response.status)
...     response = await AsyncSession().get("https://www.lego.com")
...     print("AsyncSession without custom factory ->", type(response))
...     response = Session(response_factory=MyResponse).get("https://www.lego.com")
...     print("Session with MyResponse as custom factory ->", type(response), response.status)
...     response = Session().get("https://www.lego.com")
...     print("Session without custom factory ->", type(response))
... 
>>> if __name__ == "__main__":
...     asyncio.run(main())
... 
AsyncSession with MyResponse as custom factory -> <class '__main__.MyResponse'> 200
AsyncSession without custom factory -> <class 'curl_cffi.requests.models.Response'>
Session with MyResponse as custom factory -> <class '__main__.MyResponse'> 200
Session without custom factory -> <class 'curl_cffi.requests.models.Response'>

@lexiforest
Copy link
Owner

I like the idea! Could you please add a few unittests in /tests and examples in /examples?

@novitae
Copy link
Contributor Author

novitae commented Oct 4, 2024

I like the idea! Could you please add a few unittests in /tests and examples in /examples?

Done !

curl_cffi/requests/session.py Outdated Show resolved Hide resolved
tests/integration/test_response_class.py Outdated Show resolved Hide resolved
examples/custom_response_class.py Show resolved Hide resolved
@lexiforest
Copy link
Owner

LGTM

@novitae
Copy link
Contributor Author

novitae commented Oct 5, 2024

Merge ?

@lexiforest lexiforest merged commit 81a5400 into lexiforest:main Oct 6, 2024
3 of 7 checks passed
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.

2 participants