Skip to content

Commit

Permalink
Merge pull request #14 from calind/master
Browse files Browse the repository at this point in the history
Better error handling and bugfixes
  • Loading branch information
calind committed Feb 10, 2015
2 parents 99ebc7c + d2f3572 commit 765ed7d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='zipa',
version='0.1.3',
version='0.1.4',
platforms='any',
description='General purpose REST API client',
author='Vlad Temian & Calin Don',
Expand Down
70 changes: 51 additions & 19 deletions zipa/resource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json

import requests

from requests.exceptions import HTTPError

from .entity import Entity
from .utils import dict_merge

Expand All @@ -19,7 +20,8 @@ def __init__(self, url=None, name=None, params=None, config=None):
'secure': True,
'prefix': '/',
'serializer': 'json',
'verify': True
'verify': True,
'append_slash': False
}

config = dict_merge(_config_defaults, _config)
Expand All @@ -39,6 +41,9 @@ def _get_url(self):
if self.config.use_extensions:
url += '.json'

if self.config.append_slash and not url.endswith('/'):
url += '/'

return url

def _expand_url(self, part):
Expand All @@ -53,24 +58,55 @@ def _prepare_data(self, **kwargs):
else:
return kwargs

def _prepare_entity(self, response):
try:
json = response.json()
except:
json = {}

http_error_msg = ''

if 400 <= response.status_code < 500:
http_error_msg = '%s Client Error: %s' % (response.status_code,
response.reason)

elif 500 <= response.status_code < 600:
http_error_msg = '%s Server Error: %s' % (response.status_code,
response.reason)

if http_error_msg:
raise HTTPError(http_error_msg, json, response=response)

return Entity(json)

def create(self, **kwargs):
data = self._prepare_data(**kwargs)
response = requests.put(self.url, data=data,
auth=self.config['auth'],
verify=self.config['verify'])
return Entity(response.json())
headers = {'content-type': 'application/json'}
response = requests.post(self.url, data=data,
auth=self.config['auth'],
verify=self.config['verify'],
headers=headers)

entity = self._prepare_entity(response)
return entity

def update(self, **kwargs):
data = self._prepare_data(**kwargs)
response = requests.post(self.url, data=data,
auth=self.config['auth'],
verify=self.config['verify'])
return Entity(response.json())
headers = {'content-type': 'application/json'}
response = requests.put(self.url, data=data,
auth=self.config['auth'],
verify=self.config['verify'],
headers=headers)

entity = self._prepare_entity(response)
return entity

def delete(self, **kwargs):
requests.delete(self.url, params=kwargs,
auth=self.config['auth'],
verify=self.config['verify'])
response = requests.delete(self.url, params=kwargs,
auth=self.config['auth'],
verify=self.config['verify'])
entity = response._prepare_entity(response)
return entity

def __getattr__(self, name):
if name == 'url':
Expand Down Expand Up @@ -108,12 +144,8 @@ def __call__(self, **kwargs):
response = requests.get(self.url, params=kwargs,
auth=self.config['auth'],
verify=self.config['verify'])

content = response.json()
if isinstance(content, list):
return [Entity(item) for item in content]

return Entity(content)
entity = self._prepare_entity(response)
return entity

def __str__(self):
return repr(self)
Expand Down

0 comments on commit 765ed7d

Please sign in to comment.