<s> from six . moves import http_client as httplib <EOL> from xml . etree import ElementTree <EOL> import six <EOL> class ResponseError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response_xml ) : <EOL> self . response_xml = response_xml <EOL> @ property <EOL> def response_doc ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> self . __dict__ [ '<STR_LIT>' ] = ElementTree . fromstring ( self . response_xml ) <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> @ property <EOL> def symbol ( self ) : <EOL> """<STR_LIT>""" <EOL> el = self . response_doc . find ( '<STR_LIT>' ) <EOL> if el is not None : <EOL> return el . text <EOL> @ property <EOL> def message ( self ) : <EOL> """<STR_LIT>""" <EOL> el = self . response_doc . find ( '<STR_LIT:description>' ) <EOL> if el is not None : <EOL> return el . text <EOL> @ property <EOL> def details ( self ) : <EOL> """<STR_LIT>""" <EOL> el = self . response_doc . find ( '<STR_LIT>' ) <EOL> if el is not None : <EOL> return el . text <EOL> @ property <EOL> def error ( self ) : <EOL> """<STR_LIT>""" <EOL> el = self . response_doc . find ( '<STR_LIT:error>' ) <EOL> if el is not None : <EOL> return el . text <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) <EOL> def __unicode__ ( self ) : <EOL> symbol = self . symbol <EOL> if symbol is None : <EOL> return self . error <EOL> details = self . details <EOL> if details is not None : <EOL> return six . u ( '<STR_LIT>' ) % ( symbol , self . message , details ) <EOL> return six . u ( '<STR_LIT>' ) % ( symbol , self . message ) <EOL> class ClientError ( ResponseError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class BadRequestError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ConfigurationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnauthorizedError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response_xml ) : <EOL> self . response_text = response_xml <EOL> def __unicode__ ( self ) : <EOL> return six . text_type ( self . response_text ) <EOL> class PaymentRequiredError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ForbiddenError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NotFoundError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NotAcceptableError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class PreconditionFailedError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnsupportedMediaTypeError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ValidationError ( ClientError ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def transaction_error_code ( self ) : <EOL> """<STR_LIT>""" <EOL> error = self . response_doc . find ( '<STR_LIT>' ) <EOL> if error is not None : <EOL> code = error . find ( '<STR_LIT>' ) <EOL> if code is not None : <EOL> return code . text <EOL> class Suberror ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field , symbol , message ) : <EOL> self . field = field <EOL> self . symbol = symbol <EOL> self . message = message <EOL> def __unicode__ ( self ) : <EOL> return six . u ( '<STR_LIT>' ) % ( self . symbol , self . field , self . message ) <EOL> @ property <EOL> def errors ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> suberrors = dict ( ) <EOL> for err in self . response_doc . findall ( '<STR_LIT:error>' ) : <EOL> field = err . attrib [ '<STR_LIT>' ] <EOL> symbol = err . attrib [ '<STR_LIT>' ] <EOL> message = err . text <EOL> suberrors [ field ] = self . Suberror ( field , symbol , message ) <EOL> self . __dict__ [ '<STR_LIT>' ] = suberrors <EOL> return suberrors <EOL> def __unicode__ ( self ) : <EOL> return six . u ( '<STR_LIT>' ) . join ( six . text_type ( error ) for error in six . itervalues ( self . errors ) ) <EOL> class ServerError ( ResponseError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InternalServerError ( ServerError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class BadGatewayError ( ServerError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ServiceUnavailableError ( ServerError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnexpectedStatusError ( ResponseError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , status , response_xml ) : <EOL> super ( UnexpectedStatusError , self ) . __init__ ( response_xml ) <EOL> self . status = status <EOL> def __unicode__ ( self ) : <EOL> return six . text_type ( self . status ) <EOL> error_classes = { <EOL> <NUM_LIT> : BadRequestError , <EOL> <NUM_LIT> : UnauthorizedError , <EOL> <NUM_LIT> : PaymentRequiredError , <EOL> <NUM_LIT> : ForbiddenError , <EOL> <NUM_LIT> : NotFoundError , <EOL> <NUM_LIT> : NotAcceptableError , <EOL> <NUM_LIT> : PreconditionFailedError , <EOL> <NUM_LIT> : UnsupportedMediaTypeError , <EOL> <NUM_LIT> : ValidationError , <EOL> <NUM_LIT> : InternalServerError , <EOL> <NUM_LIT> : BadGatewayError , <EOL> <NUM_LIT> : ServiceUnavailableError , <EOL> } <EOL> def error_class_for_http_status ( status ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return error_classes [ status ] <EOL> except KeyError : <EOL> def new_status_error ( xml_response ) : <EOL> return UnexpectedStatusError ( status , xml_response ) <EOL> return new_status_error <EOL> other_errors = [ ConfigurationError ] <EOL> __all__ = [ x . __name__ for x in list ( error_classes . values ( ) ) + other_errors ] </s>
