Replies: 5 comments 1 reply
-
One suggestion for how this might be implemented: the |
Beta Was this translation helpful? Give feedback.
-
Hello! Those are interesting points. I can see two topics here: 1/ ability to turn off cookie persistance, and 2/ refining the "unclosed client warning" so it is skipped under certain conditions. For 1/ — I don't think that's something we discussed or came across yet, mainly because there hasn't been a push or need for it yet. I don't think there's any workaround to a Lines 1324 to 1332 in 7fda99f Lines 1118 to 1124 in 7fda99f But we can certainly go through and change things around, if we find out that I think we may be okay with a separate ticket for optional cookie persistence, in any case? As for 2/, for the ASGI transport case incidentally Is there a way you can call # conftest.py
@pytest.fixture(scope="session")
async def client():
async with httpx.AsyncClient() as client:
datasette.some_init_hook(client=client) # `.some_init_hook` could also be private/internal. Maybe that initialisation hook doesn't exist for now (I haven't looked through the code, though the docs mention "the Alternatively, since Datasette uses ASGI underneath, you might be able to register (There's also a more general concern there about "what do we do with transports that don't keep persistent resources in practice?" (like for mock transports, or ones whose resources are on a per-request basis, etc). I'm not sure we can do anything better than to just ignore that specificity, and default to an agnostic "warn if a request was sent but |
Beta Was this translation helpful? Give feedback.
-
Yeah so, two different things here. I'm absolutely in favour of supporting a no-cookie-persistence option. I've been surprised in the past that it doesn't(?) exist in requests, particularly because if you're eg. using In practise folks just tend not to notice that since they won't have differing endpoints that rely on cookie persistence between requests/responses. But all the same, you end up using a client that'll have potentially odd and inconsistent behaviour wrt cookie persistence. So, yes I think I'd like us to support this. Perhaps, with this API?... client = httpx.Client(cookies=False) In the meantime I think you can achieve the same behaviour with something like... from http import cookiejar
import httpx
class NullCookieJar(cookiejar.CookieJar):
def extract_cookies(self, request, response):
pass
client = httpx.Client(cookies=NullCookieJar()) Wrt. not closing clients. Yes, it's true that the open/close constraint exists on the transport, not on the client itself. |
Beta Was this translation helpful? Give feedback.
-
@tomchristie I'm wondering if there has been any progress on not persisting cookies across requests since your 2020 comment. To me it's also a no-brainer to have that option, or even to not persist cookies between requests by default. The only reference to shared cookies I see in the docs is if one passes cookies as an argument to the |
Beta Was this translation helpful? Give feedback.
-
Please correct me, but I think the issue is right here: Line 1010 in a934c36 and Line 1722 in a934c36 So if an option was added, like So the code would be something like:
Maybe it's just me, but during a minor or major version bump, I'd love to see this default to |
Beta Was this translation helpful? Give feedback.
-
I've started using
httpx.AsyncClient
internally in Datasette, for two purposes:await datasette.client.get("/db/table.json")
, which lets plugin authors make in-process calls to Datasette's external JSON API, without the overhead of an HTTP network request - see https://docs.datasette.io/en/stable/internals.html#datasette-clientIn both of these cases I instantiate a new
AsyncClient
instance for every call.This has a small but measurable performance impact on my unit tests, since I need to instantiate a brand new client object for every one of several hundred tests.
I experimented with reusing a client object created like this (instead of using the
with ...
context manager):This had two problems: it persisted cookies, which isn't what I want for my test suite. It also produced warnings about unclosed clients thanks to this code:
httpx/httpx/_client.py
Lines 1779 to 1785 in ca5f524
As far as I can tell the need to open and close a client relates to the use of proxies and persistent HTTP connections. If I'm using the client to exercise an ASGI app directly I don't believe the open/closing mechanism is necessary at all.
So, my feature request: I'd love to be able to reuse a single
AsyncClient
instance in a way that doesn't persist cookies between requests and doesn't produce warnings about the client being unclosed.Beta Was this translation helpful? Give feedback.
All reactions