Skip to content

Commit

Permalink
WIP datasette.client, refs #943
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 30, 2020
1 parent 5b8b8ae commit fa3ba68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import glob
import hashlib
import httpx
import inspect
import itertools
from itsdangerous import BadSignature
Expand Down Expand Up @@ -312,6 +313,7 @@ def __init__(
self._register_renderers()
self._permission_checks = collections.deque(maxlen=200)
self._root_token = secrets.token_hex(32)
self.client = DatasetteClient(self)

async def invoke_startup(self):
for hook in pm.hook.startup(datasette=self):
Expand Down Expand Up @@ -1209,3 +1211,34 @@ def route_pattern_from_filepath(filepath):

class NotFoundExplicit(NotFound):
pass


class DatasetteClient:
def __init__(self, ds):
self._client = httpx.AsyncClient(app=ds.app())

def _fix(self, path):
if path.startswith("/"):
path = "http://localhost{}".format(path)
return path

async def get(self, path, **kwargs):
return await self._client.get(self._fix(path), **kwargs)

async def options(self, path, **kwargs):
return await self._client.options(self._fix(path), **kwargs)

async def head(self, path, **kwargs):
return await self._client.head(self._fix(path), **kwargs)

async def post(self, path, **kwargs):
return await self._client.post(self._fix(path), **kwargs)

async def put(self, path, **kwargs):
return await self._client.put(self._fix(path), **kwargs)

async def patch(self, path, **kwargs):
return await self._client.patch(self._fix(path), **kwargs)

async def delete(self, path, **kwargs):
return await self._client.delete(self._fix(path), **kwargs)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def get_version():
"click-default-group~=1.2.2",
"Jinja2>=2.10.3,<2.12.0",
"hupper~=1.9",
"httpx~=0.15",
"pint~=0.9",
"pluggy~=0.13.0",
"uvicorn~=0.11",
Expand Down

0 comments on commit fa3ba68

Please sign in to comment.