HTTP Endpoint

class octopus_sensing.device_message_endpoint.DeviceMessageHTTPEndpoint(device_coordinator: octopus_sensing.device_coordinator.DeviceCoordinator, port: int = 9331)[source]

Bases: octopus_sensing.common.endpoint_base.EndpointBase

Stars and endpoint that listens for incoming Message requests. It passes the message to the Device Coordinator to dispatch them to the devices. It accepts HTTP POST requests. The Body can be serialized in one of ‘json’, ‘msgpack’ or ‘pickle’. See the Usage section for how to call the endpoint.

Parameters
  • device_coordinator – An instance of DeviceCoordinator class.

  • port – Port to listen on. Default is: 9331

Examples

To start the endpoint:

>>> from octopus_sensing.device_coordinator import DeviceCoordinator
>>> from octopus_sensing.device_message_endpoint import DeviceMessageHTTPEndpoint
>>> device_coordinator = DeviceCoordinator()
>>> # Add you devices
>>> message_endpoint = DeviceMessageHTTPEndpoint(device_coordinator)
>>> message_endpoint.start()
>>> # You need to stop it after your program finished.
>>> message_endpoint.stop()

The client can call the endpoint like this (Note that it’s a separate process):

>>> import http.client
>>> import msgpack
>>> http_client = http.client.HTTPConnection("127.0.0.1:9331", timeout=3)
>>> http_client.request(
...     "POST", "/",
...    body=msgpack.packb({'type': 'START',
...                        'experiment_id': '123',
...                        'stimulus_id': 's8'}),
...                        headers={'Accept': 'application/msgpack'})
>>> response = http_client.getresponse()
>>> assert response.status == 200

Note that you must pass ‘Accept’ in the headers, so the endpoint knows what type of serialization you used. Also, the request needs ‘Content-Length’ in headers. However, it’s part of the HTTP standard and your http client should do it automatically.