<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import re <EOL> import urllib <EOL> import urllib2 <EOL> import httplib <EOL> import json <EOL> import datetime <EOL> from models import * <EOL> class ApiClient : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , apiKey = None , apiServer = None ) : <EOL> if apiKey == None : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . apiKey = apiKey <EOL> self . apiServer = apiServer <EOL> self . cookie = None <EOL> def callAPI ( self , resourcePath , method , queryParams , postData , <EOL> headerParams = None ) : <EOL> url = self . apiServer + resourcePath <EOL> headers = { } <EOL> if headerParams : <EOL> for param , value in headerParams . iteritems ( ) : <EOL> headers [ param ] = value <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> headers [ '<STR_LIT>' ] = self . apiKey <EOL> if self . cookie : <EOL> headers [ '<STR_LIT>' ] = self . cookie <EOL> data = None <EOL> if method == '<STR_LIT:GET>' : <EOL> if queryParams : <EOL> sentQueryParams = { } <EOL> for param , value in queryParams . items ( ) : <EOL> if value != None : <EOL> sentQueryParams [ param ] = value <EOL> url = url + '<STR_LIT:?>' + urllib . urlencode ( sentQueryParams ) <EOL> elif method in [ '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if postData : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> data = self . sanitizeForSerialization ( postData ) <EOL> data = json . dumps ( data ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + method + '<STR_LIT>' ) <EOL> request = MethodRequest ( method = method , url = url , headers = headers , <EOL> data = data ) <EOL> response = urllib2 . urlopen ( request ) <EOL> if '<STR_LIT>' in response . headers : <EOL> self . cookie = response . headers [ '<STR_LIT>' ] <EOL> string = response . read ( ) <EOL> try : <EOL> data = json . loads ( string ) <EOL> except ValueError : <EOL> data = None <EOL> return data <EOL> def toPathValue ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) == list : <EOL> return urllib . quote ( '<STR_LIT:U+002C>' . join ( obj ) ) <EOL> elif type ( obj ) == unicode : <EOL> return urllib . quote ( obj . encode ( '<STR_LIT:utf8>' ) ) <EOL> else : <EOL> return urllib . quote ( str ( obj ) ) <EOL> def sanitizeForSerialization ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if not obj : <EOL> return None <EOL> elif type ( obj ) in [ str , int , long , float , bool , unicode ] : <EOL> return obj <EOL> elif type ( obj ) == unicode : <EOL> return obj . encode ( '<STR_LIT:utf8>' ) <EOL> elif type ( obj ) == list : <EOL> return [ self . sanitizeForSerialization ( subObj ) for subObj in obj ] <EOL> elif type ( obj ) == datetime . datetime : <EOL> return obj . isoformat ( ) <EOL> else : <EOL> if type ( obj ) == dict : <EOL> objDict = obj <EOL> else : <EOL> objDict = obj . __dict__ <EOL> return { key : self . sanitizeForSerialization ( val ) <EOL> for ( key , val ) in objDict . iteritems ( ) <EOL> if key != '<STR_LIT>' } <EOL> if type ( postData ) == list : <EOL> if type ( postData [ <NUM_LIT:0> ] ) in safeToDump : <EOL> data = json . dumps ( postData ) <EOL> else : <EOL> data = json . dumps ( [ datum . __dict__ for datum in postData ] ) <EOL> elif type ( postData ) not in safeToDump : <EOL> data = json . dumps ( postData . __dict__ ) <EOL> def deserialize ( self , obj , objClass ) : <EOL> """<STR_LIT>""" <EOL> if type ( objClass ) == str : <EOL> if '<STR_LIT>' in objClass : <EOL> match = re . match ( '<STR_LIT>' , objClass ) <EOL> subClass = match . group ( <NUM_LIT:1> ) <EOL> return [ self . deserialize ( subObj , subClass ) for subObj in obj ] <EOL> if ( objClass in [ '<STR_LIT:int>' , '<STR_LIT:float>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:list>' , '<STR_LIT:str>' , '<STR_LIT:bool>' , '<STR_LIT>' ] ) : <EOL> objClass = eval ( objClass ) <EOL> else : <EOL> objClass = eval ( objClass + '<STR_LIT:.>' + objClass ) <EOL> if objClass == str : <EOL> return obj <EOL> elif objClass in [ int , long , float , dict , list , bool ] : <EOL> return objClass ( obj ) <EOL> elif objClass == datetime : <EOL> return datetime . datetime . strptime ( obj [ : - <NUM_LIT:5> ] , <EOL> "<STR_LIT>" ) <EOL> instance = objClass ( ) <EOL> for attr , attrType in instance . swaggerTypes . iteritems ( ) : <EOL> if attr in obj : <EOL> value = obj [ attr ] <EOL> if attrType in [ '<STR_LIT:str>' , '<STR_LIT:int>' , '<STR_LIT>' , '<STR_LIT:float>' , '<STR_LIT:bool>' ] : <EOL> attrType = eval ( attrType ) <EOL> try : <EOL> value = attrType ( value ) <EOL> except UnicodeEncodeError : <EOL> value = unicode ( value ) <EOL> setattr ( instance , attr , value ) <EOL> elif ( attrType == '<STR_LIT>' ) : <EOL> setattr ( instance , attr , datetime . datetime . strptime ( value [ : - <NUM_LIT:5> ] , <EOL> "<STR_LIT>" ) ) <EOL> elif '<STR_LIT>' in attrType : <EOL> match = re . match ( '<STR_LIT>' , attrType ) <EOL> subClass = match . group ( <NUM_LIT:1> ) <EOL> subValues = [ ] <EOL> if not value : <EOL> setattr ( instance , attr , None ) <EOL> else : <EOL> for subValue in value : <EOL> subValues . append ( self . deserialize ( subValue , <EOL> subClass ) ) <EOL> setattr ( instance , attr , subValues ) <EOL> else : <EOL> setattr ( instance , attr , self . deserialize ( value , <EOL> objClass ) ) <EOL> return instance <EOL> class MethodRequest ( urllib2 . Request ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> self . method = kwargs . pop ( '<STR_LIT>' ) <EOL> return urllib2 . Request . __init__ ( self , * args , ** kwargs ) <EOL> def get_method ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , urllib2 . Request . get_method ( self ) ) </s>
