js-api 0.3.19__tar.gz → 0.3.20__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- js_api-0.3.20/PKG-INFO +58 -0
- js_api-0.3.20/README.md +36 -0
- {js_api-0.3.19 → js_api-0.3.20}/pyproject.toml +1 -1
- {js_api-0.3.19 → js_api-0.3.20}/src/controllers/webhook.py +33 -0
- js_api-0.3.20/src/js_api.egg-info/PKG-INFO +58 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/js_api.egg-info/SOURCES.txt +1 -0
- js_api-0.3.20/tests/test_beszel.py +123 -0
- js_api-0.3.19/PKG-INFO +0 -42
- js_api-0.3.19/README.md +0 -20
- js_api-0.3.19/src/js_api.egg-info/PKG-INFO +0 -42
- {js_api-0.3.19 → js_api-0.3.20}/LICENSE +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/setup.cfg +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/__init__.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/api.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/controllers/__init__.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/controllers/docker.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/controllers/health_check.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/controllers/ip.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/controllers/valentines_day.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/database.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/js_api.egg-info/dependency_links.txt +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/js_api.egg-info/requires.txt +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/js_api.egg-info/top_level.txt +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/models.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/util/__init__.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/util/auth.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/util/docker.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/util/healthcheck.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/util/logging.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/src/util/mail.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/tests/test_docker.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/tests/test_health_check.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/tests/test_ip.py +0 -0
- {js_api-0.3.19 → js_api-0.3.20}/tests/test_webhook.py +0 -0
js_api-0.3.20/PKG-INFO
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: js-api
|
|
3
|
+
Version: 0.3.20
|
|
4
|
+
Summary: Custom API written in Python using FastAPI
|
|
5
|
+
Author-email: Jack Stockley <jack@jstockley.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/jnstockley/api
|
|
7
|
+
Project-URL: Repository, https://github.com/jnstockley/api.git
|
|
8
|
+
Project-URL: Issues, https://github.com/jnstockley/api/issues
|
|
9
|
+
Keywords: starter,template,python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: <3.16,>=3.15
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: fastapi[standard]==0.165.0
|
|
15
|
+
Requires-Dist: sqlalchemy==2.1.9
|
|
16
|
+
Requires-Dist: sqlmodel==0.0.50
|
|
17
|
+
Requires-Dist: python-dotenv==1.2.4
|
|
18
|
+
Requires-Dist: psycopg[binary]==3.3.9
|
|
19
|
+
Requires-Dist: nctalk==0.0.4
|
|
20
|
+
Requires-Dist: jinja2==3.1.6
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# API
|
|
24
|
+
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API
|
|
25
|
+
|
|
26
|
+
## Setup
|
|
27
|
+
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`
|
|
28
|
+
|
|
29
|
+
### Environment Vairables
|
|
30
|
+
- `API_KEY` - Any long, random string. Keep this secret as this is the only form of authentication for the API. All routes require it, except `/health-check/`
|
|
31
|
+
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
|
|
32
|
+
- `TZ` - Timezone of the container
|
|
33
|
+
- `PGTZ` - Timezone the Postgres container should use
|
|
34
|
+
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by the Talk webhooks
|
|
35
|
+
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/databasus`
|
|
36
|
+
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/databasus`
|
|
37
|
+
- `BESZEL_NEXTCLOUD_TALK_TOKEN` - Conversation token for the separate Beszel Talk bot used by `/webhook/beszel`
|
|
38
|
+
- `BESZEL_NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Beszel Talk bot
|
|
39
|
+
|
|
40
|
+
## Webhooks
|
|
41
|
+
- `POST /webhook/databasus` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
|
|
42
|
+
- `POST /webhook/beszel` - Forwards Beszel's generic JSON notifications (`{"title": "...", "message": "..."}`) using a separate Talk bot. Requires the `X-API-KEY` header. Posts the title in bold followed by the message. Returns `{"status": "sent"}` on success, `422` for invalid payloads, `500` for missing or insecure Talk configuration, or `502` when delivery fails.
|
|
43
|
+
|
|
44
|
+
### Beszel setup
|
|
45
|
+
|
|
46
|
+
1. Register a separate bot named **Beszel** using Nextcloud's [bot management commands](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) and enable it in the destination Talk conversation.
|
|
47
|
+
2. Set `NEXTCLOUD_URL`, `BESZEL_NEXTCLOUD_TALK_TOKEN` (the destination conversation token), and `BESZEL_NEXTCLOUD_TALK_SECRET` (the new bot's shared secret) in `.env`, then recreate the API container to load the settings. Beszel credentials are required; this endpoint does not fall back to the Databasus bot.
|
|
48
|
+
3. In Beszel, open **Settings > Notifications** and add this [generic webhook URL](https://beszel.dev/guide/notifications/generic), replacing the host and API key:
|
|
49
|
+
|
|
50
|
+
```text
|
|
51
|
+
generic://api.example.com/webhook/beszel?template=json&@X-API-KEY=YOUR_URL_ENCODED_API_KEY
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The `generic://` service uses HTTPS by default. URL-encode the API key if it contains special characters. `template=json` supplies the `title` and `message` fields, and `@X-API-KEY` adds the authentication header.
|
|
55
|
+
4. Send a test notification from Beszel, then enable alerts in its systems table.
|
|
56
|
+
|
|
57
|
+
## How to Access
|
|
58
|
+
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
|
js_api-0.3.20/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# API
|
|
2
|
+
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API
|
|
3
|
+
|
|
4
|
+
## Setup
|
|
5
|
+
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`
|
|
6
|
+
|
|
7
|
+
### Environment Vairables
|
|
8
|
+
- `API_KEY` - Any long, random string. Keep this secret as this is the only form of authentication for the API. All routes require it, except `/health-check/`
|
|
9
|
+
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
|
|
10
|
+
- `TZ` - Timezone of the container
|
|
11
|
+
- `PGTZ` - Timezone the Postgres container should use
|
|
12
|
+
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by the Talk webhooks
|
|
13
|
+
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/databasus`
|
|
14
|
+
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/databasus`
|
|
15
|
+
- `BESZEL_NEXTCLOUD_TALK_TOKEN` - Conversation token for the separate Beszel Talk bot used by `/webhook/beszel`
|
|
16
|
+
- `BESZEL_NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Beszel Talk bot
|
|
17
|
+
|
|
18
|
+
## Webhooks
|
|
19
|
+
- `POST /webhook/databasus` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
|
|
20
|
+
- `POST /webhook/beszel` - Forwards Beszel's generic JSON notifications (`{"title": "...", "message": "..."}`) using a separate Talk bot. Requires the `X-API-KEY` header. Posts the title in bold followed by the message. Returns `{"status": "sent"}` on success, `422` for invalid payloads, `500` for missing or insecure Talk configuration, or `502` when delivery fails.
|
|
21
|
+
|
|
22
|
+
### Beszel setup
|
|
23
|
+
|
|
24
|
+
1. Register a separate bot named **Beszel** using Nextcloud's [bot management commands](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) and enable it in the destination Talk conversation.
|
|
25
|
+
2. Set `NEXTCLOUD_URL`, `BESZEL_NEXTCLOUD_TALK_TOKEN` (the destination conversation token), and `BESZEL_NEXTCLOUD_TALK_SECRET` (the new bot's shared secret) in `.env`, then recreate the API container to load the settings. Beszel credentials are required; this endpoint does not fall back to the Databasus bot.
|
|
26
|
+
3. In Beszel, open **Settings > Notifications** and add this [generic webhook URL](https://beszel.dev/guide/notifications/generic), replacing the host and API key:
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
generic://api.example.com/webhook/beszel?template=json&@X-API-KEY=YOUR_URL_ENCODED_API_KEY
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `generic://` service uses HTTPS by default. URL-encode the API key if it contains special characters. `template=json` supplies the `title` and `message` fields, and `@X-API-KEY` adds the authentication header.
|
|
33
|
+
4. Send a test notification from Beszel, then enable alerts in its systems table.
|
|
34
|
+
|
|
35
|
+
## How to Access
|
|
36
|
+
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
|
|
@@ -16,6 +16,11 @@ class NextcloudTalkWebhook(BaseModel):
|
|
|
16
16
|
message: str = Field(..., min_length=1, max_length=32000)
|
|
17
17
|
|
|
18
18
|
|
|
19
|
+
class BeszelWebhook(BaseModel):
|
|
20
|
+
title: str = Field(..., min_length=1, max_length=200)
|
|
21
|
+
message: str = Field(..., min_length=1, max_length=32000)
|
|
22
|
+
|
|
23
|
+
|
|
19
24
|
class PveUpsWebhook(BaseModel):
|
|
20
25
|
subject: str = Field(..., min_length=1, max_length=200)
|
|
21
26
|
body: str = Field(..., min_length=1, max_length=32000)
|
|
@@ -56,6 +61,34 @@ async def databasus(webhook: NextcloudTalkWebhook):
|
|
|
56
61
|
return {"status": "sent"}
|
|
57
62
|
|
|
58
63
|
|
|
64
|
+
@router.post("/beszel")
|
|
65
|
+
async def beszel(webhook: BeszelWebhook):
|
|
66
|
+
if webhook.title.strip() == "":
|
|
67
|
+
raise HTTPException(status_code=422, detail="title cannot be empty")
|
|
68
|
+
if webhook.message.strip() == "":
|
|
69
|
+
raise HTTPException(status_code=422, detail="message cannot be empty")
|
|
70
|
+
|
|
71
|
+
endpoint_url = os.environ.get("NEXTCLOUD_URL", "").strip()
|
|
72
|
+
token = os.environ.get("BESZEL_NEXTCLOUD_TALK_TOKEN", "").strip()
|
|
73
|
+
secret = os.environ.get("BESZEL_NEXTCLOUD_TALK_SECRET", "").strip()
|
|
74
|
+
|
|
75
|
+
if not endpoint_url or not token or not secret:
|
|
76
|
+
raise HTTPException(status_code=500, detail="Nextcloud Talk is not configured")
|
|
77
|
+
if not endpoint_url.startswith("https://"):
|
|
78
|
+
raise HTTPException(status_code=500, detail="NEXTCLOUD_URL must use https://")
|
|
79
|
+
|
|
80
|
+
messenger = TalkMessenger(endpoint_url=endpoint_url, token=token, secret=secret)
|
|
81
|
+
message = f"**{webhook.title}**\n{webhook.message}"
|
|
82
|
+
|
|
83
|
+
if not messenger.send(message):
|
|
84
|
+
logger.error("Failed to send Beszel message to Nextcloud Talk")
|
|
85
|
+
raise HTTPException(
|
|
86
|
+
status_code=502, detail="Failed to send message to Nextcloud Talk"
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
return {"status": "sent"}
|
|
90
|
+
|
|
91
|
+
|
|
59
92
|
@router.post("/pve-ups")
|
|
60
93
|
async def pve_ups(webhook: PveUpsWebhook):
|
|
61
94
|
if not os.environ.get("SMTP_HOST") or not os.environ.get("SMTP_PORT"):
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: js-api
|
|
3
|
+
Version: 0.3.20
|
|
4
|
+
Summary: Custom API written in Python using FastAPI
|
|
5
|
+
Author-email: Jack Stockley <jack@jstockley.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/jnstockley/api
|
|
7
|
+
Project-URL: Repository, https://github.com/jnstockley/api.git
|
|
8
|
+
Project-URL: Issues, https://github.com/jnstockley/api/issues
|
|
9
|
+
Keywords: starter,template,python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: <3.16,>=3.15
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: fastapi[standard]==0.165.0
|
|
15
|
+
Requires-Dist: sqlalchemy==2.1.9
|
|
16
|
+
Requires-Dist: sqlmodel==0.0.50
|
|
17
|
+
Requires-Dist: python-dotenv==1.2.4
|
|
18
|
+
Requires-Dist: psycopg[binary]==3.3.9
|
|
19
|
+
Requires-Dist: nctalk==0.0.4
|
|
20
|
+
Requires-Dist: jinja2==3.1.6
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# API
|
|
24
|
+
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API
|
|
25
|
+
|
|
26
|
+
## Setup
|
|
27
|
+
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`
|
|
28
|
+
|
|
29
|
+
### Environment Vairables
|
|
30
|
+
- `API_KEY` - Any long, random string. Keep this secret as this is the only form of authentication for the API. All routes require it, except `/health-check/`
|
|
31
|
+
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
|
|
32
|
+
- `TZ` - Timezone of the container
|
|
33
|
+
- `PGTZ` - Timezone the Postgres container should use
|
|
34
|
+
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by the Talk webhooks
|
|
35
|
+
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/databasus`
|
|
36
|
+
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/databasus`
|
|
37
|
+
- `BESZEL_NEXTCLOUD_TALK_TOKEN` - Conversation token for the separate Beszel Talk bot used by `/webhook/beszel`
|
|
38
|
+
- `BESZEL_NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Beszel Talk bot
|
|
39
|
+
|
|
40
|
+
## Webhooks
|
|
41
|
+
- `POST /webhook/databasus` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
|
|
42
|
+
- `POST /webhook/beszel` - Forwards Beszel's generic JSON notifications (`{"title": "...", "message": "..."}`) using a separate Talk bot. Requires the `X-API-KEY` header. Posts the title in bold followed by the message. Returns `{"status": "sent"}` on success, `422` for invalid payloads, `500` for missing or insecure Talk configuration, or `502` when delivery fails.
|
|
43
|
+
|
|
44
|
+
### Beszel setup
|
|
45
|
+
|
|
46
|
+
1. Register a separate bot named **Beszel** using Nextcloud's [bot management commands](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) and enable it in the destination Talk conversation.
|
|
47
|
+
2. Set `NEXTCLOUD_URL`, `BESZEL_NEXTCLOUD_TALK_TOKEN` (the destination conversation token), and `BESZEL_NEXTCLOUD_TALK_SECRET` (the new bot's shared secret) in `.env`, then recreate the API container to load the settings. Beszel credentials are required; this endpoint does not fall back to the Databasus bot.
|
|
48
|
+
3. In Beszel, open **Settings > Notifications** and add this [generic webhook URL](https://beszel.dev/guide/notifications/generic), replacing the host and API key:
|
|
49
|
+
|
|
50
|
+
```text
|
|
51
|
+
generic://api.example.com/webhook/beszel?template=json&@X-API-KEY=YOUR_URL_ENCODED_API_KEY
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The `generic://` service uses HTTPS by default. URL-encode the API key if it contains special characters. `template=json` supplies the `title` and `message` fields, and `@X-API-KEY` adds the authentication header.
|
|
55
|
+
4. Send a test notification from Beszel, then enable alerts in its systems table.
|
|
56
|
+
|
|
57
|
+
## How to Access
|
|
58
|
+
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
from unittest.mock import patch
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from fastapi import FastAPI
|
|
5
|
+
from fastapi.testclient import TestClient
|
|
6
|
+
|
|
7
|
+
from controllers.webhook import router
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.fixture
|
|
11
|
+
def client(monkeypatch):
|
|
12
|
+
monkeypatch.setenv("API_KEY", "testing")
|
|
13
|
+
monkeypatch.setenv("NEXTCLOUD_URL", "https://nextcloud.example.com")
|
|
14
|
+
monkeypatch.setenv("NEXTCLOUD_TALK_TOKEN", "databasus-token")
|
|
15
|
+
monkeypatch.setenv("NEXTCLOUD_TALK_SECRET", "databasus-secret")
|
|
16
|
+
monkeypatch.setenv("BESZEL_NEXTCLOUD_TALK_TOKEN", "beszel-token")
|
|
17
|
+
monkeypatch.setenv("BESZEL_NEXTCLOUD_TALK_SECRET", "beszel-secret")
|
|
18
|
+
app = FastAPI()
|
|
19
|
+
app.include_router(router)
|
|
20
|
+
with TestClient(app) as test_client:
|
|
21
|
+
yield test_client
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
PAYLOAD = {
|
|
25
|
+
"title": "Server CPU above threshold",
|
|
26
|
+
"message": "CPU averaged 63.53% for the previous 10 minutes.",
|
|
27
|
+
}
|
|
28
|
+
HEADERS = {"X-API-KEY": "testing"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_beszel_sends_using_its_own_bot(client):
|
|
32
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
33
|
+
messenger.return_value.send.return_value = True
|
|
34
|
+
response = client.post("/webhook/beszel", headers=HEADERS, json=PAYLOAD)
|
|
35
|
+
|
|
36
|
+
assert response.status_code == 200
|
|
37
|
+
assert response.json() == {"status": "sent"}
|
|
38
|
+
messenger.assert_called_once_with(
|
|
39
|
+
endpoint_url="https://nextcloud.example.com",
|
|
40
|
+
token="beszel-token",
|
|
41
|
+
secret="beszel-secret",
|
|
42
|
+
)
|
|
43
|
+
messenger.return_value.send.assert_called_once_with(
|
|
44
|
+
f"**{PAYLOAD['title']}**\n{PAYLOAD['message']}"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@pytest.mark.parametrize("headers, status", [({}, 422), ({"X-API-KEY": "wrong"}, 401)])
|
|
49
|
+
def test_beszel_requires_api_key(client, headers, status):
|
|
50
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
51
|
+
response = client.post("/webhook/beszel", headers=headers, json=PAYLOAD)
|
|
52
|
+
assert response.status_code == status
|
|
53
|
+
messenger.assert_not_called()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@pytest.mark.parametrize(
|
|
57
|
+
"payload",
|
|
58
|
+
[
|
|
59
|
+
{},
|
|
60
|
+
{"title": "title"},
|
|
61
|
+
{"message": "message"},
|
|
62
|
+
{**PAYLOAD, "title": ""},
|
|
63
|
+
{**PAYLOAD, "title": " \t\n"},
|
|
64
|
+
{**PAYLOAD, "message": ""},
|
|
65
|
+
{**PAYLOAD, "message": " \t\n"},
|
|
66
|
+
{**PAYLOAD, "title": "x" * 201},
|
|
67
|
+
{**PAYLOAD, "message": "x" * 32001},
|
|
68
|
+
],
|
|
69
|
+
)
|
|
70
|
+
def test_beszel_rejects_invalid_payload(client, payload):
|
|
71
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
72
|
+
response = client.post("/webhook/beszel", headers=HEADERS, json=payload)
|
|
73
|
+
assert response.status_code == 422
|
|
74
|
+
messenger.assert_not_called()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@pytest.mark.parametrize(
|
|
78
|
+
"variable",
|
|
79
|
+
["NEXTCLOUD_URL", "BESZEL_NEXTCLOUD_TALK_TOKEN", "BESZEL_NEXTCLOUD_TALK_SECRET"],
|
|
80
|
+
)
|
|
81
|
+
@pytest.mark.parametrize("value", [None, "", " "])
|
|
82
|
+
def test_beszel_requires_configuration(client, monkeypatch, variable, value):
|
|
83
|
+
if value is None:
|
|
84
|
+
monkeypatch.delenv(variable)
|
|
85
|
+
else:
|
|
86
|
+
monkeypatch.setenv(variable, value)
|
|
87
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
88
|
+
response = client.post("/webhook/beszel", headers=HEADERS, json=PAYLOAD)
|
|
89
|
+
assert response.status_code == 500
|
|
90
|
+
assert response.json()["detail"] == "Nextcloud Talk is not configured"
|
|
91
|
+
messenger.assert_not_called()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_beszel_requires_https(client, monkeypatch):
|
|
95
|
+
monkeypatch.setenv("NEXTCLOUD_URL", "http://nextcloud.example.com")
|
|
96
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
97
|
+
response = client.post("/webhook/beszel", headers=HEADERS, json=PAYLOAD)
|
|
98
|
+
assert response.status_code == 500
|
|
99
|
+
messenger.assert_not_called()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_beszel_reports_delivery_failure(client):
|
|
103
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
104
|
+
messenger.return_value.send.return_value = False
|
|
105
|
+
response = client.post("/webhook/beszel", headers=HEADERS, json=PAYLOAD)
|
|
106
|
+
assert response.status_code == 502
|
|
107
|
+
assert response.json()["detail"] == "Failed to send message to Nextcloud Talk"
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_databasus_keeps_its_existing_bot(client):
|
|
111
|
+
with patch("controllers.webhook.TalkMessenger") as messenger:
|
|
112
|
+
messenger.return_value.send.return_value = True
|
|
113
|
+
response = client.post(
|
|
114
|
+
"/webhook/databasus",
|
|
115
|
+
headers=HEADERS,
|
|
116
|
+
json={"header": "Backup complete", "message": "Success"},
|
|
117
|
+
)
|
|
118
|
+
assert response.status_code == 200
|
|
119
|
+
messenger.assert_called_once_with(
|
|
120
|
+
endpoint_url="https://nextcloud.example.com",
|
|
121
|
+
token="databasus-token",
|
|
122
|
+
secret="databasus-secret",
|
|
123
|
+
)
|
js_api-0.3.19/PKG-INFO
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: js-api
|
|
3
|
-
Version: 0.3.19
|
|
4
|
-
Summary: Custom API written in Python using FastAPI
|
|
5
|
-
Author-email: Jack Stockley <jack@jstockley.com>
|
|
6
|
-
Project-URL: Homepage, https://github.com/jnstockley/api
|
|
7
|
-
Project-URL: Repository, https://github.com/jnstockley/api.git
|
|
8
|
-
Project-URL: Issues, https://github.com/jnstockley/api/issues
|
|
9
|
-
Keywords: starter,template,python
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Requires-Python: <3.16,>=3.15
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
Requires-Dist: fastapi[standard]==0.165.0
|
|
15
|
-
Requires-Dist: sqlalchemy==2.1.9
|
|
16
|
-
Requires-Dist: sqlmodel==0.0.50
|
|
17
|
-
Requires-Dist: python-dotenv==1.2.4
|
|
18
|
-
Requires-Dist: psycopg[binary]==3.3.9
|
|
19
|
-
Requires-Dist: nctalk==0.0.4
|
|
20
|
-
Requires-Dist: jinja2==3.1.6
|
|
21
|
-
Dynamic: license-file
|
|
22
|
-
|
|
23
|
-
# API
|
|
24
|
-
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API
|
|
25
|
-
|
|
26
|
-
## Setup
|
|
27
|
-
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`
|
|
28
|
-
|
|
29
|
-
### Environment Vairables
|
|
30
|
-
- `API_KEY` - Any long, random string. Keep this secret as this is the only form of authentication for the API. All routes require it, except `/health-check/`
|
|
31
|
-
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
|
|
32
|
-
- `TZ` - Timezone of the container
|
|
33
|
-
- `PGTZ` - Timezone the Postgres container should use
|
|
34
|
-
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by `/webhook/nextcloud-talk`
|
|
35
|
-
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/nextcloud-talk`
|
|
36
|
-
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/nextcloud-talk`
|
|
37
|
-
|
|
38
|
-
## Webhooks
|
|
39
|
-
- `POST /webhook/nextcloud-talk` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
|
|
40
|
-
|
|
41
|
-
## How to Access
|
|
42
|
-
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
|
js_api-0.3.19/README.md
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# API
|
|
2
|
-
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API
|
|
3
|
-
|
|
4
|
-
## Setup
|
|
5
|
-
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`
|
|
6
|
-
|
|
7
|
-
### Environment Vairables
|
|
8
|
-
- `API_KEY` - Any long, random string. Keep this secret as this is the only form of authentication for the API. All routes require it, except `/health-check/`
|
|
9
|
-
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
|
|
10
|
-
- `TZ` - Timezone of the container
|
|
11
|
-
- `PGTZ` - Timezone the Postgres container should use
|
|
12
|
-
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by `/webhook/nextcloud-talk`
|
|
13
|
-
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/nextcloud-talk`
|
|
14
|
-
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/nextcloud-talk`
|
|
15
|
-
|
|
16
|
-
## Webhooks
|
|
17
|
-
- `POST /webhook/nextcloud-talk` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
|
|
18
|
-
|
|
19
|
-
## How to Access
|
|
20
|
-
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: js-api
|
|
3
|
-
Version: 0.3.19
|
|
4
|
-
Summary: Custom API written in Python using FastAPI
|
|
5
|
-
Author-email: Jack Stockley <jack@jstockley.com>
|
|
6
|
-
Project-URL: Homepage, https://github.com/jnstockley/api
|
|
7
|
-
Project-URL: Repository, https://github.com/jnstockley/api.git
|
|
8
|
-
Project-URL: Issues, https://github.com/jnstockley/api/issues
|
|
9
|
-
Keywords: starter,template,python
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Requires-Python: <3.16,>=3.15
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
Requires-Dist: fastapi[standard]==0.165.0
|
|
15
|
-
Requires-Dist: sqlalchemy==2.1.9
|
|
16
|
-
Requires-Dist: sqlmodel==0.0.50
|
|
17
|
-
Requires-Dist: python-dotenv==1.2.4
|
|
18
|
-
Requires-Dist: psycopg[binary]==3.3.9
|
|
19
|
-
Requires-Dist: nctalk==0.0.4
|
|
20
|
-
Requires-Dist: jinja2==3.1.6
|
|
21
|
-
Dynamic: license-file
|
|
22
|
-
|
|
23
|
-
# API
|
|
24
|
-
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API
|
|
25
|
-
|
|
26
|
-
## Setup
|
|
27
|
-
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`
|
|
28
|
-
|
|
29
|
-
### Environment Vairables
|
|
30
|
-
- `API_KEY` - Any long, random string. Keep this secret as this is the only form of authentication for the API. All routes require it, except `/health-check/`
|
|
31
|
-
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
|
|
32
|
-
- `TZ` - Timezone of the container
|
|
33
|
-
- `PGTZ` - Timezone the Postgres container should use
|
|
34
|
-
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by `/webhook/nextcloud-talk`
|
|
35
|
-
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/nextcloud-talk`
|
|
36
|
-
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/nextcloud-talk`
|
|
37
|
-
|
|
38
|
-
## Webhooks
|
|
39
|
-
- `POST /webhook/nextcloud-talk` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
|
|
40
|
-
|
|
41
|
-
## How to Access
|
|
42
|
-
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|