11# -*- coding: utf-8 -*- 
2- import  requests 
3- import  urllib 
4- import  random 
5- import  hashlib 
62import  contextlib 
3+ import  hashlib 
4+ import  random 
5+ import  urllib 
6+ 
7+ import  requests 
78from  requests_oauthlib  import  OAuth1 
89
9- from  .models  import  AccessToken , LinkedInInvitation 
10- from  .utils  import  enum , to_utf8 , raise_for_error , json , StringIO 
1110from  .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 
0 commit comments