diff --git a/README.md b/README.md index d4afc7674abb71472cceee0e39c820950681c889..ea9a9b2e549ba2086e707f95d48938f30f975652 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,8 @@ res = pgrest_client.get("kerbals", params=payload) ##### Other projects ```python -from postgrestutils import settings - -settings.BASE_URI = "http://localhost:3000" -settings.JWT = "" - from postgrestutils.client import pgrest_client +pgrest_client.configure('your-JWT', base_uri='http://127.0.0.1:3000') payload = { "select": "id,forename" diff --git a/postgrestutils/client/postgrestclient.py b/postgrestutils/client/postgrestclient.py index 64c008b630fb2d96aab1c1be88708da1978b7ad7..ddf0abd182a87a3958ea17b38f9ac73336578c3f 100644 --- a/postgrestutils/client/postgrestclient.py +++ b/postgrestutils/client/postgrestclient.py @@ -6,16 +6,23 @@ from postgrestutils.client.utils import datetime_parser class PostgrestClient: - def __init__(self, base_uri, token): - self.base_uri = base_uri + def __init__(self, base_uri, token=None): self.session = requests.Session() - self.session.headers["Authorization"] = "Bearer {}".format(token) + self.configure(token, base_uri=base_uri) + self.session.headers['Accept'] = 'application/json' + + def configure(self, token, base_uri=None): + if base_uri is not None: + self.base_uri = base_uri + if token: + self.session.headers['Authorization'] = 'Bearer {}'.format(token) def get(self, path, singular=False, parse_dt=True, **kwargs): """ :param path: specifies the endpoint :param singular: if True returns a JSON object rather than a list (406 when multiple results are returned) :param parse_dt: if True attempts to parse datetime strings to python datetime objects + :param kwargs: pass kwargs directly to requests's .get() method :return: result(s) as python object or raises HTTPError """ if singular: