From 7a08055b53629bf2b822e943fc11289ffa14c861 Mon Sep 17 00:00:00 2001 From: Christian Holm Date: Wed, 20 Mar 2013 12:56:36 +0100 Subject: [PATCH 1/3] Store the status and message in the exception for better handling --- linkedin.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/linkedin.py b/linkedin.py index eaf273f..950618b 100644 --- a/linkedin.py +++ b/linkedin.py @@ -27,7 +27,13 @@ raise ImportError('A json library is required to use this python library. Lol, yay for being verbose. ;)') -class LinkedinAPIError(Exception): pass +class LinkedinAPIError(Exception): + + def __init__(self, status, message): + super('Error Code: %d, Message: %s' % (status, message)) + self.status = status + self.message = message + class LinkedinAuthError(LinkedinAPIError): pass @@ -43,7 +49,7 @@ def __init__(self, api_key=None, api_secret=None, oauth_token=None, oauth_token_ # Authentication URLs self.request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken' self.access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken' - # Authentication page in http://developer.linkedin.com/documents/authentication states that + # Authentication page in http://developer.linkedin.com/documents/authentication states that # endpoint is the following not the previous url used. self.authorize_url = 'https://api.linkedin.com/uas/oauth/authenticate' @@ -107,7 +113,7 @@ def get_authentication_tokens(self): status = int(resp['status']) if status != 200: - raise LinkedinAuthError('There was a problem authenticating you. Error: %s, Message: %s' % (status, content)) + raise LinkedinAuthError(status, content) request_tokens = dict(parse_qsl(content)) @@ -150,14 +156,15 @@ def api_request(self, endpoint, method='GET', fields='', params={}): else: resp, content = self.client.request('%s?%s' % (url, urllib.urlencode(params)), 'GET', headers=self.headers) + status = int(resp['status']) + try: content = json.loads(content) except json.JSONDecodeError: - raise LinkedinAPIError('Content is not valid JSON, unable to be decoded.') + raise LinkedinAPIError(status, 'Content is not valid JSON, unable to be decoded.') - status = int(resp['status']) if status < 200 or status >= 300: - raise LinkedinAPIError('Error Code: %d, Message: %s' % (status, content['message'])) + raise LinkedinAPIError(status, content['message']) return content From 22e2a38124cd699ed7dcd9249919eea8220c00e3 Mon Sep 17 00:00:00 2001 From: Christian Holm Date: Wed, 20 Mar 2013 13:30:20 +0100 Subject: [PATCH 2/3] Clean up the request --- linkedin.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/linkedin.py b/linkedin.py index 950618b..8672b6a 100644 --- a/linkedin.py +++ b/linkedin.py @@ -30,7 +30,7 @@ class LinkedinAPIError(Exception): def __init__(self, status, message): - super('Error Code: %d, Message: %s' % (status, message)) + super(LinkedinAPIError, self).__init__('Error Code: %d, Message: %s' % (status, message)) self.status = status self.message = message @@ -143,25 +143,21 @@ def api_request(self, endpoint, method='GET', fields='', params={}): if fields: url = '%s:(%s)' % (url, fields) - if method == 'POST': - resp, content = self.client.request(url, 'POST', body=json.dumps(params), headers=self.headers) - - # As far as I've seen, all POSTs return a 201 and NO body -.- - # So, we'll just return true if it's a post and returns 201 - - # This will catch a successful post, but continue and throw - # an error if it wasn't successful. - if 'status' in resp and int(resp['status']) == 201: - return True + if method == "GET": + url = '%s?%s' % (url, urllib.urlencode(params)) + body = None else: - resp, content = self.client.request('%s?%s' % (url, urllib.urlencode(params)), 'GET', headers=self.headers) + body = json.dumps(params) + + resp, content = self.client.request(url, method, body=body, headers=self.headers) status = int(resp['status']) - try: - content = json.loads(content) - except json.JSONDecodeError: - raise LinkedinAPIError(status, 'Content is not valid JSON, unable to be decoded.') + if content is not None: + try: + content = json.loads(content) + except json.JSONDecodeError: + raise LinkedinAPIError(status, 'Content is not valid JSON, unable to be decoded.') if status < 200 or status >= 300: raise LinkedinAPIError(status, content['message']) From b33c4f7ce0cef2653e61e177c5a77f43c17c50db Mon Sep 17 00:00:00 2001 From: Christian Holm Date: Mon, 25 Mar 2013 12:42:14 +0100 Subject: [PATCH 3/3] Don't try to decode empty content --- linkedin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linkedin.py b/linkedin.py index 8672b6a..db04298 100644 --- a/linkedin.py +++ b/linkedin.py @@ -153,7 +153,7 @@ def api_request(self, endpoint, method='GET', fields='', params={}): status = int(resp['status']) - if content is not None: + if content: try: content = json.loads(content) except json.JSONDecodeError: