disco — Service discovery support (XEP-0030)

This module provides support for XEP-0030 (Service Discovery). For this, it provides a Service subclass which can be loaded into a client using AbstractClient.summon().

Service

class aioxmpp.disco.Service(client, *, logger=None)

A service implementing XEP-0030. The service provides methods for managing the own features and identities as well as querying others features and identities.

Querying other entities’ service discovery information:

coroutine query_info(jid, *, node=None, require_fresh=False, timeout=None)

Query the features and identities of the specified entity. The entity is identified by the jid and the optional node.

Return the xso.InfoQuery instance returned by the peer. If an error is returned, that error is raised as errors.XMPPError.

The requests are cached. This means that only one request is ever fired for a given target (identified by the jid and the node). The request is re-used for all subsequent requests to that identity.

If require_fresh is set to true, the above does not hold and a fresh request is always created. The new request is the request which will be used as alias for subsequent requests to the same identity.

The visible effects of this are twofold:

  • Caching: Results of requests are implicitly cached
  • Aliasing: Two concurrent requests will be aliased to one request to save computing resources

Both can be turned off by using require_fresh. In general, you should not need to use require_fresh, as all requests are implicitly cancelled whenever the underlying session gets destroyed.

timeout is passed to StanzaStream.send_iq_and_wait_for_reply().

Services inherit from Node to manage the identities and features of the JID itself. The identities and features declared in the service using the Node interface on the Service instance are returned when a query is received for the JID with an empty or unset node attribute. For completeness, the relevant methods are listed here. Refer to the Node documentation for details.

Node.register_feature(var) Register a feature with the namespace variable var.
Node.unregister_feature(var) Unregister a feature which has previously been registered using register_feature().
Node.register_identity(category, type_, *[, ...]) Register an identity with the given category and type_.
Node.unregister_identity(category, type_) Unregister an identity previously registered using register_identity().

Note

Upon construction, the Service adds a default identity with category "client" and type "bot" to the root Node. This is to comply with XEP-0030 of always having an identity and not being forced to reply with <feature-not-implemented/> or a similar error.

After having added another identity, that default identity can be removed.

Usage example, assuming that you have a node.AbstractClient node:

import aioxmpp.disco as disco
# load service into node
sd = node.summon(disco.Service)

# retrieve server information
server_info = yield from sd.query_info(
    node.local_jid.replace(localpart=None, resource=None)
)

# retrieve resources
resources = yield from sd.query_items(
    node.local_jid.bare()
)

Entity information

class aioxmpp.disco.Node

A Node holds the information related to a specific node within the entity referred to by a JID, with respect to XEP-0030 semantics.

A Node always has at least one identity (or it will return as <item-not-found/>). It may have zero or more features beyond the XEP-0030 features which are statically included.

To manage the identities and the features of a node, use the following methods:

register_feature(var)

Register a feature with the namespace variable var.

If the feature is already registered or part of the default XEP-0030 features, a ValueError is raised.

unregister_feature(var)

Unregister a feature which has previously been registered using register_feature().

If the feature has not been registered previously, KeyError is raised.

Note

The features which are mandatory per XEP-0030 are always registered and cannot be unregistered. For the purpose of unregistration, they behave as if they had never been registered; for the purpose of registration, they behave as if they had been registered before.

register_identity(category, type_, *, names={})

Register an identity with the given category and type_.

If there is already a registered identity with the same category and type_, ValueError is raised.

names may be a mapping which maps structs.LanguageTag instances to strings. This mapping will be used to produce <identity/> declarations with the respective xml:lang and name attributes.

unregister_identity(category, type_)

Unregister an identity previously registered using register_identity().

If no identity with the given category and type_ has been registered before, KeyError is raised.

If the identity to remove is the last identity of the Node, ValueError is raised; a node must always have at least one identity.

To access the declared features and identities, use:

iter_features()

Return an iterator which yields the var values of each feature declared in this Node, including the statically declared XEP-0030 features.

iter_identities()

Return an iterator which yields tuples consisting of the category, the type, the language code and the name of each identity declared in this Node.

Both the language code and the name may be None, if no names or a name without language code have been declared.

To access items, use:

iter_items()

Return an iterator which yields the xso.Item objects which this node holds.

A bare Node cannot hold any items and will thus return an iterator which does not yield any element.

As mentioned, bare Node objects have no items; there are subclasses of Node which support items:

StaticNode Support for a list of xso.Item instances
Service Support for “mountpoints” for node subtrees
class aioxmpp.disco.StaticNode

A StaticNode is a Node with a non-dynamic set of items.

items

A list of xso.Item instances. These items will be returned when the node is queried for it’s XEP-0030 items.

It is the responsibility of the user to ensure that the set of items is valid. This includes avoiding duplicate items.

disco.xso — IQ payloads

The submodule aioxmpp.disco.xso contains the XSO classes which describe the IQ payloads used by this subpackage.

You will encounter some of these in return values, but there should never be a need to construct them by yourself; the Service handles it all.

Information queries

class aioxmpp.disco.xso.InfoQuery(*[, identities][, features][, node])[source]

A query for features and identities of an entity. The keyword arguments to the constructor can be used to initialize the attributes. Note that identities and features must be iterables of Identity and Feature, respectively; these iterables are evaluated and the items are stored in the respective attributes.

node

The node at which the query is directed.

identities

The identities of the entity, as Identity instances. Each entity has at least one identity.

features

The features of the entity, as Feature instances.

class aioxmpp.disco.xso.Feature(*[, var])[source]

A feature declaration. The keyword argument to the constructor can be used to initialize the attribute of the Feature instance.

var

The namespace which identifies the feature.

class aioxmpp.disco.xso.Identity(*[, category][, type_][, name][, lang])[source]

An identity declaration. The keyword arguments to the constructor can be used to initialize attributes of the Identity instance.

category

The category of the identity. The value is not validated against the values in the registry.

type_

The type of the identity. The value is not validated against the values in the registry.

name

The optional human-readable name of the identity. See also the lang attribute.

lang

The language of the name. This may be not None even if name is not set due to xml:lang propagation.

Item queries

class aioxmpp.disco.xso.ItemsQuery(*[, node][, items])[source]

A query for items at a specific entity. The keyword arguments to the constructor can be used to initialize the attributes of the ItemsQuery. Note that items must be an iterable of Item instances. The iterable will be evaluated and the items will be stored in the items attribute.

node

Node at which the query is directed

items

The items at the addressed entity.

class aioxmpp.disco.xso.Item(*[, jid][, name][, node])[source]

An item declaration. The keyword arguments to the constructor can be used to initialize the attributes of the Item instance.

jid

JID of the entity represented by the item.

node

Node of the item

name

Name of the item