moobius.network.http_api_wrapper#

Module-level functions#

summarize_html#

Creates a summary. Converts HTML to an easier-for-a-human format by cutting out some of the more common tags. Far from perfect.

Signature:

  • **summarize_html**(html_str)

Parameters:

  • __html_str:__ N html_string.

Returns:

  • The summary as a string.

Raises:

  • (this function does not raise any notable errors)

get_or_post#

Sends a GET or POST request and awaits for the response.

Signature:

  • **get_or_post**(url, is_post, requests_kwargs, raise_json_decode_errors)

Parameters:

  • __url:__ Https://

  • __is_post:__ False for GET, True for POST.

  • __requests_kwargs=None:__ These are fed into the requests/session get/post function.

  • __raise_json_decode_errors=True:__ Raise errors parsing the JSON that the request sends back, otherwise return the error as a dict.

Returns:

  • The dict which is the json.loads() of the return. Error condition if JSON decoding fails:

    dict[‘code’] contains the code

    10000 is “good” (but the JSON still failed). 204 indicates no return but without error which is also fine. Many other codes exist.

    dict[‘blob’] is the response text in cases where the JSON fail and raise_json is False.

Raises:

  • An Exception if Json fails and raise_json is True. Not all non-error returns are JSON thus the “blob” option.

Class BadResponseException#

For when the network is not doing what it should.

Class attributes#

BadResponseException.Exception

Class HTTPAPIWrapper#

Helper class for interacting with the Moobius HTTP API. All methods except for authenticate() and refresh() require authentication headers. When calling these methods, make sure to call authenticate() first and add headers=self.headers to the method call.

This wrapper’s methods are categorized as follows:

Auth: Authentication and sign in/out. User: Dealing with real users. Service: Apps use this API to be a service. Channel: Dealing with threads/channels/chat-rooms etc. File: Upload files (automatically fetches the URL needed). Group: Combine users, services, or channels into groups which can be addressed by a single group_id.

HTTPAPIWrapper.checked_get#

Calls self._checked_get_or_post with is_post=False..

Signature:

  • **HTTPAPIWrapper.checked_get**(self, url, the_request, requests_kwargs, good_message, bad_message, raise_errors)

Parameters:

  • __url:__ Url.

  • __the_request:__ The request itself.

  • __requests_kwargs=None:__ The kwargs for the request.

  • __good_message=None:__ The message to print on a happy 200.

  • __bad_message=’This HTTPs GET request failed’:__ The message to print on a sad non-200.

  • __raise_errors=True:__ Whether to raise errors if sad.

Returns:

  • The response. Raises a BadResponseException if it fails and raise_errors is set.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.checked_post#

Calls self._checked_get_or_post with is_post=True..

Signature:

  • **HTTPAPIWrapper.checked_post**(self, url, the_request, requests_kwargs, good_message, bad_message, raise_errors)

Parameters:

  • __url:__ Url.

  • __the_request:__ The request itself.

  • __requests_kwargs=None:__ The kwargs for the request.

  • __good_message=None:__ The message to print on a happy 200.

  • __bad_message=’This HTTPs POST request failed’:__ The message to print on a sad non-200.

  • __raise_errors=True:__ Whether to raise errors if sad.

Returns:

  • The response. Raises a BadResponseException if it fails and raise_errors is set.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.headers#

Signature:

  • **HTTPAPIWrapper.headers**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • The authentication headers. Used for all API calls except for authenticate() and refresh().

headers[“Auth-Origin”] is the authentication service, such as “cognito”. headers[“Authorization”] is the access token, etc that proves authentication.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.authenticate#

Authenticates using self.username andself.password. Needs to be called before any other API calls.

Signature:

  • **HTTPAPIWrapper.authenticate**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • (the access token, the refresh token).

Raises an Exception if doesn’t receive a valid response. Like most GET and POST functions it will raise any errors thrown by the http API.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.request_sign_up_code#

Signs up and sends the confirmation code to the email. After confirming the account, self.authenticate() can be used to retrieve access and refresh tokens.

Signature:

  • **HTTPAPIWrapper.request_sign_up_code**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.request_sign_up_code_again#

Resends the confimation code. After confirming the account, self.authenticate() can be used to retrieve access and refresh tokens.

Signature:

  • **HTTPAPIWrapper.request_sign_up_code_again**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.sign_up_with_code#

Sends the confirmation code confirming the signup itself.. After confirming the account, self.authenticate() can be used to retrieve access and refresh tokens.

Signature:

  • **HTTPAPIWrapper.sign_up_with_code**(self, the_code)

Parameters:

  • __the_code:__ Sign up code that was emailed.

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.get_password_reset_code#

Sends a reset-password request to the platform. After such a request is sent it will be necessary to check the email.

Signature:

  • **HTTPAPIWrapper.get_password_reset_code**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.reset_password#

Updates the password with a new one..

Signature:

  • **HTTPAPIWrapper.reset_password**(self, the_code)

Parameters:

  • __the_code:__ Code that was emailed to the user (use get_password_reset_code).

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.delete_account#

Deletes the currently signed in account. Mainly used for testing.

Signature:

  • **HTTPAPIWrapper.delete_account**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.sign_out#

Signs out using the access token obtained from signing in.

Signature:

  • **HTTPAPIWrapper.sign_out**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.refresh#

Refreshes the access token.

Signature:

  • **HTTPAPIWrapper.refresh**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • The new token.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_character_profile#

Signature:

  • **HTTPAPIWrapper.fetch_character_profile**(self, character)

Parameters:

  • __character:__ String-valued (or list-valued) character_id/character.

Returns:

  • The Character object (or list therof).

It works for both member_ids and agent_ids.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_member_ids#

Fetches the member ids of a channel which coorespond to real users.

Signature:

  • **HTTPAPIWrapper.fetch_member_ids**(self, channel_id, service_id, raise_empty_list_err)

Parameters:

  • __channel_id:__ The channel ID.

  • __service_id:__ The service/client/user ID.

  • __raise_empty_list_err=False:__ Raises an Exception if the list is empty.

Returns:

  • The list of character_id strings.

Raises:

  • An Exception (empty list) if raise_empty_list_err is True and the list is empty.

HTTPAPIWrapper.fetch_agents#

Signature:

  • **HTTPAPIWrapper.fetch_agents**(self, service_id)

Parameters:

  • __service_id:__ Service ID.

Returns:

  • The list of non-user Character objects bound to this service.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_user_info#

Signature:

  • **HTTPAPIWrapper.fetch_user_info**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • The UserInfo of the user logged in as, containing thier name, avatar, etc. Used by user mode.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.update_current_user#

Updates the user info. Used by user mode.

Signature:

  • **HTTPAPIWrapper.update_current_user**(self, avatar, description, name)

Parameters:

  • __avatar:__ Link to image or local file_path to upload.

  • __description:__ Of the user.

  • __name:__ The name that shows in chat.

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.create_service#

Creates and.

Signature:

  • **HTTPAPIWrapper.create_service**(self, description)

Parameters:

  • __description:__ Description string.

Returns:

  • The string-valued service_id.

Called once by the Moobius class if there is no service specified.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_service_id_list#

Signature:

  • **HTTPAPIWrapper.fetch_service_id_list**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • The list of service_id strings of the user.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.create_agent#

Creates a character with a given name, avatar, and description. The created user will be bound to the given service.

Signature:

  • **HTTPAPIWrapper.create_agent**(self, service_id, name, avatar, description)

Parameters:

  • __service_id:__ The service_id/client_id.

  • __name:__ The name of the user.

  • __avatar:__ The image URL of the user’s picture OR a local file path.

  • __description:__ The description of the user.

Returns:

  • The Character object representing the created user.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.update_agent#

Updates the characters name, avatar, etc for a FAKE user, for real users use update_current_user.

Signature:

  • **HTTPAPIWrapper.update_agent**(self, service_id, character, avatar, description, name)

Parameters:

  • __service_id:__ The service_id/client_id.

  • __character:__ Who to update. Can also be a Character object or character_id. Cannot be a list.

  • __avatar:__ A link to user’s image or a local file_path to upload.

  • __description:__ The description of user.

  • __name:__ The name that will show in chat.

Returns:

  • The Data about the user as a dict.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.create_channel#

Creates a channel.

Signature:

  • **HTTPAPIWrapper.create_channel**(self, channel_name, channel_desc)

Parameters:

  • __channel_name:__ String-valued channel name.

  • __channel_desc:__ Description.

Returns:

  • The channel_id.

Example ID: “13e44ea3-b559-45af-9106-6aa92501d4ed”.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.bind_service_to_channel#

Binds a service to a channel. This function is unusual in that it.

Signature:

  • **HTTPAPIWrapper.bind_service_to_channel**(self, service_id, channel_id)

Parameters:

  • __service_id:__ Service.

  • __channel_id:__ Channel IDs.

Returns:

  • Whether it was sucessful rather than raising errors if it fails.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.unbind_service_from_channel#

Unbinds a service to a channel.

Signature:

  • **HTTPAPIWrapper.unbind_service_from_channel**(self, service_id, channel_id)

Parameters:

  • __service_id:__ Service.

  • __channel_id:__ Channel IDs.

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.update_channel#

Updates the name and desc of a channel.

Signature:

  • **HTTPAPIWrapper.update_channel**(self, channel_id, channel_name, channel_desc)

Parameters:

  • __channel_id:__ Which channel to update.

  • __channel_name:__ The new channel name.

  • __channel_desc:__ The new channel description.

Returns:

  • None.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_channel_list#

Fetches all? channels,.

Signature:

  • **HTTPAPIWrapper.fetch_channel_list**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • The list of channel_id strings.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_message_history#

Returns the message chat history.

Signature:

  • **HTTPAPIWrapper.fetch_message_history**(self, channel_id, limit, before)

Parameters:

  • __channel_id:__ Channel with the messages inside of it.

  • __limit=64:__ Max number of messages to return (messages further back in time, if any, will not be returned).

  • __before=’null’:__ Only return messages older than this.

Returns:

  • The list of dicts.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.this_user_channels#

Signature:

  • **HTTPAPIWrapper.this_user_channels**(self)

Parameters:

  • __(this class constructor accepts no arguments):__

Returns:

  • The list of channel_ids this user is in.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.upload#

Uploads the file at local path file_path to the Moobius server. Automatically calculates the upload URL and upload fields.

Signature:

  • **HTTPAPIWrapper.upload**(self, file_path)

Parameters:

  • __file_path:__ File_path.

Returns:

  • The uploaded URL. Raises an Exception if the upload fails.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.convert_to_url#

Uploads and.

Signature:

  • **HTTPAPIWrapper.convert_to_url**(self, file_path)

Parameters:

  • __file_path:__ File_path.

Returns:

  • The bucket’s url. Idempotent: If given a URL will just return the URL.

Empty, False, or None strings are converted to a default URL.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.download_size#

Gets the download size in bytes. Queries for the header and does not download the file.

Signature:

  • **HTTPAPIWrapper.download_size**(self, url, headers)

Parameters:

  • __url:__ Url.

  • __headers=None:__ Optional headers.

Returns:

  • The number of bytes.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.download#

Downloads a file from a url or other source to a local filename, automatically creating dirs if need be.

Signature:

  • **HTTPAPIWrapper.download**(self, source, file_path, auto_dir, overwrite, bytes, headers)

Parameters:

  • __source:__ The url to download the file from. OR a MessageBody which has a .content.path in it.

  • __file_path=None:__ The file_path to download to.

    None will create a file based on the timestamp + random numbers. If no extension is specified, will infer the extension from the url if one exists.

  • __auto_dir=None:__ If no file_path is specified, a folder must be choosen.

    Defaults to ‘./downloads’.

  • __overwrite=None:__ Allow overwriting pre-existing files. If False, will raise an Exception on name collision.

  • __bytes=None:__ If True, will return bytes instead of saving a file.

  • __headers=None:__ Optional headers. Use these for downloads that require auth.

    Can set to “self” to use the same auth headers that this instance is using.

Returns:

  • The full filepath if bytes if false, otherwise the file’s content bytes if bytes=True.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_channel_group_dict#

Similar to fetch_member_ids..

Signature:

  • **HTTPAPIWrapper.fetch_channel_group_dict**(self, channel_id, service_id)

Parameters:

  • __channel_id:__ Channel_id.

  • __service_id:__ Service_id.

Returns:

  • The dict from each group_id to all characters.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_channel_group_list#

Similar to fetch_channel_group_dict..

Signature:

  • **HTTPAPIWrapper.fetch_channel_group_list**(self, channel_id, service_id)

Parameters:

  • __channel_id:__ Channel_id.

  • __service_id:__ Service_id.

Returns:

  • The raw data.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.create_channel_group#

Creates a channel group.

Signature:

  • **HTTPAPIWrapper.create_channel_group**(self, channel_id, group_name, characters)

Parameters:

  • __channel_id:__ The id of the group leader?.

  • __group_name:__ What to call it.

  • __characters:__ A list of characters or character_id strings that will be inside the group.

Returns:

  • The group_id string.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.character_ids_of_service_group#

Signature:

  • **HTTPAPIWrapper.character_ids_of_service_group**(self, group_id)

Parameters:

  • __group_id:__ Group_id.

Returns:

  • The list of character ids belonging to a service group.

Note that the ‘recipients’ in ‘on message up’ might be None:

To avoid requiring checks for None this function will return an empty list given Falsey inputs or Falsey string literals.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.character_ids_of_channel_group#

Gets a list of character ids belonging to a channel group. Websocket payloads contain these channel_groups which are shorthand for a list of characters.

Signature:

  • **HTTPAPIWrapper.character_ids_of_channel_group**(self, sender_id, channel_id, group_id)

Parameters:

  • __sender_id:__ The message’s sender.

  • __channel_id:__ The message specified that it was sent in this channel.

  • __group_id:__ The messages recipients.

Returns:

  • The character_id list.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.create_service_group#

Creates a group containing the list of characters_ids and returns this Group object. This group can then be used in send_message_down payloads.

Signature:

  • **HTTPAPIWrapper.create_service_group**(self, characters)

Parameters:

  • __characters:__ A list of character_id strings or Characters that will be inside the group.

Returns:

  • The Group object.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.update_channel_group#

Updates a channel group.

Signature:

  • **HTTPAPIWrapper.update_channel_group**(self, channel_id, group_id, characters)

Parameters:

  • __channel_id:__ The id of the group leader?.

  • __group_id:__ What to call it.

  • __characters:__ A list of character_id strings that will be inside the group.

Returns:

  • None.

Raises:

  • An Exception because it is unused, unimplemented, and may be removed.

HTTPAPIWrapper.update_temp_channel_group#

Updates a channel TEMP group.

Signature:

  • **HTTPAPIWrapper.update_temp_channel_group**(self, channel_id, characters)

Parameters:

  • __channel_id:__ The id of the group leader?.

  • __characters:__ A list of character_id strings that will be inside the group.

Returns:

  • None.

Raises:

  • An Exception because it is unused, unimplemented, and may be removed.

HTTPAPIWrapper.fetch_channel_temp_group#

Like fetch_channel_group_list but for TEMP groups..

Signature:

  • **HTTPAPIWrapper.fetch_channel_temp_group**(self, channel_id, service_id)

Parameters:

  • __channel_id:__ Channel_id.

  • __service_id:__ Service_id,.

Returns:

  • The list of groups.

Raises:

  • (this function does not raise any notable errors)

HTTPAPIWrapper.fetch_user_from_group#

Not yet implemented! Fetches the user profile of a user from a group.

Signature:

  • **HTTPAPIWrapper.fetch_user_from_group**(self, user_id, channel_id, group_id)

Parameters:

  • __user_id:__ The user ID.

  • __channel_id:__ The channel ID. (TODO: of what?).

  • __group_id:__ The group ID.

Returns:

  • The user profile Character object.

Raises:

  • An Exception because it is unused, unimplemented, and may be removed.

HTTPAPIWrapper.fetch_target_group#

Not yet implemented! Fetches info about the group.

Signature:

  • **HTTPAPIWrapper.fetch_target_group**(self, user_id, channel_id, group_id)

Parameters:

  • __user_id:__ The user id of the user bieng fetched (is this needed?).

  • __channel_id:__ The channel_id of the channel bieng fetched.

  • __group_id:__ Which group to fetch.

Returns:

  • The data-dict data.

Raises:

  • An Exception because it is unused, unimplemented, and may be removed.

Class attributes#

Internals#