def _fault_to_doc(self, inst, cls=None): if cls is None: cls = Fault if self.complex_as is list: return [cls.to_list(inst.__class__, inst, self)] elif self.complex_as is tuple: fault_as_list = [Fault.to_list(inst.__class__, inst, self)] return tuple(fault_as_list) else: return [Fault.to_dict(inst.__class__, inst, self)]
def serialize(self, ctx, message): assert message in (self.REQUEST, self.RESPONSE) self.event_manager.fire_event('before_serialize', ctx) if ctx.out_error is not None: ctx.out_document = [ Fault.to_dict(ctx.out_error.__class__, ctx.out_error) ] else: # get the result message if message is self.REQUEST: out_type = ctx.descriptor.in_message elif message is self.RESPONSE: out_type = ctx.descriptor.out_message if out_type is None: return out_type_info = out_type._type_info # instantiate the result message out_instance = out_type() # assign raw result to its wrapper, result_message for i, (k, v) in enumerate(out_type_info.items()): attr_name = k out_instance._safe_set(attr_name, ctx.out_object[i], v) ctx.out_document = self._object_to_doc(out_type, out_instance), self.event_manager.fire_event('after_serialize', ctx)
def decompose_incoming_envelope(self, ctx, message): """Sets ``ctx.in_body_doc``, ``ctx.in_header_doc`` and ``ctx.method_request_string`` using ``ctx.in_document``. """ assert message in (ProtocolBase.REQUEST, ProtocolBase.RESPONSE) # set ctx.in_header ctx.transport.in_header_doc = None # use an rpc protocol if you want headers. doc = ctx.in_document ctx.in_header_doc = None ctx.in_body_doc = doc if len(doc) == 0: raise Fault("Client", "Empty request") logger.debug('\theader : %r' % (ctx.in_header_doc)) logger.debug('\tbody : %r' % (ctx.in_body_doc)) if not isinstance(doc, dict) or len(doc) != 1: raise ValidationError("Need a dictionary with exactly one key " "as method name.") mrs, = doc.keys() ctx.method_request_string = '{%s}%s' % (self.app.interface.get_tns(), mrs)
def serialize(self, ctx, message): assert message in (self.REQUEST, self.RESPONSE) self.event_manager.fire_event('before_serialize', ctx) if ctx.out_error is not None: ctx.out_document = [Fault.to_dict(ctx.out_error.__class__, ctx.out_error)] else: # get the result message if message is self.REQUEST: out_type = ctx.descriptor.in_message elif message is self.RESPONSE: out_type = ctx.descriptor.out_message if out_type is None: return out_type_info = out_type._type_info # instantiate the result message out_instance = out_type() # assign raw result to its wrapper, result_message for i, (k, v) in enumerate(out_type_info.items()): attr_name = k out_instance._safe_set(attr_name, ctx.out_object[i], v) ctx.out_document = self._object_to_doc(out_type, out_instance), self.event_manager.fire_event('after_serialize', ctx)
def serialize(self, ctx, message): assert message in (self.REQUEST, self.RESPONSE) self.event_manager.fire_event('before_serialize', ctx) if ctx.out_error is not None: ctx.out_document = [ Fault.to_dict(ctx.out_error.__class__, ctx.out_error) ] return # get the result message if message is self.REQUEST: out_type = ctx.descriptor.in_message elif message is self.RESPONSE: out_type = ctx.descriptor.out_message else: assert False if out_type is None: return # assign raw result to its wrapper, result_message if ctx.descriptor.is_out_bare(): out_instance, = ctx.out_object else: out_type_info = out_type.get_flat_type_info(out_type) # instantiate the result message out_instance = out_type() for i, (k, v) in enumerate(out_type_info.items()): attrs = self.get_cls_attrs(v) out_instance._safe_set(k, ctx.out_object[i], v, attrs) ctx.out_document = self._object_to_doc(out_type, out_instance, set()), logger.debug("Retval: %r", ctx.out_document) self.event_manager.fire_event('after_serialize', ctx)
def serialize(self, ctx, message): assert message in (self.REQUEST, self.RESPONSE) self.event_manager.fire_event('before_serialize', ctx) if ctx.out_error is not None: ctx.out_document = [Fault.to_dict(ctx.out_error.__class__, ctx.out_error)] return # get the result message if message is self.REQUEST: out_type = ctx.descriptor.in_message elif message is self.RESPONSE: out_type = ctx.descriptor.out_message else: assert False if out_type is None: return # assign raw result to its wrapper, result_message if ctx.descriptor.is_out_bare(): out_instance, = ctx.out_object else: out_type_info = out_type.get_flat_type_info(out_type) # instantiate the result message out_instance = out_type() for i, (k, v) in enumerate(out_type_info.items()): attrs = self.get_cls_attrs(v) out_instance._safe_set(k, ctx.out_object[i], v, attrs) ctx.out_document = self._object_to_doc(out_type, out_instance, set()), logger.debug("Retval: %r", ctx.out_document) self.event_manager.fire_event('after_serialize', ctx)
def process_contexts(self, contexts): p_ctx, others = contexts[0], contexts[1:] if p_ctx.in_error: return self.handle_error(p_ctx, others, p_ctx.in_error) self.get_in_object(p_ctx) if p_ctx.in_error: logger.error(p_ctx.in_error) return self.handle_error(p_ctx, others, p_ctx.in_error) self.get_out_object(p_ctx) if p_ctx.out_error: return self.handle_error(p_ctx, others, p_ctx.out_error) try: self.get_out_string(p_ctx) except Exception as e: logger.exception(e) contexts.out_error = Fault('Server', "Internal serialization Error.") return self.handle_error(contexts, others, contexts.out_error)
def test_fault_detail_as_dict(self): elt = get_object_as_xml(Fault(detail={"this": "that"}), Fault) eltstr = etree.tostring(elt) print(eltstr) assert b'<detail><this>that</this></detail>' in eltstr
def client_fault(ctx): raise Fault("Client", "zzzz...")
def server_fault(ctx): raise Fault("Server", "boo and you know it!")
def __init__(self, faultstring): Fault.__init__(self, 'Client.SchemaValidationError', faultstring)