Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
try:
from setuptools import setup
from setuptools import setup
except ImportError, err:
from distutils.core import setup

Expand Down
28 changes: 18 additions & 10 deletions sharpy/client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import base64
import logging
from urllib import urlencode
from decimal import getcontext
from dateutil.tz import tzutc
import httplib2

from sharpy.exceptions import CheddarError, AccessDenied, BadRequest, NotFound, PreconditionFailed, CheddarFailure, NaughtyGateway, UnprocessableEntity
from sharpy.exceptions import AccessDenied
from sharpy.exceptions import BadRequest
from sharpy.exceptions import CheddarError
from sharpy.exceptions import CheddarFailure
from sharpy.exceptions import NaughtyGateway
from sharpy.exceptions import NotFound
from sharpy.exceptions import PreconditionFailed
from sharpy.exceptions import UnprocessableEntity

client_log = logging.getLogger('SharpyClient')


class Client(object):
default_endpoint = 'https://cheddargetter.com/xml'
def __init__(self, username, password, product_code, cache=None, timeout=None, endpoint=None):

def __init__(self, username, password, product_code, cache=None,
timeout=None, endpoint=None):
'''
username - Your cheddargetter username (probably an email address)
password - Your cheddargetter password
Expand Down Expand Up @@ -83,7 +92,8 @@ def make_request(self, path, params=None, data=None, method=None):
method = 'POST'
body = urlencode(data)
headers = {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'content-type':
'application/x-www-form-urlencoded; charset=UTF-8',
}

client_log.debug('Request Method: %s' % method)
Expand All @@ -92,10 +102,10 @@ def make_request(self, path, params=None, data=None, method=None):

# Setup http client
h = httplib2.Http(cache=self.cache, timeout=self.timeout)
#h.add_credentials(self.username, self.password)
# Skip the normal http client behavior and send auth headers immediately
# to save an http request.
headers['Authorization'] = "Basic %s" % base64.standard_b64encode(self.username + ':' + self.password).strip()
# Skip the normal http client behavior and send auth headers
# immediately to save an http request.
headers['Authorization'] = "Basic %s" % base64.standard_b64encode(
self.username + ':' + self.password).strip()

# Make request
response, content = h.request(url, method, body=body, headers=headers)
Expand Down Expand Up @@ -123,5 +133,3 @@ def make_request(self, path, params=None, data=None, method=None):

response.content = content
return response


28 changes: 18 additions & 10 deletions sharpy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

class CheddarError(Exception):
"Base class for exceptions returned by cheddar"

def __init__(self, response, content, *args, **kwargs):
# Importing in method to break circular dependecy
from sharpy.parsers import parse_error

super(CheddarError, self).__init__(*args, **kwargs)
error_info = parse_error(content)
self.response = response
self.error_info = error_info

def __str__(self):
return '%s (%s) %s - %s' % (
self.response.status,
Expand All @@ -20,42 +20,50 @@ def __str__(self):
self.error_info['message'],
)


class AccessDenied(CheddarError):
"A request to cheddar returned a status code of 401"
pass



class BadRequest(CheddarError):
"A request to cheddar was invalid in some way"
pass


class NotFound(CheddarError):
"A request to chedder was made for a resource which doesn't exist"
pass



class CheddarFailure(CheddarError):
"A request to cheddar encountered an unexpected error on the cheddar side"
pass



class PreconditionFailed(CheddarError):
"A request to cheddar was made but failed CG's validation in some way."
pass



class NaughtyGateway(CheddarError):
"""
Cheddar either couldn't contact the gateway or the gateway did something
very unexpected.
"""
pass



class UnprocessableEntity(CheddarError):
"""
An error occurred during processing. Please fix the error and try again.
"""
pass



class ParseError(Exception):
"""
Sharpy recieved unknown output from cheddar and doesn't know what
to do with it.
"""
pass
pass
Loading