generated from canonical/is-charms-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add register-client-account action (#17)
- Loading branch information
Showing
9 changed files
with
508 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ops==2.17.0 | ||
requests==2.32.3 | ||
cosl==0.0.42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<!-- markdownlint-disable --> | ||
|
||
<a href="../src/maubot.py#L0"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
# <kbd>module</kbd> `maubot.py` | ||
Maubot service. | ||
|
||
**Global Variables** | ||
--------------- | ||
- **MAUBOT_ROOT_URL** | ||
|
||
--- | ||
|
||
<a href="../src/maubot.py#L33"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
## <kbd>function</kbd> `login` | ||
|
||
```python | ||
login(admin_name: str, admin_password: str) → str | ||
``` | ||
|
||
Login in Maubot and returns a token. | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`admin_name`</b>: admin name that will do the login. | ||
- <b>`admin_password`</b>: admin password. | ||
|
||
|
||
|
||
**Raises:** | ||
|
||
- <b>`APIError`</b>: error while interacting with Maubot API. | ||
|
||
|
||
|
||
**Returns:** | ||
token to be used in further requests. | ||
|
||
|
||
--- | ||
|
||
<a href="../src/maubot.py#L60"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
## <kbd>function</kbd> `register_account` | ||
|
||
```python | ||
register_account( | ||
token: str, | ||
account_name: str, | ||
account_password: str, | ||
matrix_server: str | ||
) → str | ||
``` | ||
|
||
Register account. | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`token`</b>: valid token for authentication. | ||
- <b>`account_name`</b>: account name to be registered. | ||
- <b>`account_password`</b>: account password to be registered. | ||
- <b>`matrix_server`</b>: Matrix server where the account will be registered. | ||
|
||
|
||
|
||
**Raises:** | ||
|
||
- <b>`APIError`</b>: error while interacting with Maubot API. | ||
|
||
|
||
|
||
**Returns:** | ||
Account access information. | ||
|
||
|
||
--- | ||
|
||
## <kbd>class</kbd> `APIError` | ||
Exception raised when something fails while interacting with Maubot API. | ||
|
||
Attrs: msg (str): Explanation of the error. | ||
|
||
<a href="../src/maubot.py#L24"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
### <kbd>function</kbd> `__init__` | ||
|
||
```python | ||
__init__(msg: str) | ||
``` | ||
|
||
Initialize a new instance of the MaubotError exception. | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`msg`</b> (str): Explanation of the error. | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""Maubot service.""" | ||
|
||
import logging | ||
|
||
import requests | ||
|
||
MAUBOT_ROOT_URL = "http://localhost:29316/_matrix/maubot" | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class APIError(Exception): | ||
"""Exception raised when something fails while interacting with Maubot API. | ||
Attrs: | ||
msg (str): Explanation of the error. | ||
""" | ||
|
||
def __init__(self, msg: str): | ||
"""Initialize a new instance of the MaubotError exception. | ||
Args: | ||
msg (str): Explanation of the error. | ||
""" | ||
self.msg = msg | ||
|
||
|
||
def login(admin_name: str, admin_password: str) -> str: | ||
"""Login in Maubot and returns a token. | ||
Args: | ||
admin_name: admin name that will do the login. | ||
admin_password: admin password. | ||
Raises: | ||
APIError: error while interacting with Maubot API. | ||
Returns: | ||
token to be used in further requests. | ||
""" | ||
url = f"{MAUBOT_ROOT_URL}/v1/auth/login" | ||
payload = {"username": admin_name, "password": admin_password} | ||
try: | ||
response = requests.post(url, json=payload, timeout=5) | ||
response.raise_for_status() | ||
token = response.json().get("token") | ||
if not token: | ||
raise APIError("token not found in Maubot API response") | ||
return token | ||
except (requests.exceptions.RequestException, TimeoutError) as e: | ||
logger.exception("failed to request Maubot API: %s", str(e)) | ||
raise APIError("error while interacting with Maubot API") from e | ||
|
||
|
||
def register_account( | ||
token: str, account_name: str, account_password: str, matrix_server: str | ||
) -> str: | ||
"""Register account. | ||
Args: | ||
token: valid token for authentication. | ||
account_name: account name to be registered. | ||
account_password: account password to be registered. | ||
matrix_server: Matrix server where the account will be registered. | ||
Raises: | ||
APIError: error while interacting with Maubot API. | ||
Returns: | ||
Account access information. | ||
""" | ||
url = f"{MAUBOT_ROOT_URL}/v1/client/auth/{matrix_server}/register" | ||
try: | ||
payload = {"username": account_name, "password": account_password} | ||
headers = {"Authorization": f"Bearer {token}"} | ||
response = requests.post(url, json=payload, headers=headers, timeout=5) | ||
response.raise_for_status() | ||
return response.json() | ||
except (requests.exceptions.RequestException, TimeoutError) as e: | ||
logger.exception("failed to request Maubot API: %s", str(e)) | ||
raise APIError("error while interacting with Maubot API") from e |
Oops, something went wrong.