Skip to content

Commit

Permalink
Bump version, PEP8 Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhelmick committed Feb 2, 2012
1 parent 3384527 commit 153d766
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
52 changes: 26 additions & 26 deletions linkedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@
""" LinkedIn """

__author__ = 'Mike Helmick <mikehelmick@me.com>'
__version__ = '0.1.1'
__version__ = '0.1.2'

import urllib
import urllib2
import urlparse
import httplib
import httplib2
import inspect
import time

try:
from urlparse import parse_qsl
except ImportError:
from cgi import parse_qsl

import oauth2 as oauth

from urllib2 import HTTPError
import httplib2

try:
import simplejson as json
Expand All @@ -33,11 +26,13 @@
except ImportError:
raise ImportError('A json library is required to use this python library. Lol, yay for being verbose. ;)')


class LinkedinAPIError(Exception): pass
class LinkedinAuthError(LinkedinAPIError): pass


class LinkedinAPI(object):
def __init__(self, api_key=None, api_secret=None, oauth_token=None, oauth_token_secret=None, headers=None, client_args={}, callback_url=None):
def __init__(self, api_key=None, api_secret=None, oauth_token=None, oauth_token_secret=None, headers=None, client_args=None, callback_url=None):
self.api_key = api_key
self.api_secret = api_secret
self.oauth_token = oauth_token
Expand All @@ -56,25 +51,28 @@ def __init__(self, api_key=None, api_secret=None, oauth_token=None, oauth_token_
self.api_version = 'v1'
self.api_url = '%s/%s/' % (self.api_base, self.api_version)

# If there's headers, set them. If not, lets
# If there's headers, set them. If not, lets
self.headers = headers
if self.headers is None:
self.headers = {'User-agent': 'Linkedin %s' % __version__}

# ALL requests will be json. Enforcing it here...
self.headers.update({'x-li-format': 'json', 'Content-Type':'application/json'})
self.headers.update({'x-li-format': 'json',
'Content-Type': 'application/json'})

self.consumer = None
self.token = None

client_args = client_args or {}

# See if they're authenticating for the first or if they already have some tokens.
# http://michaelhelmick.com/tokens.jpg
if self.api_key is not None and self.api_secret is not None:
self.consumer = oauth.Consumer(key=self.api_key, secret=self.api_secret)

if self.oauth_token is not None and self.oauth_secret is not None:
self.token = oauth.Token(key=oauth_token, secret=oauth_token_secret)

if self.consumer is not None and self.token is not None:
# Authenticated
self.client = oauth.Client(self.consumer, self.token, **client_args)
Expand All @@ -99,15 +97,15 @@ 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))

request_tokens = dict(parse_qsl(content))

auth_url_params = {
'oauth_token' : request_tokens['oauth_token'],
'oauth_token': request_tokens['oauth_token'],
}

request_tokens['auth_url'] = self.authorize_url + '?' + urllib.urlencode(auth_url_params)

return request_tokens

def get_access_token(self, oauth_verifier):
Expand All @@ -119,41 +117,43 @@ def get_access_token(self, oauth_verifier):
oauth_token_secret = authorized_tokens['oauth_token_secret']
"""

resp, content = self.client.request(self.access_token_url+'?oauth_verifier=%s' % oauth_verifier, 'GET')
resp, content = self.client.request('%s?oauth_verifier=%s' % (self.access_token_url, oauth_verifier), 'GET')
return dict(parse_qsl(content))

def api_request(self, endpoint, method='GET', fields='', params={}):
url = self.api_url+endpoint
url = self.api_url + endpoint

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
# 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
else:
resp, content = self.client.request('%s?%s' % (url, urllib.urlencode(params)), 'GET', headers=self.headers)

try:
content = json.loads(content)
except json.JSONDecodeError:
raise LinkedinAPIError('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']))

return content

def get(self, endpoint, fields='', params={}):
def get(self, endpoint, fields='', params=None):
params = params or {}
return self.api_request(endpoint, fields=fields, params=params)

def post(self, endpoint, fields='', params={}):
return self.api_request(endpoint, method='POST', fields=fields, params=params)
def post(self, endpoint, fields='', params=None):
params = params or {}
return self.api_request(endpoint, method='POST', fields=fields, params=params)
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='linkedin',
version='0.1.1',
version='0.1.2',
install_requires=['httplib2', 'oauth2', 'simplejson'],
author='Mike Helmick',
author_email='mikehelmick@me.com',
Expand All @@ -15,12 +15,12 @@
long_description=open('README.md').read(),
download_url="https://github.com/michaelhelmick/linkedin/zipball/master",
py_modules=["linkedin"],
classifiers = [
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Communications :: Chat',
'Topic :: Internet'
]
)
)

0 comments on commit 153d766

Please sign in to comment.