moobius.network.ws_client#
Module-level functions#
asserted_dataclass_asdict#
Asserts that the input is the correct dataclass or is a dict which matches to a dataclsss.
**asserted_dataclass_asdict**(x, the_class)
__x:__ The input dict or dataclass.
__the_class:__ The class to match to, such as types.Button.
The modified value of x, as a dict.
An Exception if there is a mismatch in the formatting.
time_out_wrap#
Sometimes the connection can hang forever. Adds a timeout that will make await raise an asyncio.TimeoutError if the function takes too long..
**time_out_wrap**(co_routine, timeout)
__co_routine:__ Co-routine.
__timeout=16:__ A timeout.
The co-routine with a timeout.
(this function does not raise any notable errors)
Class WSClient#
WSClient is a websocket client that has a wide variety of Moobius-specific functions for sending payloads specific to the Moobius platform. It contains the standard socket functions such as on_connect(), send(), and receive() and is more robust: it has a queuing system and will automatically reconnect.
WSClient.connect#
Connects to the websocket server. Call after self.authenticate(). Keeps trying if it fails!.
**WSClient.connect**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
WSClient.send#
Adds the message to self.outbound_queue for sending to the server. Note: Call this and other socket functions after self.authenticate()
If the server responds to the message it will be detected in the self.recieve() loop.
**WSClient.send**(self, message)
__message:__ Dict-valued message (or JSON string).
None.
(this function does not raise any notable errors)
WSClient.receive#
Waits in a loop for messages from the websocket server or from the wand queue. Never.
**WSClient.receive**(self)
__(this class constructor accepts no arguments):__
The
Reconnectes if the connection fails or self.websocket.recv() stops getting anything (no heartbeats nor messages).
(this function does not raise any notable errors)
WSClient.safe_handle#
Handles it with self.handle, which is specified on construction, catching errors.
**WSClient.safe_handle**(self, message)
__message:__ String-valued message from the websocket server.
None.
(this function does not raise any notable errors)
WSClient.heartbeat#
Sends a heartbeat..
**WSClient.heartbeat**(self, dry_run)
__dry_run:__ N optional dry_run to not send anything if True.
The message dict.
(this function does not raise any notable errors)
WSClient.dumps#
A slightly better json.dumps..
**WSClient.dumps**(data)
__data:__ Datastructure or dataclass and.
The JSON string.
(this function does not raise any notable errors)
WSClient.service_login#
Logs in. Much like the HTTP api, this needs to be sent before any other messages.
**WSClient.service_login**(self, service_id, access_token, dry_run)
- __service_id:__ The client_id of a Moobius service object, which is the ID of the running service.
Used in almost every function.
__access_token:__ TODO; This is the access token from http_api_wrapper; for clean code decouple access_token here!.
__dry_run:__ Don’t acually send anything (must functions offer a dry-run option).
The message as a dict.
(this function does not raise any notable errors)
WSClient.user_login#
Logs-in a user. Every 2h AWS will force-disconnect, so it is a good idea to send this on connect.
**WSClient.user_login**(self, access_token, dry_run)
- __access_token:__ Used in the user_login message that is sent.
This is the access token from http_api_wrapper.
__dry_run:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
WSClient.leave_channel#
A user leaves the channel with channel_id, unless dry_run is True..
**WSClient.leave_channel**(self, user_id, channel_id, dry_run)
__user_id:__ User_id.
__channel_id:__ The channel_id.
__dry_run:__ Whether to dry_run.
The message sent.
(this function does not raise any notable errors)
WSClient.join_channel#
A user joins the channel with channel_id, unless dry_run is True..
**WSClient.join_channel**(self, user_id, channel_id, dry_run)
__user_id:__ User_id.
__channel_id:__ The channel_id.
__dry_run:__ Whether to dry_run.
The message sent.
(this function does not raise any notable errors)
WSClient.send_characters#
Updates the characters that the recipients see.
**WSClient.send_characters**(self, characters, service_id, channel_id, recipients, dry_run)
__characters:__ The group id to represent the characters who are updated.
__service_id:__ As always.
__channel_id:__ The channel id.
__recipients:__ The group id to send to.
__dry_run:__ If True don’t acually send the message (messages are sent in thier JSON-strin format).
The message as a dict.
(this function does not raise any notable errors)
WSClient.send_style#
Updates the style (whether the canvas is expanded, other look-and-feel aspects) that the recipients see.
**WSClient.send_style**(self, style_items, service_id, channel_id, recipients, dry_run)
__style_items:__ The style content to be updated. Dicts are converted into 1-elemnt lists.
__service_id:__ As always.
__channel_id:__ The channel id.
__recipients:__ The group id to send to.
__dry_run:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
Start from here.
This is a Button, which most channels have
" >>> }] >>> ws_client.update_style("service_id", "channel_id", style_items, ["user1", "user2"])WSClient.update_channel_info#
Updates the channel name, description, etc for a given channel.
**WSClient.update_channel_info**(self, channel_info, service_id, channel_id, dry_run)
__channel_info:__ The data of the update.
__service_id:__ As always.
__channel_id:__ The channel id.
__dry_run:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
WSClient.update_canvas#
Updates the canvas that the recipients see.
**WSClient.update_canvas**(self, service_id, channel_id, canvas_items, recipients, dry_run)
__service_id:__ As always.
__channel_id:__ The channel id.
__canvas_items:__ The elements to push to the canvas.
__recipients:__ The recipients character_ids who see the update.
__dry_run:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
WSClient.update#
A generic update function that is rarely used.
**WSClient.update**(self, data, target_client_id, service_id, dry_run)
__data:__ The content of the update.
__target_client_id:__ The target client id (TODO: not currently used).
__service_id:__ The ID of the service.
__dry_run:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
WSClient.message_up#
Used by users to send messages.
**WSClient.message_up**(self, user_id, service_id, channel_id, recipients, subtype, content, context, dry_run)
__user_id:__ An enduser id generally.
__service_id:__ Which service to send to.
__channel_id:__ Which channel to broadcast the message in.
__recipients:__ The group id to send to.
__subtype:__ The subtype of message to send (text, etc). Goes into message[‘body’] JSON.
__content:__ What is inside the message[‘body’][‘content’] JSON.
__context:__ Optional metadata.
__dry_run=None:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
WSClient.message_down#
Sends a message to the recipients.
**WSClient.message_down**(self, user_id, service_id, channel_id, recipients, subtype, content, sender, context, dry_run)
__user_id:__ An service id generally.
__service_id:__ Which service to send to.
__channel_id:__ Which channel to broadcast the message in.
__recipients:__ The group id to send to.
__subtype:__ The subtype of message to send (text, etc). Goes into message[‘body’] JSON.
__content:__ What is inside the message[‘body’][‘content’] JSON.
__sender:__ The sender ID of the message, which determines who the chat shows the message as sent by.
__context:__ Optional metadata.
__dry_run=None:__ Don’t actually send anything if True.
The message as a dict.
(this function does not raise any notable errors)
WSClient.refresh_as_user#
Refreshes everything the user can see. The socket will send back messages with the information later.
**WSClient.refresh_as_user**(self, user_id, channel_id, dry_run)
__user_id:__ Used in the “action” message that is sent.
__channel_id:__ Used in the body of said message.
- __dry_run:__ Don’t actually send anything if True.
These three parameters are common to most fetch messages.
The message that was sent as a dict.
(this function does not raise any notable errors)