dxlclient.service module¶
Classes used for registering and exposing services to a DXL fabric.
-
class
dxlclient.service.
ServiceRegistrationInfo
(client, service_type)¶ Bases:
dxlclient._BaseObject
Service Registration instances are used to register and expose services onto a DXL fabric.
DXL Services are exposed to the DXL fabric and are invoked in a fashion similar to RESTful web services. Communication between an invoking client and the DXL service is one-to-one (request/response).
Each service is identified by the "topics" it responds to. Each of these "topics" can be thought of as a method that is being "invoked" on the service by the remote client.
Multiple service "instances" can be registered with the DXL fabric that respond to the same "topics". When this occurs (unless explicitly overridden by the client) the fabric will select the particular instance to route the request to (by default round-robin). Multiple service instances can be used to increase scalability and fault-tolerance.
The following demonstrates registering a service that responds to a single topic with the DXL fabric:
from dxlclient.callbacks import RequestCallback from dxlclient.message import Response from dxlclient.service import ServiceRegistrationInfo class MyRequestCallback(RequestCallback): def on_request(self, request): # Extract information from request print request.payload.decode() # Create the response message res = Response(request) # Populate the response payload res.payload = "pong".encode() # Send the response dxl_client.send_response(res) # Create service registration object info = ServiceRegistrationInfo(dxl_client, "/mycompany/myservice") # Add a topic for the service to respond to info.add_topic("/testservice/testrequesttopic", MyRequestCallback()) # Register the service with the fabric (wait up to 10 seconds for registration to complete) dxl_client.register_service_sync(info, 10)
The following demonstrates a client that is invoking the service in the example above:
from dxlclient.message import Request, Message # Create the request message req = Request("/testservice/testrequesttopic") # Populate the request payload req.payload = "ping".encode() # Send the request and wait for a response (synchronous) res = dxl_client.sync_request(req) # Extract information from the response (if an error did not occur) if res.message_type != Message.MESSAGE_TYPE_ERROR: print res.payload.decode()
Constructor parameters:
Parameters: - client -- The
dxlclient.client.DxlClient
instance that will expose this service - service_type -- A textual name for the service. For example, "/mycompany/myservice"
-
add_topic
(topic, callback)¶ Registers a topic for the service to respond to along with the
dxlclient.callbacks.RequestCallback
that will be invoked.Parameters: - topic -- The topic for the service to respond to
- callback -- The
dxlclient.callbacks.RequestCallback
that will be invoked when adxlclient.message.Request
message is received
-
add_topics
(callbacks_by_topic)¶ Registers a set of topics for the service to respond to along with their associated
dxlclient.callbacks.RequestCallback
instances as a dictionaryParameters: callbacks_by_topic -- Dictionary containing a set of topics for the service to respond to along with their associated dxlclient.callbacks.RequestCallback
instances
-
destination_tenant_guids
¶ The set of tenant identifiers that the service will be available to. Setting this value will limit which tenants can invoke the service.
-
metadata
¶ A dictionary of name-value pairs that are sent as part of the service registration. Brokers provide a registry service that allows for registered services and their associated meta-information to be inspected. The metadata is typically used to include information such as the versions for products that are exposing DXL services, etc.
-
service_id
¶ A unique identifier for the service instance (automatically generated when the
ServiceRegistrationInfo
object is constructed)
-
service_type
¶ A textual name for the service. For example, "/mycompany/myservice"
-
topics
¶ Returns a tuple containing the topics that the service responds to
-
ttl
¶ The interval (in minutes) at which the client will automatically re-register the service with the DXL fabric (defaults to 60 minutes)
- client -- The