Skip to content

Commit

Permalink
Merge 4eded15 into e6c4a13
Browse files Browse the repository at this point in the history
  • Loading branch information
caffodian committed Jun 27, 2014
2 parents e6c4a13 + 4eded15 commit 46e1e8b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# pycharm
.idea
1 change: 1 addition & 0 deletions buffpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .api import *
from .response import *
from .exceptions import *
20 changes: 20 additions & 0 deletions buffpy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rauth import OAuth2Session, OAuth2Service

from buffpy.response import ResponseObject
from buffpy.exceptions import BuffpyRestException


BASE_URL = 'https://api.bufferapp.com/1/%s'
Expand Down Expand Up @@ -43,6 +44,10 @@ def get(self, url, parser=None):

response = self.session.get(url=BASE_URL % url)

if not response.ok:
self._handleResponseError(url, response)


return parser(response.content)

def post(self, url, parser=None, **params):
Expand All @@ -56,8 +61,23 @@ def post(self, url, parser=None, **params):

response = self.session.post(url=BASE_URL % url, headers=headers, **params)

if not response.ok:
self._handleResponseError(url, response)

return parser(response.content)

def _handleResponseError(self, url, response):
http_code = response.status_code
try:
parsed = parser(response.content)
error_code = parsed['error_code']
description = parsed['message']
except:
error_code = None
description = response.content

raise BuffpyRestException(url, http_code, error_code, description)

@property
def info(self):
'''
Expand Down
40 changes: 40 additions & 0 deletions buffpy/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys

class BuffpyException(Exception):
pass


class BuffpyRestException(BuffpyException):
"""
Represents any 4xx or 5xx exceptions from the Buffer API
Reference: https://bufferapp.com/developers/api/errors
Some ideas borrowed from Twilio's REST exception handling
"""

def __init__(self, url, http_code, error_code=None, description=None, method='GET'):
self.url = url
self.http_code = http_code
self.error_code = error_code
self.description = description
self.method = method

def __str__(self):
"""
:return: if on TTY, a friendly string conversion for the object,
else a one liner indicating the error
"""

if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
msg = (
"\nHTTP Error - Your request was:\n\n{request}"
"\n\nBuffer returned the following error message:\n\nHTTP {http_code} error: {error_code} description: {description}\n".format(
request="%s %s" % (self.method, self.url),
http_code = self.http_code,
error_code = self.error_code,
description=self.description
))
return msg
else:
return "HTTP {} error: {} description: {}".format(self.http_code, self.error_code, self.description)
14 changes: 14 additions & 0 deletions buffpy/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from mock import MagicMock, patch

from buffpy.api import API
from buffpy.exceptions import *

import httpretty

def test_api_get_request():
'''
Expand Down Expand Up @@ -96,3 +99,14 @@ def test_api_info():
url = 'https://api.bufferapp.com/1/info/configuration.json'
mocked_session.get.assert_called_once_with(url=url)
eq_(info.status, 'ok')

@raises(BuffpyRestException)
@httpretty.activate
def test_api_post_parse_buffpy_error():

httpretty.register_uri(httpretty.POST, "https://api.bufferapp.com/1/hey",
body="{u'message': u\"Whoops, it looks like you've posted that one recently. Unfortunately, we're not able to post the same thing again so soon!\", u'code': 1025, u'success': False}",
status=400)

api = API(client_id='1', client_secret='2', access_token='access_token')
api.post(url='hey', data='new=True')
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
colorama==0.2.7
coverage==3.7.1
httpretty==0.8.3
mock==1.0.1
nose==1.3.0
rauth==0.6.2
requests==1.2.3
urllib3==1.7.1
wsgiref==0.1.2

0 comments on commit 46e1e8b

Please sign in to comment.