<s> __author__ = '<STR_LIT>' <EOL> import hashlib <EOL> import random <EOL> import time <EOL> import urlparse <EOL> from types import ListType <EOL> import data <EOL> import http <EOL> from opensocial import simplejson <EOL> def generate_uuid ( * args ) : <EOL> """<STR_LIT>""" <EOL> t = long ( time . time ( ) * <NUM_LIT:1000> ) <EOL> r = long ( random . random ( ) * <NUM_LIT> L ) <EOL> a = random . random ( ) * <NUM_LIT> L <EOL> data = '<STR_LIT>' % ( str ( t ) , str ( r ) , str ( a ) , str ( args ) ) <EOL> return hashlib . md5 ( data ) . hexdigest ( ) <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rest_request , rpc_request , requestor = None ) : <EOL> self . rest_request = rest_request <EOL> self . rpc_request = rpc_request <EOL> self . set_requestor ( requestor ) <EOL> def get_requestor ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __requestor <EOL> def set_requestor ( self , id ) : <EOL> """<STR_LIT>""" <EOL> if id and id [ <NUM_LIT:0> ] is not '<STR_LIT:@>' : <EOL> self . __requestor = id <EOL> else : <EOL> self . __requestor = None <EOL> def get_query_params ( self ) : <EOL> """<STR_LIT>""" <EOL> query_params = { } <EOL> if self . get_requestor ( ) : <EOL> query_params [ '<STR_LIT>' ] = self . get_requestor ( ) <EOL> return query_params <EOL> def make_rest_request ( self , url_base ) : <EOL> """<STR_LIT>""" <EOL> return self . rest_request . make_http_request ( url_base , <EOL> self . get_query_params ( ) ) <EOL> def get_rpc_body ( self ) : <EOL> return self . rpc_request . get_rpc_body ( ) <EOL> class FetchSupportedFields ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , osobject ) : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( osobject , '<STR_LIT>' ) ) , '<STR_LIT:GET>' ) <EOL> super ( FetchSupportedFields , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return json <EOL> class FetchGroupRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , params = None ) : <EOL> params = params or { } <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchGroupRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return data . Collection . parse_json ( json , data . Group ) <EOL> class FetchAlbumRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , albumid = None , params = None ) : <EOL> params = params or { } <EOL> if albumid != None : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' , albumid ) ) , '<STR_LIT:GET>' , params ) <EOL> else : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchAlbumRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> json_list = json . get ( '<STR_LIT>' ) <EOL> if json_list != None : <EOL> """<STR_LIT>""" <EOL> return data . Album ( json_list ) <EOL> return data . Collection . parse_json ( json , data . Album ) <EOL> class FetchMediaItemsRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , albumid = None , mediaitemid = None , params = None ) : <EOL> params = params or { } <EOL> if mediaitemid != None : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' , albumid , mediaitemid ) ) , '<STR_LIT:GET>' , params ) <EOL> else : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' , albumid ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchMediaItemsRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> json_list = json . get ( '<STR_LIT>' ) <EOL> if json_list != None : <EOL> """<STR_LIT>""" <EOL> return data . MediaItem ( json_list ) <EOL> return data . Collection . parse_json ( json , data . MediaItem ) <EOL> class FetchStatusMoodRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , params = None ) : <EOL> params = params or { } <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchStatusMoodRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return data . StatusMood ( json ) <EOL> class FetchStatusMoodCommentsRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , params = None ) : <EOL> params = params or { } <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchStatusMoodCommentsRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return data . Collection . parse_json ( json , data . StatusMoodComments ) <EOL> class FetchProfileCommentsRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , params = None ) : <EOL> params = params or { } <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchProfileCommentsRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return data . Collection . parse_json ( json , data . ProfileComments ) <EOL> class FetchPeopleRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , group_id , fields = None , params = None ) : <EOL> params = params or { } <EOL> if fields : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( fields ) <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , group_id ) ) , <EOL> params = params ) <EOL> rpc_params = params . copy ( ) <EOL> rpc_params . update ( { '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : group_id } ) <EOL> rpc_request = RpcRequestInfo ( '<STR_LIT>' , params = rpc_params ) <EOL> super ( FetchPeopleRequest , self ) . __init__ ( rest_request , <EOL> rpc_request , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return data . Collection . parse_json ( json , data . Person ) <EOL> class FetchPersonRequest ( FetchPeopleRequest ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , fields = None , params = { } ) : <EOL> super ( FetchPersonRequest , self ) . __init__ ( user_id , <EOL> '<STR_LIT>' , <EOL> fields = fields , <EOL> params = params ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> return data . Person . parse_json ( json ) <EOL> class FetchAppDataRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , group_id , app_id = '<STR_LIT>' , fields = None , <EOL> params = None ) : <EOL> params = params or { } <EOL> if fields : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( fields ) <EOL> rest_path = '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , group_id , app_id ) ) <EOL> rest_request = RestRequestInfo ( rest_path , params = params ) <EOL> params . update ( { '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : group_id , <EOL> '<STR_LIT>' : app_id , <EOL> '<STR_LIT>' : fields } ) <EOL> rpc_request = RpcRequestInfo ( '<STR_LIT>' , params = params ) <EOL> super ( FetchAppDataRequest , self ) . __init__ ( rest_request , <EOL> rpc_request , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> """<STR_LIT>""" <EOL> if type ( json ) == ListType : <EOL> return json <EOL> else : <EOL> return data . AppData . parse_json ( json ) <EOL> class UpdateAppDataRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , group_id , app_id = '<STR_LIT>' , fields = None , data = { } , <EOL> params = None ) : <EOL> params = params or { } <EOL> if fields : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( fields ) <EOL> else : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( data . keys ( ) ) <EOL> params [ '<STR_LIT:data>' ] = data <EOL> params . update ( { '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : group_id , <EOL> '<STR_LIT>' : app_id } ) <EOL> rpc_request = RpcRequestInfo ( '<STR_LIT>' , params = params ) <EOL> super ( UpdateAppDataRequest , self ) . __init__ ( None , <EOL> rpc_request , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> return json <EOL> class DeleteAppDataRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , group_id , app_id = '<STR_LIT>' , fields = None , <EOL> params = None ) : <EOL> params = params or { } <EOL> if fields : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( fields ) <EOL> params . update ( { '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : group_id , <EOL> '<STR_LIT>' : app_id , <EOL> '<STR_LIT>' : params [ '<STR_LIT>' ] } ) <EOL> rpc_request = RpcRequestInfo ( '<STR_LIT>' , params = params ) <EOL> super ( DeleteAppDataRequest , self ) . __init__ ( None , <EOL> rpc_request , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> return json <EOL> class CreateNotificationRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , recipients , mediaitems = None , templateParameters = None , <EOL> params = None ) : <EOL> params = params or { } <EOL> bodycontent = { } <EOL> if mediaitems != None : <EOL> itemlist = [ ] <EOL> for mediaitem in mediaitems : <EOL> itemdict = { } <EOL> itemdict [ '<STR_LIT>' ] = mediaitem ; <EOL> itemlist . append ( itemdict ) ; <EOL> bodycontent [ '<STR_LIT>' ] = itemlist <EOL> bodycontent [ '<STR_LIT>' ] = recipients <EOL> bodycontent [ '<STR_LIT>' ] = templateParameters <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:POST>' , params , bodycontent , False ) <EOL> super ( CreateNotificationRequest , self ) . __init__ ( rest_request , <EOL> None , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> return json <EOL> class CreateAlbumRequest ( Request ) : <EOL> def __init__ ( self , user_id , body , params = None ) : <EOL> params = params or { } <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:POST>' , params , body , False ) <EOL> super ( CreateAlbumRequest , self ) . __init__ ( rest_request , None , user_id ) <EOL> pass <EOL> def process_json ( self , json ) : <EOL> return json <EOL> class CreateActivityRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , activity , group_id = '<STR_LIT>' , app_id = '<STR_LIT>' , <EOL> params = None ) : <EOL> params = params or { } <EOL> params [ '<STR_LIT>' ] = activity <EOL> params . update ( { '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : group_id , <EOL> '<STR_LIT>' : app_id } ) <EOL> rpc_request = RpcRequestInfo ( '<STR_LIT>' , params = params ) <EOL> super ( CreateActivityRequest , self ) . __init__ ( None , <EOL> rpc_request , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> return json <EOL> class FetchActivityRequest ( Request ) : <EOL> def __init__ ( self , user_id , group_id = '<STR_LIT>' , app_id = None , params = None ) : <EOL> params = params or { } <EOL> params . update ( { '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : group_id , <EOL> '<STR_LIT>' : app_id , <EOL> } ) <EOL> rpc_request = RpcRequestInfo ( '<STR_LIT>' , params = params ) <EOL> if app_id != None : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' , app_id ) ) , '<STR_LIT:GET>' , params ) <EOL> else : <EOL> rest_request = RestRequestInfo ( '<STR_LIT:/>' . join ( ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) , '<STR_LIT:GET>' , params ) <EOL> super ( FetchActivityRequest , self ) . __init__ ( rest_request , <EOL> rpc_request , <EOL> user_id ) <EOL> def process_json ( self , json ) : <EOL> return data . Collection . parse_json ( json , data . Activity ) <EOL> class RestRequestInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , method = '<STR_LIT:GET>' , params = None , body = None , body_hash = True ) : <EOL> self . method = method <EOL> self . path = path <EOL> self . params = params or { } <EOL> self . body = body <EOL> self . body_hash = body_hash <EOL> def make_http_request ( self , url_base , query_params = None ) : <EOL> """<STR_LIT>""" <EOL> if url_base [ - <NUM_LIT:1> ] is not '<STR_LIT:/>' and self . path [ <NUM_LIT:0> ] is not '<STR_LIT:/>' : <EOL> url_base = url_base + '<STR_LIT:/>' <EOL> url = url_base + self . path <EOL> if query_params : <EOL> self . params . update ( query_params ) <EOL> return http . Request ( url , method = self . method , signed_params = self . params , post_body = self . body , add_bodyhash = self . body_hash ) <EOL> class TextRpcRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rpc_body , requestor = None ) : <EOL> self . __rpc_body = rpc_body ; <EOL> self . set_requestor ( requestor ) <EOL> def get_rpc_body ( self ) : <EOL> return simplejson . loads ( self . __rpc_body ) <EOL> def get_requestor ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __requestor <EOL> def set_requestor ( self , id ) : <EOL> """<STR_LIT>""" <EOL> if id and id [ <NUM_LIT:0> ] is not '<STR_LIT:@>' : <EOL> self . __requestor = id <EOL> else : <EOL> self . __requestor = None <EOL> def process_json ( self , json ) : <EOL> return json <EOL> class RpcRequestInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method , params , id = None ) : <EOL> self . method = method <EOL> self . params = params <EOL> self . id = id or generate_uuid ( method ) <EOL> def get_rpc_body ( self ) : <EOL> """<STR_LIT>""" <EOL> rpc_body = { <EOL> '<STR_LIT>' : self . params , <EOL> '<STR_LIT>' : self . method , <EOL> '<STR_LIT:id>' : self . id , <EOL> } <EOL> return rpc_body <EOL> class RequestBatch ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . requests = { } <EOL> self . data = { } <EOL> def add_request ( self , key , request ) : <EOL> """<STR_LIT>""" <EOL> if key : <EOL> request . rpc_request . id = key <EOL> self . requests [ key ] = request <EOL> def get ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . data . get ( key ) <EOL> def send ( self , container ) : <EOL> """<STR_LIT>""" <EOL> container . send_request_batch ( self , False ) <EOL> def _set_data ( self , key , data ) : <EOL> self . data [ key ] = data </s>
