mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Backport new DatasetteClient implementation, refs #2443
This commit is contained in:
parent
2eba9a6630
commit
bd818aef62
1 changed files with 18 additions and 15 deletions
|
|
@ -1568,37 +1568,40 @@ class DatasetteClient:
|
||||||
path = f"http://localhost{path}"
|
path = f"http://localhost{path}"
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
async def _request(self, method, path, **kwargs):
|
||||||
|
async with httpx.AsyncClient(
|
||||||
|
transport=httpx.ASGITransport(app=self.app),
|
||||||
|
cookies=kwargs.pop("cookies", None),
|
||||||
|
) as client:
|
||||||
|
return await getattr(client, method)(self._fix(path), **kwargs)
|
||||||
|
|
||||||
async def get(self, path, **kwargs):
|
async def get(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("get", path, **kwargs)
|
||||||
return await client.get(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def options(self, path, **kwargs):
|
async def options(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("options", path, **kwargs)
|
||||||
return await client.options(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def head(self, path, **kwargs):
|
async def head(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("head", path, **kwargs)
|
||||||
return await client.head(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def post(self, path, **kwargs):
|
async def post(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("post", path, **kwargs)
|
||||||
return await client.post(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def put(self, path, **kwargs):
|
async def put(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("put", path, **kwargs)
|
||||||
return await client.put(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def patch(self, path, **kwargs):
|
async def patch(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("patch", path, **kwargs)
|
||||||
return await client.patch(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def delete(self, path, **kwargs):
|
async def delete(self, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
return await self._request("delete", path, **kwargs)
|
||||||
return await client.delete(self._fix(path), **kwargs)
|
|
||||||
|
|
||||||
async def request(self, method, path, **kwargs):
|
async def request(self, method, path, **kwargs):
|
||||||
avoid_path_rewrites = kwargs.pop("avoid_path_rewrites", None)
|
avoid_path_rewrites = kwargs.pop("avoid_path_rewrites", None)
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
async with httpx.AsyncClient(
|
||||||
|
transport=httpx.ASGITransport(app=self.app),
|
||||||
|
cookies=kwargs.pop("cookies", None),
|
||||||
|
) as client:
|
||||||
return await client.request(
|
return await client.request(
|
||||||
method, self._fix(path, avoid_path_rewrites), **kwargs
|
method, self._fix(path, avoid_path_rewrites), **kwargs
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue