From 8caa03b02663d6ec2b3c1c5d24b4f0271a2cbc8a Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Tue, 27 Feb 2018 15:54:31 +0100 Subject: [PATCH] Refactor and add setup.py --- .gitignore | 4 +++ README.md | 19 -------------- README.rst | 19 ++++++++++++++ daiquiri_client/__init__.py | 32 +++++++++++++++++------- daiquiri_client/auth.py | 3 +++ daiquiri_client/client.py | 50 +++++++++++++++++++++++++++++++++++++ daiquiri_client/query.py | 25 +++++++++++++++++++ setup.py | 26 +++++++++++++++++++ 8 files changed, 150 insertions(+), 28 deletions(-) delete mode 100644 README.md create mode 100644 README.rst create mode 100644 daiquiri_client/client.py create mode 100644 daiquiri_client/query.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 074fde8..5cd89c2 100755 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,10 @@ *.swp .DS_Store +*.egg-info/ +dist/ +build/ + env env3 diff --git a/README.md b/README.md deleted file mode 100644 index 6d6f7dd..0000000 --- a/README.md +++ /dev/null @@ -1,19 +0,0 @@ -Daiquiri Client -=============== - -Daiquiri Client is a python library meant to be used with the Daiquiri Framework. - -Daiquiri can be downloaded from [https://github.com/aipescience/django-daiquiri](https://github.com/aipescience/django-daiquiri). - -Daiquiri Client provides a set of functions which can be used to use the API of a Daiquiri powered website inside a script. The nessesarry http requests are abstracted in a transparent way. - -A script for getting the emails of all users using Daiquiri Client could look like this: - -```python -from daiquiri_client import Client - -client = Client('http://localhost:8000', '6f3d17e17e46c8e188b4c285ebb53a3fa3ce98c6') - -for profile in client.auth.get_profiles(): - print(profile['user']['email']) -``` diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..4440ae4 --- /dev/null +++ b/README.rst @@ -0,0 +1,19 @@ +Daiquiri Client +=============== + +Daiquiri Client is a python library meant to be used with the Daiquiri Framework. + +Daiquiri can be downloaded from `https://github.com/aipescience/django-daiquiri `_. + +Daiquiri Client provides a set of functions which can be used to use the API of a Daiquiri powered website inside a script. The nessesarry HTTP requests are abstracted in a transparent way. + +A script for getting the emails of all users using Daiquiri Client could look like this: + +.. code:: python + + from daiquiri_client import Client + + client = Client('http://localhost:8000', '6f3d17e17e46c8e188b4c285ebb53a3fa3ce98c6') + + for profile in client.auth.get_profiles(): + print(profile['user']['email']) diff --git a/daiquiri_client/__init__.py b/daiquiri_client/__init__.py index 76ff0f3..d16ab79 100644 --- a/daiquiri_client/__init__.py +++ b/daiquiri_client/__init__.py @@ -1,19 +1,23 @@ import requests +import simplejson from auth import Auth from metadata import Metadata +from query import Query class Client(object): - def __init__(self, base_url, token): + def __init__(self, base_url, token=None): self.base_url = base_url - self.headers = { - 'Authorization': 'Token %s' % token - } + + self.headers = {} + if token: + self.headers['Authorization'] = 'Token %s' % token self.auth = Auth(self) self.metadata = Metadata(self) + self.query = Query(self) def get(self, url): response = requests.get(self.base_url + url, headers=self.headers) @@ -22,15 +26,25 @@ def get(self, url): def post(self, url, data): response = requests.post(self.base_url + url, data, headers=self.headers) - response.raise_for_status() - return response.json() + try: + response.raise_for_status() + return response.json() + except requests.exceptions.HTTPError as e: + try: + print(response.json()) + except simplejson.scanner.JSONDecodeError: + pass + raise e def put(self, url, data): response = requests.put(self.base_url + url, data, headers=self.headers) - response.raise_for_status() - return response.json() + try: + response.raise_for_status() + return response.json() + except requests.exceptions.HTTPError as e: + print(response.json()) + raise e def delete(self, url): response = requests.delete(self.base_url + url, headers=self.headers) response.raise_for_status() - return response.json() diff --git a/daiquiri_client/auth.py b/daiquiri_client/auth.py index bc039d6..834934f 100644 --- a/daiquiri_client/auth.py +++ b/daiquiri_client/auth.py @@ -8,3 +8,6 @@ def get_profiles(self): def get_groups(self): return self.client.get('/auth/api/groups/') + + def get_group_map(self): + return {group['id']: group['name'] for group in self.get_groups()} diff --git a/daiquiri_client/client.py b/daiquiri_client/client.py new file mode 100644 index 0000000..d16ab79 --- /dev/null +++ b/daiquiri_client/client.py @@ -0,0 +1,50 @@ +import requests +import simplejson + +from auth import Auth +from metadata import Metadata +from query import Query + + +class Client(object): + + def __init__(self, base_url, token=None): + self.base_url = base_url + + self.headers = {} + if token: + self.headers['Authorization'] = 'Token %s' % token + + self.auth = Auth(self) + self.metadata = Metadata(self) + self.query = Query(self) + + def get(self, url): + response = requests.get(self.base_url + url, headers=self.headers) + response.raise_for_status() + return response.json() + + def post(self, url, data): + response = requests.post(self.base_url + url, data, headers=self.headers) + try: + response.raise_for_status() + return response.json() + except requests.exceptions.HTTPError as e: + try: + print(response.json()) + except simplejson.scanner.JSONDecodeError: + pass + raise e + + def put(self, url, data): + response = requests.put(self.base_url + url, data, headers=self.headers) + try: + response.raise_for_status() + return response.json() + except requests.exceptions.HTTPError as e: + print(response.json()) + raise e + + def delete(self, url): + response = requests.delete(self.base_url + url, headers=self.headers) + response.raise_for_status() diff --git a/daiquiri_client/query.py b/daiquiri_client/query.py new file mode 100644 index 0000000..3152e8e --- /dev/null +++ b/daiquiri_client/query.py @@ -0,0 +1,25 @@ +class Query(): + + def __init__(self, client): + self.client = client + + def get_jobs(self): + return self.client.get('/query/api/jobs/') + + def get_job(self, pk): + return self.client.get('/query/api/jobs/%s/' % pk) + + def create_job(self, data): + return self.client.post('/query/api/jobs/', data) + + def update_job(self, pk, data): + return self.client.put('/query/api/jobs/%s/' % pk, { + 'table_name': data['table_name'] + }) + + def delete_job(self, pk): + self.client.delete('/query/api/jobs/%s/' % pk) + return {'id': pk} + + def abort_job(self, pk): + return self.client.put('/query/api/jobs/%s/abort/' % pk, {}) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3212836 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup, find_packages + +version = '0.1.0' + +author = 'Jochen Klar' +email = 'jklar@aip.de' + +setup( + name='django-daiquiri-client', + version=version, + author=author, + author_email=email, + maintainer=author, + maintainer_email=email, + license='Apache-2.0', + url='https://github.com/aipescience/django-daiquiri-client', + description=u'Daiquiri Client is a python library meant to be used with the Daiquiri Framework.', + long_description=open('README.rst').read(), + install_requires=[ + 'requests>=2.18.4', + 'simplejson>= 3.11.1', + ], + classifiers=[], + packages=find_packages(), + include_package_data=True +)