<s> import requests <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> IAMPORT_API_URL = '<STR_LIT>' <EOL> class Iamport ( object ) : <EOL> def __init__ ( self , imp_key , imp_secret , imp_url = IAMPORT_API_URL ) : <EOL> self . imp_key = imp_key <EOL> self . imp_secret = imp_secret <EOL> self . imp_url = imp_url <EOL> requests_session = requests . Session ( ) <EOL> requests_adapters = requests . adapters . HTTPAdapter ( max_retries = <NUM_LIT:3> ) <EOL> requests_session . mount ( '<STR_LIT>' , requests_adapters ) <EOL> self . requests_session = requests_session <EOL> class ResponseError ( Exception ) : <EOL> def __init__ ( self , code = None , message = None ) : <EOL> self . code = code <EOL> self . message = message <EOL> @ staticmethod <EOL> def get_response ( response ) : <EOL> if response . status_code != requests . codes . ok : <EOL> return { } <EOL> result = response . json ( ) <EOL> if result [ '<STR_LIT:code>' ] != <NUM_LIT:0> : <EOL> raise Iamport . ResponseError ( result . get ( '<STR_LIT:code>' ) , result . get ( '<STR_LIT:message>' ) ) <EOL> return result . get ( '<STR_LIT>' ) <EOL> def _get_token ( self ) : <EOL> url = '<STR_LIT>' . format ( self . imp_url ) <EOL> payload = { '<STR_LIT>' : self . imp_key , <EOL> '<STR_LIT>' : self . imp_secret } <EOL> response = self . requests_session . post ( url , data = payload ) <EOL> return self . get_response ( response ) . get ( '<STR_LIT>' ) <EOL> def get_headers ( self ) : <EOL> return { '<STR_LIT>' : self . _get_token ( ) } <EOL> def _get ( self , url , payload = None ) : <EOL> headers = self . get_headers ( ) <EOL> response = self . requests_session . get ( url , headers = headers , params = payload ) <EOL> return self . get_response ( response ) <EOL> def _post ( self , url , payload = None ) : <EOL> headers = self . get_headers ( ) <EOL> response = self . requests_session . post ( url , headers = headers , data = payload ) <EOL> return self . get_response ( response ) <EOL> def find_by_merchant_uid ( self , merchant_uid ) : <EOL> url = '<STR_LIT>' . format ( self . imp_url , merchant_uid ) <EOL> return self . _get ( url ) <EOL> def find_by_imp_uid ( self , imp_uid ) : <EOL> url = '<STR_LIT>' . format ( self . imp_url , imp_uid ) <EOL> return self . _get ( url ) <EOL> def find ( self , ** kwargs ) : <EOL> merchant_uid = kwargs . get ( '<STR_LIT>' ) <EOL> if merchant_uid : <EOL> return self . find_by_merchant_uid ( merchant_uid ) <EOL> try : <EOL> imp_uid = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> return self . find_by_imp_uid ( imp_uid ) <EOL> def _cancel ( self , payload ) : <EOL> url = '<STR_LIT>' . format ( self . imp_url ) <EOL> return self . _post ( url , payload ) <EOL> def pay_onetime ( self , ** kwargs ) : <EOL> url = '<STR_LIT>' . format ( self . imp_url ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if key not in kwargs : <EOL> raise KeyError ( '<STR_LIT>' % key ) <EOL> return self . _post ( url , kwargs ) <EOL> def cancel_by_merchant_uid ( self , merchant_uid , reason ) : <EOL> payload = { '<STR_LIT>' : merchant_uid , '<STR_LIT>' : reason } <EOL> return self . _cancel ( payload ) <EOL> def cancel_by_imp_uid ( self , imp_uid , reason ) : <EOL> payload = { '<STR_LIT>' : imp_uid , '<STR_LIT>' : reason } <EOL> return self . _cancel ( payload ) <EOL> def cancel ( self , reason , ** kwargs ) : <EOL> merchant_uid = kwargs . get ( '<STR_LIT>' ) <EOL> if merchant_uid : <EOL> return self . cancel_by_merchant_uid ( merchant_uid , reason ) <EOL> try : <EOL> imp_uid = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> return self . cancel_by_imp_uid ( imp_uid , reason ) <EOL> def is_paid ( self , amount , ** kwargs ) : <EOL> response = kwargs . get ( '<STR_LIT>' ) <EOL> if not response : <EOL> response = self . find ( ** kwargs ) <EOL> status = response . get ( '<STR_LIT:status>' ) <EOL> response_amount = response . get ( '<STR_LIT>' ) <EOL> return status == '<STR_LIT>' and response_amount == amount </s>
