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 httpx deprecation warnings #2307

Closed
simonw opened this issue Mar 15, 2024 · 1 comment
Closed

Fix httpx deprecation warnings #2307

simonw opened this issue Mar 15, 2024 · 1 comment
Labels
Milestone

Comments

@simonw
Copy link
Owner

simonw commented Mar 15, 2024

Lots of these: https://github.com/simonw/datasette/actions/runs/8258529260/job/22590924898

  /opt/hostedtoolcache/Python/3.12.2/x64/lib/python3.12/site-packages/httpx/_client.py:1426:
  DeprecationWarning: The 'app' shortcut is now deprecated. Use the explicit style 'transport=ASGITransport(app=...)' instead.

And:

  /opt/hostedtoolcache/Python/3.12.2/x64/lib/python3.12/site-packages/httpx/_client.py:1559:
DeprecationWarning: Setting per-request cookies=<...> is being deprecated, because the expected behaviour on cookie persistence is ambiguous. Set cookies directly on the client instance instead.
@simonw simonw added the tests label Mar 15, 2024
@simonw
Copy link
Owner Author

simonw commented Mar 15, 2024

I can handle both of these in the datasette.client class, such that I don't have to change how dozens of tests that use datasette.client.get(..., cookie=...) work across many different plugins.

datasette/datasette/app.py

Lines 1920 to 1969 in 8b6f155

class DatasetteClient:
def __init__(self, ds):
self.ds = ds
self.app = ds.app()
def actor_cookie(self, actor):
# Utility method, mainly for tests
return self.ds.sign({"a": actor}, "actor")
def _fix(self, path, avoid_path_rewrites=False):
if not isinstance(path, PrefixedUrlString) and not avoid_path_rewrites:
path = self.ds.urls.path(path)
if path.startswith("/"):
path = f"http://localhost{path}"
return path
async def get(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.get(self._fix(path), **kwargs)
async def options(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.options(self._fix(path), **kwargs)
async def head(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.head(self._fix(path), **kwargs)
async def post(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.post(self._fix(path), **kwargs)
async def put(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.put(self._fix(path), **kwargs)
async def patch(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.patch(self._fix(path), **kwargs)
async def delete(self, path, **kwargs):
async with httpx.AsyncClient(app=self.app) as client:
return await client.delete(self._fix(path), **kwargs)
async def request(self, method, path, **kwargs):
avoid_path_rewrites = kwargs.pop("avoid_path_rewrites", None)
async with httpx.AsyncClient(app=self.app) as client:
return await client.request(
method, self._fix(path, avoid_path_rewrites), **kwargs
)

simonw added a commit that referenced this issue Mar 15, 2024
@simonw simonw added this to the Datasette 1.0 milestone Mar 15, 2024
@simonw simonw closed this as completed in eb8545c Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant