<s> import urllib <EOL> import sys <EOL> import time <EOL> from shippo import api_requestor , error , util , rates_req_timeout , transaction_req_timeout <EOL> def convert_to_shippo_object ( resp , api_key ) : <EOL> if isinstance ( resp , list ) : <EOL> return [ convert_to_shippo_object ( i , api_key ) for i in resp ] <EOL> elif isinstance ( resp , dict ) and not isinstance ( resp , ShippoObject ) : <EOL> resp = resp . copy ( ) <EOL> return ShippoObject . construct_from ( resp , api_key ) <EOL> else : <EOL> return resp <EOL> class ShippoObject ( dict ) : <EOL> def __init__ ( self , id = None , api_key = None , ** params ) : <EOL> super ( ShippoObject , self ) . __init__ ( ) <EOL> self . _unsaved_values = set ( ) <EOL> self . _transient_values = set ( ) <EOL> self . _retrieve_params = params <EOL> self . _previous_metadata = None <EOL> object . __setattr__ ( self , '<STR_LIT>' , api_key ) <EOL> if id : <EOL> self [ '<STR_LIT>' ] = id <EOL> def __setattr__ ( self , k , v ) : <EOL> if k [ <NUM_LIT:0> ] == '<STR_LIT:_>' or k in self . __dict__ : <EOL> return super ( ShippoObject , self ) . __setattr__ ( k , v ) <EOL> else : <EOL> self [ k ] = v <EOL> def __getattr__ ( self , k ) : <EOL> if k [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> raise AttributeError ( k ) <EOL> try : <EOL> return self [ k ] <EOL> except KeyError , err : <EOL> raise AttributeError ( * err . args ) <EOL> def __setitem__ ( self , k , v ) : <EOL> if v == "<STR_LIT>" : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> k , str ( self ) , k ) ) <EOL> super ( ShippoObject , self ) . __setitem__ ( k , v ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _unsaved_values = set ( ) <EOL> self . _unsaved_values . add ( k ) <EOL> def __getitem__ ( self , k ) : <EOL> try : <EOL> return super ( ShippoObject , self ) . __getitem__ ( k ) <EOL> except KeyError , err : <EOL> if k in self . _transient_values : <EOL> raise KeyError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( k , k , '<STR_LIT:U+002CU+0020>' . join ( self . keys ( ) ) ) ) <EOL> else : <EOL> raise err <EOL> def __delitem__ ( self , k ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ classmethod <EOL> def construct_from ( cls , values , api_key ) : <EOL> instance = cls ( values . get ( '<STR_LIT>' ) , api_key ) <EOL> instance . refresh_from ( values , api_key ) <EOL> return instance <EOL> def refresh_from ( self , values , api_key = None , partial = False ) : <EOL> self . api_key = api_key or getattr ( values , '<STR_LIT>' , None ) <EOL> if partial : <EOL> self . _unsaved_values = ( self . _unsaved_values - set ( values ) ) <EOL> else : <EOL> removed = set ( self . keys ( ) ) - set ( values ) <EOL> self . _transient_values = self . _transient_values | removed <EOL> self . _unsaved_values = set ( ) <EOL> self . clear ( ) <EOL> self . _transient_values = self . _transient_values - set ( values ) <EOL> for k , v in values . iteritems ( ) : <EOL> super ( ShippoObject , self ) . __setitem__ ( <EOL> k , convert_to_shippo_object ( v , api_key ) ) <EOL> self . _previous_metadata = values . get ( '<STR_LIT>' ) <EOL> def request ( self , method , url , params = None ) : <EOL> if params is None : <EOL> params = self . _retrieve_params <EOL> requestor = api_requestor . APIRequestor ( self . api_key ) <EOL> response , api_key = requestor . request ( method , url , params ) <EOL> return convert_to_shippo_object ( response , api_key ) <EOL> def __repr__ ( self ) : <EOL> ident_parts = [ type ( self ) . __name__ ] <EOL> if isinstance ( self . get ( '<STR_LIT:object>' ) , basestring ) : <EOL> ident_parts . append ( self . get ( '<STR_LIT:object>' ) ) <EOL> if isinstance ( self . get ( '<STR_LIT>' ) , basestring ) : <EOL> ident_parts . append ( '<STR_LIT>' % ( self . get ( '<STR_LIT>' ) , ) ) <EOL> unicode_repr = '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' . join ( ident_parts ) , hex ( id ( self ) ) , str ( self ) ) <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> return unicode_repr . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return unicode_repr <EOL> def __str__ ( self ) : <EOL> return util . json . dumps ( self , sort_keys = True , indent = <NUM_LIT:2> ) <EOL> @ property <EOL> def shippo_id ( self ) : <EOL> return self . id <EOL> class APIResource ( ShippoObject ) : <EOL> def refresh ( self ) : <EOL> self . refresh_from ( self . request ( '<STR_LIT>' , self . instance_url ( ) ) ) <EOL> return self <EOL> @ classmethod <EOL> def class_name ( cls ) : <EOL> if cls == APIResource : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return str ( urllib . quote_plus ( cls . __name__ . lower ( ) ) ) <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> def instance_url ( self ) : <EOL> object_id = self . get ( '<STR_LIT>' ) <EOL> if not object_id : <EOL> raise error . InvalidRequestError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( type ( self ) . __name__ , object_id ) , '<STR_LIT>' ) <EOL> object_id = util . utf8 ( object_id ) <EOL> base = self . class_url ( ) <EOL> extn = urllib . quote_plus ( object_id ) <EOL> return "<STR_LIT>" % ( base , extn ) <EOL> class CreateableAPIResource ( APIResource ) : <EOL> @ classmethod <EOL> def create ( cls , api_key = None , ** params ) : <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_shippo_object ( response , api_key ) <EOL> class ListableAPIResource ( APIResource ) : <EOL> @ classmethod <EOL> def all ( cls , api_key = None , size = None , page = None , ** params ) : <EOL> """<STR_LIT>""" <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) <EOL> if size : <EOL> url = url + '<STR_LIT>' + urllib . quote_plus ( str ( size ) ) <EOL> if page : <EOL> url = url + '<STR_LIT>' + urllib . quote_plus ( str ( page ) ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_shippo_object ( response , api_key ) <EOL> class FetchableAPIResource ( APIResource ) : <EOL> @ classmethod <EOL> def retrieve ( cls , object_id , api_key = None ) : <EOL> object_id = util . utf8 ( object_id ) <EOL> extn = urllib . quote_plus ( object_id ) <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) + extn <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url ) <EOL> return convert_to_shippo_object ( response , api_key ) <EOL> class UpdateableAPIResource ( APIResource ) : <EOL> @ classmethod <EOL> def update ( cls , object_id , api_key = None , ** params ) : <EOL> object_id = util . utf8 ( object_id ) <EOL> extn = urllib . quote_plus ( object_id ) <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) + extn <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_shippo_object ( response , api_key ) <EOL> class Address ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> @ classmethod <EOL> def validate ( cls , object_id , api_key = None ) : <EOL> extn = urllib . quote_plus ( object_id ) <EOL> url = cls . class_url ( ) + extn + '<STR_LIT>' <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> response , content = requestor . request ( '<STR_LIT>' , url ) <EOL> return convert_to_shippo_object ( response , content ) <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class CustomsItem ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> return "<STR_LIT>" <EOL> class CustomsDeclaration ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> return "<STR_LIT>" <EOL> class Parcel ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class Manifest ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class Refund ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class Shipment ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def get_rates ( cls , object_id , api_key = None , currency = None , ** params ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in params and params [ '<STR_LIT>' ] : <EOL> timeout = time . time ( ) + rates_req_timeout <EOL> while cls . retrieve ( object_id ) . object_status in ( "<STR_LIT>" , "<STR_LIT>" ) and time . time ( ) < timeout : <EOL> continue <EOL> shipment_id = urllib . quote_plus ( object_id ) <EOL> url = cls . class_url ( ) + shipment_id + '<STR_LIT>' <EOL> if currency : <EOL> url = url + '<STR_LIT>' + urllib . quote_plus ( currency ) <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> response , content = requestor . request ( '<STR_LIT>' , url ) <EOL> return convert_to_shippo_object ( response , content ) <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class Transaction ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def create ( cls , api_key = None , ** params ) : <EOL> """<STR_LIT>""" <EOL> url = cls . class_url ( ) <EOL> requestor = api_requestor . APIRequestor ( api_key ) <EOL> response , content = requestor . request ( '<STR_LIT>' , url , params ) <EOL> transaction = convert_to_shippo_object ( response , content ) <EOL> if '<STR_LIT>' in params and params [ '<STR_LIT>' ] : <EOL> timeout = time . time ( ) + transaction_req_timeout <EOL> while transaction . object_status in ( "<STR_LIT>" , "<STR_LIT>" ) and time . time ( ) < timeout : <EOL> transaction = cls . retrieve ( transaction . object_id ) <EOL> continue <EOL> return transaction <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class Rate ( ListableAPIResource , FetchableAPIResource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> cls_name = cls . class_name ( ) <EOL> return "<STR_LIT>" % ( cls_name , ) <EOL> class CarrierAccount ( CreateableAPIResource , ListableAPIResource , FetchableAPIResource , UpdateableAPIResource ) : <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> return "<STR_LIT>" </s>
