Table Of Contents

Previous topic

Spyne API Reference

Next topic

Application Definition

This Page

Fundamental Data Structures

MethodContext

class spyne.MethodContext(transport)

Bases: object

The base class for all RPC Contexts. Holds all information about the current state of execution of a remote procedure call.

app = None

The parent application.

aux = None

Auxiliary-method specific context. You can use this to share data between auxiliary sessions. This is not set in primary contexts.

call_end = None

The time the rpc operation was completed in seconds-since-epoch format.

Useful for benchmarking purposes.

call_start = None

The time the rpc operation was initiated in seconds-since-epoch format.

Useful for benchmarking purposes.

descriptor

The :class:MethodDescriptor object representing the current method. It is only set when the incoming request was successfully mapped to a method in the public interface. The contents of this property should not be changed by the user code.

event = None

Event-specific context. Use this as you want, preferably only in events, as you’d probably want to separate the event data from the method data.

frozen = False

When this is set, no new attribute can be added to this class instance. This is mostly for internal use.

function = None

The callable of the user code.

in_body_doc = None

Incoming body document of the request.

in_document = None

Incoming document, what you get when you parse the incoming stream.

in_error = None

Native python error object. If this is set, either there was a parsing error or the incoming document was representing an exception.

in_header = None

Deserialized incoming header – a native object.

in_header_doc = None

Incoming header document of the request.

in_object = None

In the request (i.e. server) case, this contains the function argument sequence for the function in the service definition class. In the response (i.e. client) case, this contains the object returned by the remote procedure call.

It’s always a sequence of objects:
  • [None] when the function has no output (client)/input (server) types.
  • A single-element list that wraps the return value when the function has one return type defined,
  • A tuple of return values in case of the function having more than one return value.

The order of the argument sequence is in line with self.descriptor.in_message._type_info.keys().

in_string = None

Incoming bytestream as a sequence of str or bytes instances.

locale = None

The locale the request will use when needed for things like date formatting, html rendering and such.

method_name

The public name of the method the method_request_string was matched to.

method_request_string = None

This is used to decide which native method to call. It is set by the protocol classes.

out_body_doc = None

Serialized body object.

out_document = None

out_body_doc and out_header_doc wrapped in the outgoing envelope

out_error = None

Native exception thrown by the function in the service definition class.

out_header = None

Native python object set by the function in the service definition class.

out_header_doc = None

Serialized header object.

out_object = None

In the response (i.e. server) case, this contains the native python object(s) returned by the function in the service definition class. In the request (i.e. client) case, this contains the function arguments passed to the function call wrapper.

It’s always a sequence of objects:
  • [None] when the function has no output (server)/input (client) types.
  • A single-element list that wraps the return value when the function has one return type defined,
  • A tuple of return values in case of the function having more than one return value.

The order of the argument sequence is in line with self.descriptor.out_message._type_info.keys().

out_string = None

Outgoing bytestream (i.e. a sequence of strings)

protocol = None

The protocol-specific context. Protocol implementors can use this to their liking.

service_class = None

The service definition class the method belongs to.

transport = None

The transport-specific context. Transport implementors can use this to their liking.

udc = None

The user defined context. Use it to your liking.

MethodDescriptor

class spyne.MethodDescriptor(function, in_message, out_message, doc, is_callback=False, is_async=False, mtom=False, in_header=None, out_header=None, faults=None, port_type=None, no_ctx=False, udp=None, class_key=None, aux=None, patterns=None, body_style=None)

Bases: object

This class represents the method signature of an exposed service. It is produced by the spyne.decorator.srpc() decorator.

aux = None

Value to indicate what kind of auxiliary method this is. (None means primary)

Primary methods block the request as long as they’re running. Their return values are returned to the client. Auxiliary ones execute asyncronously after the primary method returns, and their return values are ignored by the rpc layer.

body_style = None

One of (BODY_STYLE_EMPTY, BODY_STYLE_BARE, BODY_STYLE_WRAPPED).

class_key = None

The identifier of this method in its parent spyne.service.ServiceBase subclass.

doc = None

The function docstring.

faults = None

An iterable of spyne.model.fault.Fault subclasses to denote the types of exceptions that this method can throw.

in_header = None

An iterable of spyne.model.complex.ComplexModel subclasses to denote the types of header objects that this method can accept.

in_message = None

A spyne.model.complex.ComplexModel subclass that defines the input signature of the user function and that was automatically generated by the @srpc decorator.

key

The function identifier in ‘{namespace}name’ form.

name

The public name of the function. Equals to the type_name of the in_message.

no_ctx = None

no_ctx: Boolean flag to denote whether the user code gets an implicit spyne.MethodContext instance as first argument.

out_header = None

An iterable of spyne.model.complex.ComplexModel subclasses to denote the types of header objects that this method can emit along with its return value.

out_message = None

A spyne.model.complex.ComplexModel subclass that defines the output signature of the user function and that was automatically generated by the @srpc decorator.

patterns = None

This list stores patterns which will match this callable using various elements of the request protocol.

Currently, the only object supported here is the spyne.protocol.http.HttpPattern object.

udp = None

Short for “User Defined Properties”, this is just an arbitrary python object set by the user to pass arbitrary metadata via the @srpc decorator.

EventManager

class spyne.EventManager(parent, handlers={})

Spyne supports a simple event system that can be used to have repetitive boiler plate code that has to run for every method call nicely tucked away in one or more event handlers. The popular use-cases include things like database transaction management, logging and measuring performance.

Various Spyne components support firing events at various stages during the processing of the request, which are documented in the relevant classes.

The classes that support events are:

The events are stored in an ordered set. This means that the events are ran in the order they were added and adding a handler twice does not cause it to run twice.

add_listener(event_name, handler)

Register a handler for the given event name.

Parameters:
  • event_name – The event identifier, indicated by the documentation. Usually, this is a string.
  • handler – A static python function that receives a single MethodContext argument.
fire_event(event_name, ctx)

Run all the handlers for a given event name.

Parameters:
  • event_name – The event identifier, indicated by the documentation. Usually, this is a string.
  • handler – The method context. Event-related data is conventionally stored in ctx.event attribute.