Skip to content

Commit

Permalink
add linked in message type
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Owen committed Jul 22, 2014
1 parent 0784d17 commit d0c25b5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ nosetests.xml
.project
.pydevproject
.idea

.settings/org.eclipse.core.resources.prefs
29 changes: 19 additions & 10 deletions linkedin/linkedin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
import requests
import urllib
import random
import hashlib
import contextlib
import hashlib
import random
import urllib

import requests
from requests_oauthlib import OAuth1

from .models import AccessToken, LinkedInInvitation
from .utils import enum, to_utf8, raise_for_error, json, StringIO
from .exceptions import LinkedInError
from .models import AccessToken, LinkedInInvitation, LinkedInMessage
from .utils import enum, to_utf8, raise_for_error, json, StringIO


__all__ = ['LinkedInAuthentication', 'LinkedInApplication', 'PERMISSIONS']
Expand Down Expand Up @@ -102,7 +103,7 @@ def last_error(self):

def _make_new_state(self):
return hashlib.md5(
'%s%s' % (random.randrange(0, 2**63), self.secret)).hexdigest()
'%s%s' % (random.randrange(0, 2 ** 63), self.secret)).hexdigest()

def get_access_token(self, timeout=60):
assert self.authorization_code, 'You must first get the authorization code'
Expand Down Expand Up @@ -420,7 +421,7 @@ def submit_share(self, comment=None, title=None, description=None,
raise_for_error(response)
return response.json()

def get_network_updates(self, types, member_id=None,
def get_network_updates(self, types, member_id=None,
self_scope=True, params=None, headers=None):
if member_id:
url = '%s/id=%s/network/updates' % (ENDPOINTS.PEOPLE,
Expand All @@ -441,7 +442,7 @@ def get_network_updates(self, types, member_id=None,
raise_for_error(response)
return response.json()

def get_network_update(self, types, update_key,
def get_network_update(self, types, update_key,
self_scope=True, params=None, headers=None):
url = '%s/~/network/updates/key=%s' % (ENDPOINTS.PEOPLE, str(update_key))

Expand Down Expand Up @@ -472,6 +473,14 @@ def send_invitation(self, invitation):
raise_for_error(response)
return True

def send_message(self, message):
assert type(message) == LinkedInMessage, 'LinkedInInvitation required'
url = '%s/~/mailbox' % ENDPOINTS.PEOPLE
response = self.make_request('POST', url,
data=json.dumps(message.json))
raise_for_error(response)
return True

def comment_on_update(self, update_key, comment):
comment = {'comment': comment}
url = '%s/~/network/updates/key=%s/update-comments' % (ENDPOINTS.PEOPLE, update_key)
Expand All @@ -483,4 +492,4 @@ def like_update(self, update_key, is_liked=True):
url = '%s/~/network/updates/key=%s/is-liked' % (ENDPOINTS.PEOPLE, update_key)
response = self.make_request('PUT', url, data=json.dumps(is_liked))
raise_for_error(response)
return True
return True
30 changes: 30 additions & 0 deletions linkedin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ def json(self):
result['item-content']['invitation-request']['authorization'] = auth

return result


class LinkedInMessage(object):
def __init__(self, subject, body, recipients, auth_name=None,
auth_value=None):
self.subject = subject
self.body = body
self.recipients = recipients
self.auth_name = auth_name
self.auth_value = auth_value

@property
def json(self):
result = {
'recipients': {
'values': []
},
'subject': self.subject,
'body': self.body,
}
}
for recipient in self.recipients:
result['recipients']['values'].append(recipient.json)

if self.auth_name and self.auth_value:
auth = {'name': self.auth_name, 'value': self.auth_value}
result['item-content']['invitation-request']['authorization'] = auth

return result

0 comments on commit d0c25b5

Please sign in to comment.