def parse_soap_fault(body, expected_faults=None): """ Parse a SOAP fault element and its details. :param body: SOAP ``Body`` element. :param expected_faults: A `list` of `SoapFault` subclasses whose ``detail_type`` attribute will be used to determine a match against each top-level SOAP fault detail element. :return: A 2-tuple of: matching exception type and an instance of it's ``detail_type``; and SOAP fault information (code, string, actor, detail). ``None`` if there is no SOAP fault. """ fault = elemfind(body, SOAP_ENV.Fault) if fault is None: return None faultcode = gettext(fault, u'faultcode') faultstring = gettext(fault, u'faultstring') faultactor = gettext(fault, u'faultactor') detail = elemfind(fault, u'detail') if expected_faults is None: expected_faults = [] parsed = _parse_expected_faults(detail, expected_faults) return parsed, (faultcode, faultstring, faultactor, detail)
def from_element(cls, element): if element.tag != cls.tag.text: return None return cls( message_id=gettext(element, 'messageId'), text=gettext(element, 'text'), variables=list(gettextall(element, 'variables')))
def test_parse(self): """ Specifying a `parse` callable results in that being called to transform the element text. """ self.assertEqual(42, gettext(self.root, u'b', parse=int)) self.assertEqual( MetasyntacticVariables.Foo, gettext(self.root, u'c', parse=MetasyntacticVariables.lookupByName)) self.assertRaises(ValueError, gettext, self.root, u'c', parse=int)
def from_element(cls, root): """ Create an `SmsMessage` instance from an ElementTree element. """ return cls( message=gettext(root, 'message'), sender_address=gettext( root, 'senderAddress', parse=normalize_address), service_activation_number=gettext( root, 'smsServiceActivationNumber', parse=normalize_address), timestamp=gettext(root, 'dateTime', parse=iso8601.parse_date))
def test_simple(self): """ Getting a sub-element with a `text` attribute returns the text as a `unicode` object. """ res = gettext(self.root, u'a') self.assertIdentical(unicode, type(res)) self.assertEqual(res, u'hello') res = gettext(self.root, u'sub/e') self.assertIdentical(unicode, type(res)) self.assertEqual(res, u'world')
def test_parseWithDefault(self): """ In the event that a default value is specified and a `parse` callable given, and the default value is used, the default value will be passed to the callable. """ self.assertEqual( 42, gettext(self.root, u'b', default=3.1415, parse=int)) self.assertEqual( 21, gettext(self.root, u'd', default=21, parse=int)) self.assertRaises(ValueError, gettext, self.root, u'd', default='foo', parse=int)
def test_parse(self): """ Specifying a `parse` callable results in that being called to transform the element text. """ self.assertEqual( 42, gettext(self.root, u'b', parse=int)) self.assertEqual( MetasyntacticVariables.Foo, gettext(self.root, u'c', parse=MetasyntacticVariables.lookupByName)) self.assertRaises(ValueError, gettext, self.root, u'c', parse=int)
def process_notifySmsReception(self, root, header, name): """ Process a received text message. """ linkid = None if header is not None: linkid = gettext(header, './/' + str(PARLAYX_COMMON_NS.linkid)) correlator = gettext(root, NOTIFICATION_NS.correlator) message = SmsMessage.from_element( elemfind(root, NOTIFICATION_NS.message)) d = maybeDeferred( self.callback_message_received, correlator, linkid, message) d.addCallback( lambda ignored: NOTIFICATION_NS.notifySmsReceptionResponse()) return d
def test_parseWithDefault(self): """ In the event that a default value is specified and a `parse` callable given, and the default value is used, the default value will be passed to the callable. """ self.assertEqual(42, gettext(self.root, u'b', default=3.1415, parse=int)) self.assertEqual(21, gettext(self.root, u'd', default=21, parse=int)) self.assertRaises(ValueError, gettext, self.root, u'd', default='foo', parse=int)
def from_element(cls, root): """ Create a `DeliveryInformation` instance from an ElementTree element. """ try: delivery_status = gettext( root, 'deliveryStatus', parse=DeliveryStatus.lookupByName) except ValueError, e: raise ValueError( 'No such delivery status enumeration value: %r' % (str(e),))
def process_notifySmsDeliveryReceipt(self, root, header, name): """ Process a text message delivery receipt. """ correlator = gettext(root, NOTIFICATION_NS.correlator) delivery_info = DeliveryInformation.from_element( elemfind(root, NOTIFICATION_NS.deliveryStatus)) d = maybeDeferred(self.callback_message_delivered, correlator, delivery_info.delivery_status.value) d.addCallback( lambda ignored: NOTIFICATION_NS.notifySmsDeliveryReceiptResponse()) return d
def test_default(self): """ Getting a sub-element without a `text` attribute, or attempting to get a sub-element that does not exist, results in the `default` parameter to `gettext` being used, defaulting to `None`. """ self.assertIdentical(gettext(self.root, u'd'), None) self.assertEqual(gettext(self.root, u'd', default=42), 42) self.assertIdentical(gettext(self.root, u'sub/f'), None) res = gettext(self.root, u'sub/f', default='a') self.assertIdentical(str, type(res)) self.assertEqual(res, 'a') self.assertIdentical(gettext(self.root, u'haha_what'), None) self.assertEqual(gettext(self.root, u'haha_what', default=42), 42)
def from_element(cls, elem): if elem.tag != L.ToyFaultDetail.text: return None return cls(gettext(elem, 'foo'), gettext(elem, 'bar'))
def _extractRequestIdentifier((body, header)): return gettext(body, './/' + str(SEND_NS.result), default='')
def from_element(cls, element): if element.tag != cls.tag.text: return None return cls(message_id=gettext(element, 'messageId'), text=gettext(element, 'text'), variables=list(gettextall(element, 'variables')))
ParlayX `DeliveryInformation` complex type. """ @classmethod def from_element(cls, root): """ Create a `DeliveryInformation` instance from an ElementTree element. """ try: delivery_status = gettext( root, 'deliveryStatus', parse=DeliveryStatus.lookupByName) except ValueError, e: raise ValueError( 'No such delivery status enumeration value: %r' % (str(e),)) else: return cls( address=gettext(root, 'address', parse=normalize_address), delivery_status=delivery_status) class SmsNotificationService(Resource): """ Web resource to handle SOAP requests for ParlayX SMS deliveries and delivery receipts. """ isLeaf = True def __init__(self, callback_message_received, callback_message_delivered): self.callback_message_received = callback_message_received self.callback_message_delivered = callback_message_delivered Resource.__init__(self)