<s> import logging <EOL> import os <EOL> import platform <EOL> import sys <EOL> import urllib <EOL> import textwrap <EOL> import time <EOL> import datetime <EOL> import types <EOL> try : <EOL> import cStringIO as StringIO <EOL> except ImportError : <EOL> import StringIO <EOL> _httplib = None <EOL> try : <EOL> from google . appengine . api import urlfetch <EOL> _httplib = '<STR_LIT>' <EOL> except ImportError : <EOL> pass <EOL> if not _httplib : <EOL> try : <EOL> import requests <EOL> _httplib = '<STR_LIT>' <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> version = requests . __version__ <EOL> major , minor , patch = [ int ( i ) for i in version . split ( '<STR_LIT:.>' ) ] <EOL> except : <EOL> pass <EOL> else : <EOL> if minor < <NUM_LIT:8> or ( minor == <NUM_LIT:8> and patch < <NUM_LIT:8> ) : <EOL> print >> sys . stderr , '<STR_LIT>' % ( version , ) <EOL> _httplib = None <EOL> if not _httplib : <EOL> try : <EOL> import pycurl <EOL> _httplib = '<STR_LIT>' <EOL> except ImportError : <EOL> pass <EOL> if not _httplib : <EOL> try : <EOL> import urllib2 <EOL> _httplib = '<STR_LIT>' <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> except ImportError : <EOL> pass <EOL> if not _httplib : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> from version import VERSION <EOL> import importer <EOL> json = importer . import_json ( ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> api_key = None <EOL> api_base = '<STR_LIT>' <EOL> verify_ssl_certs = True <EOL> class StripeError ( Exception ) : <EOL> def __init__ ( self , message = None , http_body = None , http_status = None , json_body = None ) : <EOL> super ( StripeError , self ) . __init__ ( message ) <EOL> self . http_body = http_body <EOL> self . http_status = http_status <EOL> self . json_body = json_body <EOL> class APIError ( StripeError ) : <EOL> pass <EOL> class APIConnectionError ( StripeError ) : <EOL> pass <EOL> class CardError ( StripeError ) : <EOL> def __init__ ( self , message , param , code , http_body = None , http_status = None , json_body = None ) : <EOL> super ( CardError , self ) . __init__ ( message , http_body , http_status , json_body ) <EOL> self . param = param <EOL> self . code = code <EOL> self . http_body = http_body <EOL> self . http_status = http_status <EOL> self . json_body = json_body <EOL> class InvalidRequestError ( StripeError ) : <EOL> def __init__ ( self , message , param , http_body = None , http_status = None , json_body = None ) : <EOL> super ( InvalidRequestError , self ) . __init__ ( message , http_body , http_status , json_body ) <EOL> self . param = param <EOL> class AuthenticationError ( StripeError ) : <EOL> pass <EOL> def convert_to_stripe_object ( resp , api_key ) : <EOL> types = { '<STR_LIT>' : Charge , '<STR_LIT>' : Customer , <EOL> '<STR_LIT>' : Invoice , '<STR_LIT>' : InvoiceItem , <EOL> '<STR_LIT>' : Plan , '<STR_LIT>' : Coupon , '<STR_LIT>' : Token , '<STR_LIT>' : Event , <EOL> '<STR_LIT>' : Transfer } <EOL> if isinstance ( resp , list ) : <EOL> return [ convert_to_stripe_object ( i , api_key ) for i in resp ] <EOL> elif isinstance ( resp , dict ) : <EOL> resp = resp . copy ( ) <EOL> klass_name = resp . get ( '<STR_LIT:object>' ) <EOL> if isinstance ( klass_name , basestring ) : <EOL> klass = types . get ( klass_name , StripeObject ) <EOL> else : <EOL> klass = StripeObject <EOL> return klass . construct_from ( resp , api_key ) <EOL> else : <EOL> return resp <EOL> class APIRequestor ( object ) : <EOL> def __init__ ( self , key = None ) : <EOL> self . api_key = key <EOL> @ classmethod <EOL> def api_url ( cls , url = '<STR_LIT>' ) : <EOL> return '<STR_LIT>' % ( api_base , url ) <EOL> @ classmethod <EOL> def _utf8 ( cls , value ) : <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return value <EOL> @ classmethod <EOL> def encode_dict ( cls , stk , key , dictvalue ) : <EOL> n = { } <EOL> for k , v in dictvalue . iteritems ( ) : <EOL> k = cls . _utf8 ( k ) <EOL> v = cls . _utf8 ( v ) <EOL> n [ "<STR_LIT>" % ( key , k ) ] = v <EOL> stk . extend ( cls . _encode_inner ( n ) ) <EOL> @ classmethod <EOL> def encode_datetime ( cls , stk , key , dttime ) : <EOL> utc_timestamp = int ( time . mktime ( dttime . timetuple ( ) ) ) <EOL> stk . append ( ( key , utc_timestamp ) ) <EOL> @ classmethod <EOL> def encode_none ( cls , stk , k , v ) : <EOL> pass <EOL> @ classmethod <EOL> def _encode_inner ( cls , d ) : <EOL> """<STR_LIT>""" <EOL> ENCODERS = { <EOL> dict : cls . encode_dict , <EOL> datetime . datetime : cls . encode_datetime , <EOL> types . NoneType : cls . encode_none , <EOL> } <EOL> stk = [ ] <EOL> for key , value in d . iteritems ( ) : <EOL> key = cls . _utf8 ( key ) <EOL> try : <EOL> encoder = ENCODERS [ value . __class__ ] <EOL> encoder ( stk , key , value ) <EOL> except KeyError : <EOL> value = cls . _utf8 ( value ) <EOL> stk . append ( ( key , value ) ) <EOL> return stk <EOL> @ classmethod <EOL> def _objects_to_ids ( cls , d ) : <EOL> if isinstance ( d , APIResource ) : <EOL> return d . id <EOL> elif isinstance ( d , dict ) : <EOL> res = { } <EOL> for k , v in d . iteritems ( ) : <EOL> res [ k ] = cls . _objects_to_ids ( v ) <EOL> return res <EOL> else : <EOL> return d <EOL> @ classmethod <EOL> def encode ( cls , d ) : <EOL> """<STR_LIT>""" <EOL> return urllib . urlencode ( cls . _encode_inner ( d ) ) <EOL> def request ( self , meth , url , params = { } ) : <EOL> rbody , rcode , my_api_key = self . request_raw ( meth , url , params ) <EOL> resp = self . interpret_response ( rbody , rcode ) <EOL> return resp , my_api_key <EOL> def handle_api_error ( self , rbody , rcode , resp ) : <EOL> try : <EOL> error = resp [ '<STR_LIT:error>' ] <EOL> except ( KeyError , TypeError ) : <EOL> raise APIError ( "<STR_LIT>" % ( rbody , rcode ) , rbody , rcode , resp ) <EOL> if rcode in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> raise InvalidRequestError ( error . get ( '<STR_LIT:message>' ) , error . get ( '<STR_LIT>' ) , rbody , rcode , resp ) <EOL> elif rcode == <NUM_LIT> : <EOL> raise AuthenticationError ( error . get ( '<STR_LIT:message>' ) , rbody , rcode , resp ) <EOL> elif rcode == <NUM_LIT> : <EOL> raise CardError ( error . get ( '<STR_LIT:message>' ) , error . get ( '<STR_LIT>' ) , error . get ( '<STR_LIT:code>' ) , rbody , rcode , resp ) <EOL> else : <EOL> raise APIError ( error . get ( '<STR_LIT:message>' ) , rbody , rcode , resp ) <EOL> def request_raw ( self , meth , url , params = { } ) : <EOL> """<STR_LIT>""" <EOL> my_api_key = self . api_key or api_key <EOL> if my_api_key is None : <EOL> raise AuthenticationError ( '<STR_LIT>' ) <EOL> abs_url = self . api_url ( url ) <EOL> params = params . copy ( ) <EOL> self . _objects_to_ids ( params ) <EOL> ua = { <EOL> '<STR_LIT>' : VERSION , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for attr , func in [ [ '<STR_LIT>' , platform . python_version ] , <EOL> [ '<STR_LIT>' , platform . platform ] , <EOL> [ '<STR_LIT>' , lambda : '<STR_LIT:U+0020>' . join ( platform . uname ( ) ) ] ] : <EOL> try : <EOL> val = func ( ) <EOL> except Exception , e : <EOL> val = "<STR_LIT>" % e <EOL> ua [ attr ] = val <EOL> headers = { <EOL> '<STR_LIT>' : json . dumps ( ua ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( VERSION , ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( my_api_key , ) <EOL> } <EOL> if _httplib == '<STR_LIT>' : <EOL> rbody , rcode = self . requests_request ( meth , abs_url , headers , params ) <EOL> elif _httplib == '<STR_LIT>' : <EOL> rbody , rcode = self . pycurl_request ( meth , abs_url , headers , params ) <EOL> elif _httplib == '<STR_LIT>' : <EOL> rbody , rcode = self . urlfetch_request ( meth , abs_url , headers , params ) <EOL> elif _httplib == '<STR_LIT>' : <EOL> rbody , rcode = self . urllib2_request ( meth , abs_url , headers , params ) <EOL> else : <EOL> raise StripeError ( "<STR_LIT>" % ( _httplib , ) ) <EOL> logger . info ( '<STR_LIT>' % ( abs_url , rcode , rbody ) ) <EOL> return rbody , rcode , my_api_key <EOL> def interpret_response ( self , rbody , rcode ) : <EOL> try : <EOL> resp = json . loads ( rbody ) <EOL> except Exception : <EOL> raise APIError ( "<STR_LIT>" % ( rbody , rcode ) , rbody , rcode ) <EOL> if not ( <NUM_LIT:200> <= rcode < <NUM_LIT> ) : <EOL> self . handle_api_error ( rbody , rcode , resp ) <EOL> return resp <EOL> def requests_request ( self , meth , abs_url , headers , params ) : <EOL> meth = meth . lower ( ) <EOL> if meth == '<STR_LIT>' or meth == '<STR_LIT>' : <EOL> if params : <EOL> abs_url = '<STR_LIT>' % ( abs_url , self . encode ( params ) ) <EOL> data = None <EOL> elif meth == '<STR_LIT>' : <EOL> data = self . encode ( params ) <EOL> else : <EOL> raise APIConnectionError ( '<STR_LIT>' % ( meth , ) ) <EOL> try : <EOL> try : <EOL> result = requests . request ( meth , abs_url , <EOL> headers = headers , data = data , timeout = <NUM_LIT> , <EOL> verify = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> except TypeError , e : <EOL> raise TypeError ( '<STR_LIT>' % ( e , ) ) <EOL> content = result . content <EOL> status_code = result . status_code <EOL> except Exception , e : <EOL> self . handle_requests_error ( e ) <EOL> return content , status_code <EOL> def handle_requests_error ( self , e ) : <EOL> if isinstance ( e , requests . exceptions . RequestException ) : <EOL> msg = "<STR_LIT>" <EOL> err = "<STR_LIT>" % ( type ( e ) . __name__ , e . message ) <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> err = "<STR_LIT>" % ( type ( e ) . __name__ , ) <EOL> if e . message : <EOL> err += "<STR_LIT>" % ( e . message , ) <EOL> else : <EOL> err += "<STR_LIT>" <EOL> msg = textwrap . fill ( msg ) + "<STR_LIT>" + err + "<STR_LIT:)>" <EOL> raise APIConnectionError ( msg ) <EOL> def pycurl_request ( self , meth , abs_url , headers , params ) : <EOL> s = StringIO . StringIO ( ) <EOL> curl = pycurl . Curl ( ) <EOL> meth = meth . lower ( ) <EOL> if meth == '<STR_LIT>' : <EOL> curl . setopt ( pycurl . HTTPGET , <NUM_LIT:1> ) <EOL> if params : <EOL> abs_url = '<STR_LIT>' % ( abs_url , self . encode ( params ) ) <EOL> elif meth == '<STR_LIT>' : <EOL> curl . setopt ( pycurl . POST , <NUM_LIT:1> ) <EOL> curl . setopt ( pycurl . POSTFIELDS , self . encode ( params ) ) <EOL> elif meth == '<STR_LIT>' : <EOL> curl . setopt ( pycurl . CUSTOMREQUEST , '<STR_LIT>' ) <EOL> if params : <EOL> abs_url = '<STR_LIT>' % ( abs_url , self . encode ( params ) ) <EOL> else : <EOL> raise APIConnectionError ( '<STR_LIT>' % ( meth , ) ) <EOL> abs_url = self . _utf8 ( abs_url ) <EOL> curl . setopt ( pycurl . URL , abs_url ) <EOL> curl . setopt ( pycurl . WRITEFUNCTION , s . write ) <EOL> curl . setopt ( pycurl . NOSIGNAL , <NUM_LIT:1> ) <EOL> curl . setopt ( pycurl . CONNECTTIMEOUT , <NUM_LIT:30> ) <EOL> curl . setopt ( pycurl . TIMEOUT , <NUM_LIT> ) <EOL> curl . setopt ( pycurl . HTTPHEADER , [ '<STR_LIT>' % ( k , v ) for k , v in headers . iteritems ( ) ] ) <EOL> if verify_ssl_certs : <EOL> curl . setopt ( pycurl . CAINFO , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> else : <EOL> curl . setopt ( pycurl . SSL_VERIFYHOST , False ) <EOL> try : <EOL> curl . perform ( ) <EOL> except pycurl . error , e : <EOL> self . handle_pycurl_error ( e ) <EOL> rbody = s . getvalue ( ) <EOL> rcode = curl . getinfo ( pycurl . RESPONSE_CODE ) <EOL> return rbody , rcode <EOL> def handle_pycurl_error ( self , e ) : <EOL> if e [ <NUM_LIT:0> ] in [ pycurl . E_COULDNT_CONNECT , <EOL> pycurl . E_COULDNT_RESOLVE_HOST , <EOL> pycurl . E_OPERATION_TIMEOUTED ] : <EOL> msg = "<STR_LIT>" % ( api_base , ) <EOL> elif e [ <NUM_LIT:0> ] == pycurl . E_SSL_CACERT or e [ <NUM_LIT:0> ] == pycurl . E_SSL_PEER_CERTIFICATE : <EOL> msg = "<STR_LIT>" % ( api_base , ) <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> msg = textwrap . fill ( msg ) + "<STR_LIT>" + e [ <NUM_LIT:1> ] + "<STR_LIT:)>" <EOL> raise APIConnectionError ( msg ) <EOL> def urlfetch_request ( self , meth , abs_url , headers , params ) : <EOL> args = { } <EOL> if meth == '<STR_LIT>' : <EOL> args [ '<STR_LIT>' ] = self . encode ( params ) <EOL> elif meth == '<STR_LIT>' or meth == '<STR_LIT>' : <EOL> abs_url = '<STR_LIT>' % ( abs_url , self . encode ( params ) ) <EOL> else : <EOL> raise APIConnectionError ( '<STR_LIT>' % ( meth , ) ) <EOL> args [ '<STR_LIT:url>' ] = abs_url <EOL> args [ '<STR_LIT>' ] = meth <EOL> args [ '<STR_LIT>' ] = headers <EOL> args [ '<STR_LIT>' ] = verify_ssl_certs <EOL> args [ '<STR_LIT>' ] = <NUM_LIT> <EOL> try : <EOL> result = urlfetch . fetch ( ** args ) <EOL> except urlfetch . Error , e : <EOL> self . handle_urlfetch_error ( e , abs_url ) <EOL> return result . content , result . status_code <EOL> def handle_urlfetch_error ( self , e , abs_url ) : <EOL> if isinstance ( e , urlfetch . InvalidURLError ) : <EOL> msg = "<STR_LIT>" % ( abs_url , ) <EOL> elif isinstance ( e , urlfetch . DownloadError ) : <EOL> msg = "<STR_LIT>" <EOL> elif isinstance ( e , urlfetch . ResponseTooLargeError ) : <EOL> msg = "<STR_LIT>" <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> msg = textwrap . fill ( msg ) + "<STR_LIT>" + str ( e ) + "<STR_LIT:)>" <EOL> raise APIConnectionError ( msg ) <EOL> def urllib2_request ( self , meth , abs_url , headers , params ) : <EOL> args = { } <EOL> if meth == '<STR_LIT>' : <EOL> abs_url = '<STR_LIT>' % ( abs_url , self . encode ( params ) ) <EOL> req = urllib2 . Request ( abs_url , None , headers ) <EOL> elif meth == '<STR_LIT>' : <EOL> body = self . encode ( params ) <EOL> req = urllib2 . Request ( abs_url , body , headers ) <EOL> elif meth == '<STR_LIT>' : <EOL> abs_url = '<STR_LIT>' % ( abs_url , self . encode ( params ) ) <EOL> req = urllib2 . Request ( abs_url , None , headers ) <EOL> req . get_method = lambda : '<STR_LIT>' <EOL> else : <EOL> raise APIConnectionError ( '<STR_LIT>' % ( meth , ) ) <EOL> try : <EOL> response = urllib2 . urlopen ( req ) <EOL> rbody = response . read ( ) <EOL> rcode = response . code <EOL> except urllib2 . HTTPError , e : <EOL> rcode = e . code <EOL> rbody = e . read ( ) <EOL> except ( urllib2 . URLError , ValueError ) , e : <EOL> self . handle_urllib2_error ( e , abs_url ) <EOL> return rbody , rcode <EOL> def handle_urllib2_error ( self , e , abs_url ) : <EOL> msg = "<STR_LIT>" <EOL> msg = textwrap . fill ( msg ) + "<STR_LIT>" + str ( e ) + "<STR_LIT:)>" <EOL> raise APIConnectionError ( msg ) <EOL> class StripeObject ( object ) : <EOL> _permanent_attributes = set ( [ '<STR_LIT>' ] ) <EOL> def __init__ ( self , id = None , api_key = None ) : <EOL> self . __dict__ [ '<STR_LIT>' ] = set ( ) <EOL> self . __dict__ [ '<STR_LIT>' ] = set ( ) <EOL> self . __dict__ [ '<STR_LIT>' ] = set ( ) <EOL> self . __dict__ [ '<STR_LIT>' ] = api_key <EOL> if id : <EOL> self . id = id <EOL> def __setattr__ ( self , k , v ) : <EOL> self . __dict__ [ k ] = v <EOL> self . _values . add ( k ) <EOL> if k not in self . _permanent_attributes : <EOL> self . _unsaved_values . add ( k ) <EOL> def __getattr__ ( self , k ) : <EOL> try : <EOL> return self . __dict__ [ k ] <EOL> except KeyError : <EOL> pass <EOL> if k in self . _transient_values : <EOL> raise AttributeError ( "<STR_LIT>" % <EOL> ( type ( self ) . __name__ , k , k , '<STR_LIT:U+002CU+0020>' . join ( self . _values ) ) ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" % ( type ( self ) . __name__ , k ) ) <EOL> def __getitem__ ( self , k ) : <EOL> if k in self . _values : <EOL> return self . __dict__ [ k ] <EOL> elif k in self . _transient_values : <EOL> raise KeyError ( "<STR_LIT>" % ( k , k , '<STR_LIT:U+002CU+0020>' . join ( self . _values ) ) ) <EOL> else : <EOL> raise KeyError ( k ) <EOL> def get ( self , k , default = None ) : <EOL> try : <EOL> return self [ k ] <EOL> except KeyError : <EOL> return default <EOL> def setdefault ( self , k , default = None ) : <EOL> try : <EOL> return self [ k ] <EOL> except KeyError : <EOL> self [ k ] = default <EOL> return default <EOL> def __setitem__ ( self , k , v ) : <EOL> setattr ( self , k , v ) <EOL> def keys ( self ) : <EOL> return self . _values . keys ( ) <EOL> def values ( self ) : <EOL> return self . _values . keys ( ) <EOL> @ classmethod <EOL> def construct_from ( cls , values , api_key ) : <EOL> instance = cls ( values . get ( '<STR_LIT:id>' ) , api_key ) <EOL> instance . refresh_from ( values , api_key ) <EOL> return instance <EOL> def refresh_from ( self , values , api_key , partial = False ) : <EOL> self . api_key = api_key <EOL> if partial : <EOL> removed = set ( ) <EOL> else : <EOL> removed = self . _values - set ( values ) <EOL> for k in removed : <EOL> if k in self . _permanent_attributes : <EOL> continue <EOL> del self . __dict__ [ k ] <EOL> self . _values . discard ( k ) <EOL> self . _transient_values . add ( k ) <EOL> self . _unsaved_values . discard ( k ) <EOL> for k , v in values . iteritems ( ) : <EOL> if k in self . _permanent_attributes : <EOL> continue <EOL> self . __dict__ [ k ] = convert_to_stripe_object ( v , api_key ) <EOL> self . _values . add ( k ) <EOL> self . _transient_values . discard ( k ) <EOL> self . _unsaved_values . discard ( k ) <EOL> def __repr__ ( self ) : <EOL> type_string = '<STR_LIT>' <EOL> if isinstance ( self . get ( '<STR_LIT:object>' ) , basestring ) : <EOL> type_string = '<STR_LIT>' % self . get ( '<STR_LIT:object>' ) . encode ( '<STR_LIT:utf8>' ) <EOL> id_string = '<STR_LIT>' <EOL> if isinstance ( self . get ( '<STR_LIT:id>' ) , basestring ) : <EOL> id_string = '<STR_LIT>' % self . get ( '<STR_LIT:id>' ) . encode ( '<STR_LIT:utf8>' ) <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , type_string , id_string , hex ( id ( self ) ) , json . dumps ( self . to_dict ( ) , sort_keys = True , indent = <NUM_LIT:2> , cls = StripeObjectEncoder ) ) <EOL> def __str__ ( self ) : <EOL> return json . dumps ( self . to_dict ( ) , sort_keys = True , indent = <NUM_LIT:2> , cls = StripeObjectEncoder ) <EOL> def to_dict ( self ) : <EOL> def _serialize ( o ) : <EOL> if isinstance ( o , StripeObject ) : <EOL> return o . to_dict ( ) <EOL> if isinstance ( o , list ) : <EOL> return [ _serialize ( i ) for i in o ] <EOL> return o <EOL> d = dict ( ) <EOL> for k in sorted ( self . _values ) : <EOL> if k in self . _permanent_attributes : <EOL> continue <EOL> v = getattr ( self , k ) <EOL> v = _serialize ( v ) <EOL> d [ k ] = v <EOL> return d <EOL> class StripeObjectEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , StripeObject ) : <EOL> return obj . to_dict ( ) <EOL> else : <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> class APIResource ( StripeObject ) : <EOL> def _ident ( self ) : <EOL> return [ self . get ( '<STR_LIT:id>' ) ] <EOL> @ classmethod <EOL> def retrieve ( cls , id , api_key = None ) : <EOL> instance = cls ( id , api_key ) <EOL> instance . refresh ( ) <EOL> return instance <EOL> def refresh ( self ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url ) <EOL> self . refresh_from ( response , api_key ) <EOL> return self <EOL> @ classmethod <EOL> def class_url ( cls ) : <EOL> if cls == APIResource : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> return "<STR_LIT>" % urllib . quote_plus ( cls . __name__ . lower ( ) ) <EOL> def instance_url ( self ) : <EOL> id = self . get ( '<STR_LIT:id>' ) <EOL> if not id : <EOL> raise InvalidRequestError ( '<STR_LIT>' % ( type ( self ) . __name__ , id ) , '<STR_LIT:id>' ) <EOL> id = APIRequestor . _utf8 ( id ) <EOL> base = self . class_url ( ) <EOL> extn = urllib . quote_plus ( id ) <EOL> return "<STR_LIT>" % ( base , extn ) <EOL> class ListableAPIResource ( APIResource ) : <EOL> @ classmethod <EOL> def all ( cls , api_key = None , ** params ) : <EOL> requestor = APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_stripe_object ( response , api_key ) <EOL> class CreateableAPIResource ( APIResource ) : <EOL> @ classmethod <EOL> def create ( cls , api_key = None , ** params ) : <EOL> requestor = APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_stripe_object ( response , api_key ) <EOL> class UpdateableAPIResource ( APIResource ) : <EOL> def save ( self ) : <EOL> if self . _unsaved_values : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> params = { } <EOL> for k in self . _unsaved_values : <EOL> params [ k ] = getattr ( self , k ) <EOL> url = self . instance_url ( ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> self . refresh_from ( response , api_key ) <EOL> else : <EOL> logger . debug ( "<STR_LIT>" % ( self , ) ) <EOL> return self <EOL> class DeletableAPIResource ( APIResource ) : <EOL> def delete ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> self . refresh_from ( response , api_key ) <EOL> return self <EOL> class Charge ( CreateableAPIResource , ListableAPIResource , UpdateableAPIResource ) : <EOL> def refund ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> self . refresh_from ( response , api_key ) <EOL> return self <EOL> def capture ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> self . refresh_from ( response , api_key ) <EOL> return self <EOL> class Customer ( CreateableAPIResource , UpdateableAPIResource , <EOL> ListableAPIResource , DeletableAPIResource ) : <EOL> def add_invoice_item ( self , ** params ) : <EOL> params [ '<STR_LIT>' ] = self . id <EOL> ii = InvoiceItem . create ( self . api_key , ** params ) <EOL> return ii <EOL> def invoices ( self , ** params ) : <EOL> params [ '<STR_LIT>' ] = self . id <EOL> invoices = Invoice . all ( self . api_key , ** params ) <EOL> return invoices <EOL> def invoice_items ( self , ** params ) : <EOL> params [ '<STR_LIT>' ] = self . id <EOL> iis = InvoiceItem . all ( self . api_key , ** params ) <EOL> return iis <EOL> def charges ( self , ** params ) : <EOL> params [ '<STR_LIT>' ] = self . id <EOL> charges = Charge . all ( self . api_key , ** params ) <EOL> return charges <EOL> def update_subscription ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> self . refresh_from ( { '<STR_LIT>' : response } , api_key , True ) <EOL> return self . subscription <EOL> def cancel_subscription ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> self . refresh_from ( { '<STR_LIT>' : response } , api_key , True ) <EOL> return self . subscription <EOL> def delete_discount ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url ) <EOL> self . refresh_from ( { '<STR_LIT>' : None } , api_key , True ) <EOL> class Invoice ( ListableAPIResource ) : <EOL> @ classmethod <EOL> def upcoming ( cls , api_key = None , ** params ) : <EOL> requestor = APIRequestor ( api_key ) <EOL> url = cls . class_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_stripe_object ( response , api_key ) <EOL> class InvoiceItem ( CreateableAPIResource , UpdateableAPIResource , <EOL> ListableAPIResource , DeletableAPIResource ) : <EOL> pass <EOL> class Plan ( CreateableAPIResource , DeletableAPIResource , UpdateableAPIResource , ListableAPIResource ) : <EOL> pass <EOL> class Token ( CreateableAPIResource ) : <EOL> pass <EOL> class Coupon ( CreateableAPIResource , DeletableAPIResource , ListableAPIResource ) : <EOL> pass <EOL> class Event ( ListableAPIResource ) : <EOL> pass <EOL> class Transfer ( ListableAPIResource ) : <EOL> def transactions ( self , ** params ) : <EOL> requestor = APIRequestor ( self . api_key ) <EOL> url = self . instance_url ( ) + '<STR_LIT>' <EOL> response , api_key = requestor . request ( '<STR_LIT>' , url , params ) <EOL> return convert_to_stripe_object ( response , api_key ) </s>
