Skip to content

Commit d0c25b5

Browse files
author
Josh Owen
committed
add linked in message type
1 parent 0784d17 commit d0c25b5

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ nosetests.xml
3434
.project
3535
.pydevproject
3636
.idea
37+
38+
.settings/org.eclipse.core.resources.prefs

linkedin/linkedin.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
import requests
3-
import urllib
4-
import random
5-
import hashlib
62
import contextlib
3+
import hashlib
4+
import random
5+
import urllib
6+
7+
import requests
78
from requests_oauthlib import OAuth1
89

9-
from .models import AccessToken, LinkedInInvitation
10-
from .utils import enum, to_utf8, raise_for_error, json, StringIO
1110
from .exceptions import LinkedInError
11+
from .models import AccessToken, LinkedInInvitation, LinkedInMessage
12+
from .utils import enum, to_utf8, raise_for_error, json, StringIO
1213

1314

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

103104
def _make_new_state(self):
104105
return hashlib.md5(
105-
'%s%s' % (random.randrange(0, 2**63), self.secret)).hexdigest()
106+
'%s%s' % (random.randrange(0, 2 ** 63), self.secret)).hexdigest()
106107

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

423-
def get_network_updates(self, types, member_id=None,
424+
def get_network_updates(self, types, member_id=None,
424425
self_scope=True, params=None, headers=None):
425426
if member_id:
426427
url = '%s/id=%s/network/updates' % (ENDPOINTS.PEOPLE,
@@ -441,7 +442,7 @@ def get_network_updates(self, types, member_id=None,
441442
raise_for_error(response)
442443
return response.json()
443444

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

@@ -472,6 +473,14 @@ def send_invitation(self, invitation):
472473
raise_for_error(response)
473474
return True
474475

476+
def send_message(self, message):
477+
assert type(message) == LinkedInMessage, 'LinkedInInvitation required'
478+
url = '%s/~/mailbox' % ENDPOINTS.PEOPLE
479+
response = self.make_request('POST', url,
480+
data=json.dumps(message.json))
481+
raise_for_error(response)
482+
return True
483+
475484
def comment_on_update(self, update_key, comment):
476485
comment = {'comment': comment}
477486
url = '%s/~/network/updates/key=%s/update-comments' % (ENDPOINTS.PEOPLE, update_key)
@@ -483,4 +492,4 @@ def like_update(self, update_key, is_liked=True):
483492
url = '%s/~/network/updates/key=%s/is-liked' % (ENDPOINTS.PEOPLE, update_key)
484493
response = self.make_request('PUT', url, data=json.dumps(is_liked))
485494
raise_for_error(response)
486-
return True
495+
return True

linkedin/models.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,33 @@ def json(self):
6464
result['item-content']['invitation-request']['authorization'] = auth
6565

6666
return result
67+
68+
69+
class LinkedInMessage(object):
70+
def __init__(self, subject, body, recipients, auth_name=None,
71+
auth_value=None):
72+
self.subject = subject
73+
self.body = body
74+
self.recipients = recipients
75+
self.auth_name = auth_name
76+
self.auth_value = auth_value
77+
78+
@property
79+
def json(self):
80+
result = {
81+
'recipients': {
82+
'values': []
83+
},
84+
'subject': self.subject,
85+
'body': self.body,
86+
}
87+
}
88+
for recipient in self.recipients:
89+
result['recipients']['values'].append(recipient.json)
90+
91+
if self.auth_name and self.auth_value:
92+
auth = {'name': self.auth_name, 'value': self.auth_value}
93+
result['item-content']['invitation-request']['authorization'] = auth
94+
95+
return result
96+

0 commit comments

Comments
 (0)