diff --git a/linkedin.py b/linkedin.py index eaf273f..db04298 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(LinkedinAPIError, self).__init__('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)) @@ -137,27 +143,24 @@ 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) - try: - content = json.loads(content) - except json.JSONDecodeError: - raise LinkedinAPIError('Content is not valid JSON, unable to be decoded.') + resp, content = self.client.request(url, method, body=body, headers=self.headers) status = int(resp['status']) + + if content: + 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('Error Code: %d, Message: %s' % (status, content['message'])) + raise LinkedinAPIError(status, content['message']) return content