<s> import json <EOL> import urllib <EOL> import requests <EOL> import types <EOL> class MovesAPIError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MovesAPINotModifed ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MovesClient ( object ) : <EOL> """<STR_LIT>""" <EOL> api_url = "<STR_LIT>" <EOL> app_auth_url = "<STR_LIT>" <EOL> web_auth_uri = "<STR_LIT>" <EOL> token_url = "<STR_LIT>" <EOL> tokeninfo_url = "<STR_LIT>" <EOL> refresh_url = "<STR_LIT>" <EOL> def __init__ ( self , client_id = None , client_secret = None , <EOL> access_token = None , use_app = False ) : <EOL> self . client_id = client_id <EOL> self . client_secret = client_secret <EOL> self . access_token = access_token <EOL> self . auth_url = self . app_auth_url if use_app else self . web_auth_uri <EOL> self . use_app = use_app <EOL> self . _last_headers = None <EOL> def parse_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( response . text ) <EOL> def build_oauth_url ( self , redirect_uri = None , scope = "<STR_LIT>" ) : <EOL> params = { <EOL> '<STR_LIT>' : self . client_id , <EOL> '<STR_LIT>' : scope <EOL> } <EOL> if not self . use_app : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:code>' <EOL> if redirect_uri : <EOL> params [ '<STR_LIT>' ] = redirect_uri <EOL> encoded = urllib . urlencode ( params ) . replace ( '<STR_LIT:+>' , '<STR_LIT>' ) <EOL> return "<STR_LIT>" % ( self . auth_url , encoded ) <EOL> def get_oauth_token ( self , code , ** kwargs ) : <EOL> params = { <EOL> '<STR_LIT>' : self . client_id , <EOL> '<STR_LIT>' : self . client_secret , <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> if '<STR_LIT>' in kwargs : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> response = requests . post ( self . token_url , params = params ) <EOL> response = json . loads ( response . content ) <EOL> try : <EOL> return response [ '<STR_LIT>' ] , response [ '<STR_LIT>' ] <EOL> except : <EOL> error = "<STR_LIT>" % response <EOL> raise MovesAPIError ( error ) <EOL> def refresh_oauth_token ( self , refresh_token , ** kwargs ) : <EOL> params = { <EOL> '<STR_LIT>' : self . client_id , <EOL> '<STR_LIT>' : self . client_secret , <EOL> '<STR_LIT>' : refresh_token , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> response = requests . post ( self . refresh_url , params = params ) <EOL> response = json . loads ( response . content ) <EOL> try : <EOL> return response [ '<STR_LIT>' ] , response [ '<STR_LIT>' ] <EOL> except : <EOL> error = "<STR_LIT>" % response <EOL> raise MovesAPIError ( error ) <EOL> def tokeninfo ( self ) : <EOL> params = { <EOL> '<STR_LIT>' : self . access_token <EOL> } <EOL> response = requests . get ( self . tokeninfo_url , params = params ) <EOL> response = json . loads ( response . content ) <EOL> try : <EOL> return response <EOL> except : <EOL> error = "<STR_LIT>" % response <EOL> raise MovesAPIError ( error ) <EOL> def api ( self , path , method = '<STR_LIT:GET>' , ** kwargs ) : <EOL> params = kwargs [ '<STR_LIT>' ] if '<STR_LIT>' in kwargs else { } <EOL> data = kwargs [ '<STR_LIT:data>' ] if '<STR_LIT:data>' in kwargs else { } <EOL> if not self . access_token and '<STR_LIT>' not in params : <EOL> raise MovesAPIError ( "<STR_LIT>" ) <EOL> url = "<STR_LIT>" % ( self . api_url , path ) <EOL> if '<STR_LIT>' in params : <EOL> access_token = params [ '<STR_LIT>' ] <EOL> del ( params [ '<STR_LIT>' ] ) <EOL> else : <EOL> access_token = self . access_token <EOL> headers = { <EOL> "<STR_LIT>" : '<STR_LIT>' + access_token <EOL> } <EOL> if '<STR_LIT>' in params : <EOL> headers [ '<STR_LIT>' ] = params [ '<STR_LIT>' ] <EOL> del ( params [ '<STR_LIT>' ] ) <EOL> resp = requests . request ( method , url , <EOL> data = data , <EOL> params = params , <EOL> headers = headers ) <EOL> if str ( resp . status_code ) [ <NUM_LIT:0> ] not in ( '<STR_LIT:2>' , '<STR_LIT:3>' ) : <EOL> raise MovesAPIError ( "<STR_LIT>" % <EOL> resp . status_code , resp . text ) <EOL> if resp . status_code == <NUM_LIT> : <EOL> raise MovesAPINotModifed ( "<STR_LIT>" ) <EOL> self . _last_headers = resp . headers <EOL> return resp <EOL> def get ( self , path , ** params ) : <EOL> return self . parse_response ( <EOL> self . api ( path , '<STR_LIT:GET>' , params = params ) ) <EOL> def post ( self , path , ** data ) : <EOL> return self . parse_response ( <EOL> self . api ( path , '<STR_LIT:POST>' , data = data ) ) <EOL> def set_first_date ( self ) : <EOL> if not self . first_date : <EOL> response = self . user_profile ( ) <EOL> self . first_date = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def __getattr__ ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> base_path = name . replace ( '<STR_LIT:_>' , '<STR_LIT:/>' ) <EOL> def closure ( * path , ** params ) : <EOL> '<STR_LIT>' <EOL> path = list ( path ) <EOL> path . insert ( <NUM_LIT:0> , base_path ) <EOL> return self . parse_response ( <EOL> self . api ( '<STR_LIT:/>' . join ( path ) , '<STR_LIT:GET>' , params = params ) <EOL> ) <EOL> retval = types . FunctionType ( <EOL> closure . func_code , <EOL> closure . func_globals , <EOL> name , <EOL> closure . func_defaults , <EOL> closure . func_closure ) <EOL> retval . func_doc = closure . func_doc % base_path <EOL> setattr ( self , name , retval ) <EOL> return retval <EOL> _move_client_status = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> for att in _move_client_status : <EOL> att = att . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> setattr ( MovesClient , att , property ( lambda self , att = att : self . _last_headers . get ( att , None ) <EOL> if self . _last_headers else att ) ) </s>
