Skip to content

Commit

Permalink
Refactor and add setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Feb 27, 2018
1 parent baf7b68 commit 8caa03b
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*.swp
.DS_Store

*.egg-info/
dist/
build/

env
env3

Expand Down
19 changes: 0 additions & 19 deletions README.md

This file was deleted.

19 changes: 19 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -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 <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'])
32 changes: 23 additions & 9 deletions daiquiri_client/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
3 changes: 3 additions & 0 deletions daiquiri_client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
50 changes: 50 additions & 0 deletions daiquiri_client/client.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions daiquiri_client/query.py
Original file line number Diff line number Diff line change
@@ -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, {})
26 changes: 26 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from setuptools import setup, find_packages

version = '0.1.0'

author = 'Jochen Klar'
email = '[email protected]'

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
)

0 comments on commit 8caa03b

Please sign in to comment.