From 380fad0eee7b929e9f92bbb12de9283e8ae2041e Mon Sep 17 00:00:00 2001 From: beckerfy <fynn.becker@hs-hannover.de> Date: Mon, 27 May 2019 14:45:09 +0200 Subject: [PATCH] Close #1 Improve configuration for python projects --- README.md | 6 +----- postgrestutils/client/postgrestclient.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d4afc76..ea9a9b2 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 64c008b..ddf0abd 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: -- GitLab