mautrix.api package

Submodules

mautrix.api.http module

class mautrix.api.http.APIPath[source]

Bases: enum.Enum

The known Matrix API path prefixes.

CLIENT = '/_matrix/client/r0'
IDENTITY = '/_matrix/identity/r0'
MEDIA = '/_matrix/media/r0'
mautrix.api.http.ClientPath

A utility class to build API paths.

Examples

>>> from mautrix.api import Path
>>> room_id = "!foo:example.com"
>>> event_id = "$bar:example.com"
>>> str(Path.rooms[room_id].event[event_id])
"/_matrix/client/r0/rooms/%21foo%3Aexample.com/event/%24bar%3Aexample.com"
class mautrix.api.http.HTTPAPI(base_url: str, token: str, *, client_session: aiohttp.client.ClientSession = None, txn_id: int = 0, log: Optional[logging.Logger] = None, loop: Optional[asyncio.events.AbstractEventLoop] = None)[source]

Bases: object

HTTPAPI is a simple asyncio Matrix API request sender.

get_download_url(mxc_uri: str, download_type: str = 'download') → str[source]

Get the full HTTP URL to download a mxc:// URI.

Parameters:
  • mxc_uri (str) – The MXC URI whose full URL to get.
  • download_type (str) – The type of download (“download” or “thumbnail”)
Return type:

str

Returns:

The full HTTP URL.

Raises:

ValueError – If mxc_uri doesn’t begin with mxc://

Examples

>>> api = HTTPAPI(...)
>>> api.get_download_url("mxc://matrix.org/pqjkOuKZ1ZKRULWXgz2IVZV6")
"https://matrix.org/_matrix/media/r0/download/matrix.org/pqjkOuKZ1ZKRULWXgz2IVZV6
get_txn_id() → str[source]

Get a new unique transaction ID.

Return type:str
request(method: mautrix.api.http.Method, path: mautrix.api.http.PathBuilder, content: Union[NewType.<locals>.new_type, bytes, str, None] = None, headers: Optional[Dict[str, str]] = None, query_params: Optional[Dict[str, str]] = None) → NewType.<locals>.new_type[source]

Make a raw HTTP request.

Parameters:
  • method (Method) – The HTTP method to use.
  • path (PathBuilder) – The API endpoint to call. Does not include the base path (e.g. /_matrix/client/r0).
  • content (Union[JSON[Union[str, int, float, None, Dict[str, _Forwardref], List[_Forwardref]]], bytes, str, None]) – The content to post as a dict (json) or bytes/str (raw).
  • headers (Optional[Dict[str, str]]) – The dict of HTTP headers to send.
  • query_params (Optional[Dict[str, str]]) – The dict of query parameters to send.
  • api_path – The base API path.
Return type:

JSON[Union[str, int, float, None, Dict[str, _Forwardref], List[_Forwardref]]]

Returns:

The response as a dict.

mautrix.api.http.IdentityPath

A utility class to build API paths.

Examples

>>> from mautrix.api import Path
>>> room_id = "!foo:example.com"
>>> event_id = "$bar:example.com"
>>> str(Path.rooms[room_id].event[event_id])
"/_matrix/client/r0/rooms/%21foo%3Aexample.com/event/%24bar%3Aexample.com"
mautrix.api.http.MediaPath

A utility class to build API paths.

Examples

>>> from mautrix.api import Path
>>> room_id = "!foo:example.com"
>>> event_id = "$bar:example.com"
>>> str(Path.rooms[room_id].event[event_id])
"/_matrix/client/r0/rooms/%21foo%3Aexample.com/event/%24bar%3Aexample.com"
class mautrix.api.http.Method[source]

Bases: enum.Enum

A HTTP method.

DELETE = 'DELETE'
GET = 'GET'
POST = 'POST'
PUT = 'PUT'
mautrix.api.http.Path

A utility class to build API paths.

Examples

>>> from mautrix.api import Path
>>> room_id = "!foo:example.com"
>>> event_id = "$bar:example.com"
>>> str(Path.rooms[room_id].event[event_id])
"/_matrix/client/r0/rooms/%21foo%3Aexample.com/event/%24bar%3Aexample.com"
class mautrix.api.http.PathBuilder(path: Union[str, mautrix.api.http.APIPath] = '')[source]

Bases: object

A utility class to build API paths.

Examples

>>> from mautrix.api import Path
>>> room_id = "!foo:example.com"
>>> event_id = "$bar:example.com"
>>> str(Path.rooms[room_id].event[event_id])
"/_matrix/client/r0/rooms/%21foo%3Aexample.com/event/%24bar%3Aexample.com"
raw(append: str) → mautrix.api.http.PathBuilder[source]

Directly append a string to the path.

Parameters:append (str) – The string to append.
Return type:PathBuilder

Module contents