charms.reactive.patterns

BaseRequest Base class for requests using the request / response pattern.
BaseResponse Base class for responses using the request / response pattern.
Field Defines a Field property for a Request or Response object.
FieldFinders Metaclass for defining response_by_FIELD methods on RequesterEndpoint classes.
FieldHolderDictProxy Base class for field holders that makes it act like a dict for easy serialization.
RequesterEndpoint Base class for Endpoints that create requests in the request / response pattern.
ResponderEndpoint Base class for Endpoints that respond to requests in the request / response pattern.
SetNameBackport Metaclass to backport the __set_name__ behavior for data descriptors, which was added in Python 3.6, to earlier versions.

Reference

class charms.reactive.patterns.request_response.BaseRequest(source, request_id)

Bases: charms.reactive.patterns.request_response.FieldHolderDictProxy

Base class for requests using the request / response pattern.

Subclasses must set the RESPONSE_CLASS attribute to a subclass of the BaseResponse which defines the fields that the response will use. They must also define additional attributes as :class:`Field`s.

For example:

class TLSResponse(BaseResponse):
    key = Field('Private key for the cert')
    cert = Field('Public cert info')


class TLSRequest(BaseRequest):
    RESPONSE_CLASS = TLSResponse

    common_name = Field('Common Name (CN) for the cert to be created')
    sans = Field('List of Subject Alternative Names (SANs)')
RESPONSE_CLASS = None
classmethod create(relation, **fields)

Create a new request.

Fields and their values can be passed in to pre-populate the request as keyword arguments, or can be set individually on the resulting request.

classmethod create_or_update(match_fields, relation, **fields)

Find a request and update it, or create a new one.

If multiple requests match, only the first one is updated.

Parameters:
  • match_fields – List of the field names to match by.
  • relation – Relation to find or create the request on.
  • **fields

    Name / value pairs to match by and update to.

Returns:

The new or updated request.

Example:

for relation in self.relations:
    JobRequest.create_or_update(match_fields=['job_name'],
                                relation=relation,
                                job_name='foo',
                                job_data=job_data)
create_response(**fields)

Create a response to this request.

Fields and their values can be passed in to pre-populate the response as keyword arguments, or can be set individually on the resulting response.

Returns the response object (which can also be accessed as request.response).

egress_subnets

Subnets over which network traffic to the requester will flow.

classmethod find(relation=None, **fields)

Find the first request whose fields match the given values.

Parameters:
  • relation – If given, look for the request on a specific relation.
  • **fields

    Name / value pairs to match by.

classmethod find_all(**fields)

Find all requests whose fields match the given values.

Parameters:**fields

Name / value pairs to match by.

classmethod get(request_id)

Get a specific request by ID.

classmethod get_all()

Get a list of all requests (in order of their ID).

ingress_address

Address to use if a connection to the requester is required.

is_created

Whether this request was created by this side of the relation.

is_received

Whether this request was received by the other side of the relation.

request_id

UUID for this request. Will be automatically generated.

respond(**fields)

Respond to this request. (Alias of create_response().)

Fields and their values can be passed in to pre-populate the response as keyword arguments, or can be set individually on the resulting response.

Returns the response object (which can also be accessed as request.response).

class charms.reactive.patterns.request_response.BaseResponse(request)

Bases: charms.reactive.patterns.request_response.FieldHolderDictProxy

Base class for responses using the request / response pattern.

classmethod create(request, **fields)

Create a response to the given request.

Fields and their values can be passed in to pre-populate the response as keyword arguments, or can be set individually on the resulting response.

is_received
class charms.reactive.patterns.request_response.Field(description)

Bases: property

Defines a Field property for a Request or Response object.

Can be set or retrieved like a normal attribute, and will be automatically serialized over the relation using JSON.

class charms.reactive.patterns.request_response.FieldFinders(name, bases, namespace)

Bases: type

Metaclass for defining response_by_FIELD methods on RequesterEndpoint classes.

This is done as a metaclass to handle fields on derived classes but to still allow the methods to show up in the class’s documentation.

class charms.reactive.patterns.request_response.FieldHolderDictProxy

Bases: dict

Base class for field holders that makes it act like a dict for easy serialization.

class charms.reactive.patterns.request_response.RequesterEndpoint(*args, **kwargs)

Bases: charms.reactive.endpoints.Endpoint

Base class for Endpoints that create requests in the request / response pattern.

Subclasses must set the REQUEST_CLASS attribute to a subclass of BaseRequest which defines the fields the request will use.

Will automatically manage the following flags:

  • endpoint.{endpoint_name}.has_responses Set if any responses are available
  • endpoint.{endpoint_name}.all_responses Set if all requests have responses.
REQUEST_CLASS = None
requests

A list of all requests which have been submitted.

response_by_field(relation=None, **fields)

Find a response by the value(s) of fields on its request.

Parameters:
  • relation – If given, limit the search to that relation.
  • **fields

    Name / value pairs to match by.

responses

A list of all responses which have been received.

class charms.reactive.patterns.request_response.ResponderEndpoint(*args, **kwargs)

Bases: charms.reactive.endpoints.Endpoint

Base class for Endpoints that respond to requests in the request / response pattern.

Subclasses must set the REQUEST_CLASS attribute to a subclass of BaseRequest which defines the fields the request will use.

Will automatically manage the following flags:

  • endpoint.{endpoint_name}.has_requests Set if any requests are available
  • endpoint.{endpoint_name}.new_requests Set if any unhandled requests are available.
REQUEST_CLASS = None
all_requests

A list of all requests, including ones which have been responded to.

new_requests

A list of requests which have not been responded.

Requests should be handled by the charm and then responded to by calling request.respond(...).

class charms.reactive.patterns.request_response.SetNameBackport

Bases: type

Metaclass to backport the __set_name__ behavior for data descriptors, which was added in Python 3.6, to earlier versions.