Web API
The Dallinger API allows the experiment frontend to communicate with the
backend. Many of these routes correspond to specific functions of
Dallinger’s classes, particularly
dallinger.models.Node. For example,
nodes have a connect method that creates new vectors between nodes
and there is a corresponding connect/ route that allows the frontend
to call this method.
Miscellaneous routes
GET /ad_address/<mode>/<hit_id>
Used to get the address of the experiment on the gunicorn server and to return
participants to Mechanical Turk upon completion of the experiment. This route
is pinged automatically by the function submitAssignment in dallinger2.js.
GET /<directory>/<page>
Returns the html page with the name <page> from the directory called
<directory>.
GET /summary
Returns a summary of the statuses of Participants.
GET /<page>
Returns the html page with the name <page>.
GET /chat?channel=<channel>&worker_id=<worker_id>&participant_id=<participant_id>&scope=<scope>
Opens a WebSocket channel that subscribes the client to all messages sent to the
channel named <channel>. Messages are relayed as soon as they arrive. For
more information see
Using WebSockets in Dallinger Experiments. In the
browser, dallinger.openChatSocket opens this connection.
Clients may prefix outgoing messages with any channel name except
dallinger_control, which is reserved for the server’s own connection and
subscription events, dallinger_direct, which is reserved for directed
messages, dallinger_realtime_session, which carries the views of pages in
real-time sessions and tells pages when their sessions start, and
dallinger_time, which carries their clock measurements. Messages a client
addresses to any of the first three are discarded. The server handles those on
dallinger_time itself, for a connection in a real-time session, and drops
them from any other. A channel argument naming any of the four is refused.
A connection which supplies a participant_id also receives any message sent
to that participant with Experiment.publish_to_participants, prefixed with
the dallinger_direct channel name, whether or not it subscribed to a
channel of its own. Such a connection goes through the same participant check
as an experiment socket, described below, and is closed with 1008 or
1013 in the same cases. A connection without a participant_id isn’t
checked.
tolerance is accepted and ignored; it once capped delivery lag. Sending it
raises no error and no deprecation warning, so existing client code needs no
change.
channel is optional: a connection without one receives nothing, and can
still send. scope is optional too, reported unaltered in the client
payload of every control channel event. worker_id is reported there too,
unverified, and only for a connection with a participant_id. A binary
frame closes the connection with code 1003.
GET /experiment-socket?channel=<channel>&worker_id=<worker_id>&participant_id=<participant_id>&scope=<scope>
Opens a WebSocket that passes each incoming message to the experiment’s
handle_websocket_message method, on the web process holding the socket,
instead of publishing it to the named channel. No other subscriber sees the
message. Everything else matches /chat, including the reserved channel
names, the directed messages a connection supplying a participant_id
receives, and the optional channel the connection still receives broadcasts
on. In the browser, dallinger.openExperimentSocket opens this connection.
An experiment socket must carry a participant_id that exists, and the
experiment’s accept_websocket_connection method must accept it, or the
server closes the connection with code 1008. If the participant lookup
raises, the experiment class cannot be built, or accept_websocket_connection
raises, the server closes with 1013 instead, which means the client should
try again later.
scope is passed to handle_websocket_message as well as being reported in
control channel events.
Either route also takes a session argument, the id of a real-time session
for the connection to join, as Real-Time Games
describes. A connection that names a session must also carry a
participant_id of one of the session’s players, or the server closes it
with code 1008, and with 1013 if the session can’t be looked up. Frames
it sends on the session’s game channel go to the session, and to nothing else.
An experiment socket can take a wait_for_game argument instead, naming the
channel of one of the experiment’s game types, for a page that doesn’t know its
session’s id yet. The connection is sent the id of the newest running session of
that game type with the participant as a player, as soon as it connects if there
is one, or else when one starts. For a participant who plays several sessions of
the game type, one after another, an after_session argument naming the
session the page already has makes only newer sessions count. A
wait_for_game that isn’t a game type’s channel, or that comes with a
session, closes the connection with code 1008, as does one on /chat
or an after_session that isn’t a whole number, and a failed lookup closes it
with 1013. In the browser, dallinger.waitForRealtimeSession opens this
connection.
Experiment routes
GET /experiment/<property>
Returns the value of the requested property as a JSON <property>.
The property must be a key in the experiment.public_properties
mapping and be JSON serializable. Experiments have no public properties
by default.
GET /info/<node_id>/<info_id>
Returns a JSON description of the requested info as info.
node_id must be specified to ensure the requesting node has access
to the requested info. Calls experiment method
`info_get_request(node, info).
POST /info/<node_id>
Create an info with its origin set to the specified node. contents
must be passed as data. info_type can be passed as data and will
cause the info to be of the specified type. Also calls experiment method
info_post_request(node, info).
If the specified node is failed then this will fail unless failed is
also passed with the value True. This will create a failed Info on the node.
POST /launch
Initializes the experiment and opens recruitment. This route is
automatically pinged by Dallinger. Because it runs the experiment’s setup and
can open recruitment (publishing a study and spending money), it requires the
dashboard credentials (dashboard_user and dashboard_password) with
HTTP Basic authentication, and answers 403 without them. Dallinger’s
deploy commands send them for you. To launch by hand, for example:
curl -X POST -u "admin:<dashboard password>" https://<app host>/launch
The route also answers 403, even with the credentials, to a request that a
browser sends from another site’s page, which it detects from the
Sec-Fetch-Site or Origin header. Browsers attach saved HTTP Basic
credentials to cross-site form posts, so without this check another page could
launch the experiment. Clients such as curl and requests send neither
header and are not affected.
GET /network/<network_id>
Returns a JSON description of the requested network as network.
POST /node/<node_id>/connect/<other_node_id>
Create vector(s) between the node and other_node by calling
node.connect(whom=other_node). Direction can be passed as data and
will be forwarded as an argument. Calls experiment method
vector_post_request(node, vectors). Returns a list of JSON
descriptions of the created vectors as vectors.
GET /node/<node_id>/infos
Returns a list of JSON descriptions of the infos created by the node as
infos. Infos are identified by calling node.infos().
info_type can be passed as data and will be forwarded as an
argument. Requesting node and the list of infos are also passed to
experiment method info_get_request(node, infos).
GET /node/<node_id>/neighbors
Returns a list of JSON descriptions of the node’s neighbors as
nodes. Neighbors are identified by calling node.neighbors().
node_type and connection can be passed as data and will be
forwarded as arguments. Requesting node and list of neighbors are also
passed to experiment method node_get_request(node, nodes).
GET /node/<node_id>/received_infos
Returns a list of JSON descriptions of the infos sent to the node as
infos. Infos are identified by calling node.received_infos().
info_type can be passed as data and will be forwarded as an
argument. Requesting node and the list of infos are also passed to
experiment method info_get_request(node, infos).
GET /node/<int:node_id>/transformations
Returns a list of JSON descriptions of all the transformations of a node
identified using node.transformations(). The node id must be
specified in the url. You can also pass transformation_type as data
and it will be forwarded to node.transformations() as the argument
type.
GET /node/<node_id>/transmissions
Returns a list of JSON descriptions of the transmissions sent to/from
the node as transmissions. Transmissions are identified by calling
node.transmissions(). direction and status can be passed as
data and will be forwarded as arguments. Requesting node and the list of
transmissions are also passed to experiment method
transmission_get_request(node, transmissions).
POST /node/<node_id>/transmit
Transmit to another node by calling node.transmit(). The sender’s
node id must be specified in the url. As with node.transmit() the
key parameters are what and to_whom and they should be passed
as data. However, the values these accept are more limited than for
the backend due to the necessity of serialization.
If what and to_whom are not specified they will default to
None. Alternatively you can pass an int (e.g. ‘5’) or a class name
(e.g. Info or Agent). Passing an int will get that info/node,
passing a class name will pass the class. Note that if the class you
are specifying is a custom class it will need to be added to the
dictionary of known_classes in your experiment code.
You may also pass the values property1, property2, property3, property4, property5 and details. If passed this will fill in the relevant values of the transmissions created with the values you specified.
The transmitting node and a list of created transmissions are sent to
experiment method transmission_post_request(node, transmissions).
This route returns a list of JSON descriptions of the created
transmissions as transmissions. For example, to transmit all infos
of type Meme to the node with id 10:
reqwest({
url: "/node/" + my_node_id + "/transmit",
method: 'post',
type: 'json',
data: {
what: "Meme",
to_whom: 10,
},
});
GET /node/<node_id>/vectors
Returns a list of JSON descriptions of vectors connected to the node as
vectors. Vectors are identified by calling node.vectors().
direction and failed can be passed as data and will be forwarded
as arguments. Requesting node and list of vectors are also passed to
experiment method vector_get_request(node, vectors).
POST /node/<participant_id>
Create a node for the specified participant. The route calls the
following experiment methods:
get_network_for_participant(participant),
create_node(network, participant),
add_node_to_network(node, network), and
node_post_request(participant, node). Returns a JSON description of
the created node as node.
GET /participant/<participant_id>
Returns a JSON description of the requested participant as
participant.
POST /participant/<worker_id>/<hit_id>/<assignment_id>/<mode>
Create a participant. Returns a JSON description of the participant as
participant. Delegates participant creation to
create_participant()
POST /participant
Create a participant from a running experiment using arbitrary
entry_information passed as POST data. The experiment or recruiter is
responsible for transforming entry_information data into the
assignment_id, hit_id, and worker_id data needed to create a
participant. See
normalize_entry_information().
POST /load-participant
Loads a participant from a running experiment by assignment_id or arbitrary
entry_information passed as POST data. The experiment or recruiter is
responsible for transforming entry_information data into an
assignment_id data needed to lookup a participant.
See normalize_entry_information().
The participant in the response includes unique_id and worker_id
only when the worker_id that normalize_entry_information returns for the
request matches the participant’s.
When the lookup fails, the JSON error response may include a machine-readable
error_code:
assignment_id_missing- noassignment_idcould be resolved from the requestparticipant_not_found- anassignment_idwas present, but no matching participant exists
JavaScript clients can read these values from AjaxRejection.errorCode.
POST /question/<participant_id>
Create a question. question, response and question_id should
be passed as data. Does not return anything.
POST /transformation/<int:node_id>/<int:info_in_id>/<int:info_out_id>
Create a transformation from info_in to info_out at the
specified node. transformation_type can be passed as data and the
transformation will be of that class if it is a known class. Returns a
JSON description of the created transformation.