-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add notify as provider * Update notify.py * Fix format * remove unused imports * Upate import order * Update import order * Add notify to readme * Add documentation for Notify * Update test_notify.py * Fix formating * Remove unused import * Add tests * Add sanity test * Fix documentation
- Loading branch information
Showing
7 changed files
with
139 additions
and
2 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
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,65 @@ | ||
from ..core import Provider | ||
from ..core import Response | ||
from ..utils import requests | ||
|
||
|
||
class NotifyMixin: | ||
name = "notify" | ||
site_url = "https://github.com/K0IN/Notify" | ||
base_url = "{base_url}/api/notify" | ||
path_to_errors = ("message",) | ||
|
||
def _get_headers(self, token: str) -> dict: | ||
""" | ||
Builds Notify's requests header bases on the token provided | ||
:param token: Send token | ||
:return: Authentication header dict | ||
""" | ||
return {"Authorization": f"Bearer {token}"} | ||
|
||
|
||
class Notify(NotifyMixin, Provider): | ||
"""Send Notify notifications""" | ||
|
||
site_url = "https://github.com/K0IN/Notify" | ||
name = "notify" | ||
|
||
_required = {"required": ["title", "message", "base_url"]} | ||
_schema = { | ||
"type": "object", | ||
"properties": { | ||
"base_url": {"type": "string"}, | ||
"message": {"type": "string", "title": "your message"}, | ||
"title": {"type": "string", "title": "your message's title"}, | ||
"token": { | ||
"type": "string", | ||
"title": "your application's send key, see https://github.com/K0IN/Notify/blob/main/doc/docker.md", | ||
}, | ||
"tags": { | ||
"type": "array", | ||
"title": "your message's tags", | ||
"items": {"type": "string"}, | ||
}, | ||
}, | ||
"additionalProperties": False, | ||
} | ||
|
||
def _prepare_data(self, data: dict) -> dict: | ||
return data | ||
|
||
def _send_notification(self, data: dict) -> Response: | ||
url = self.base_url.format(base_url=data.pop("base_url")) | ||
token = data.pop("token", None) | ||
headers = self._get_headers(token) if token else {} | ||
response, errors = requests.post( | ||
url, | ||
json={ | ||
"message": data.pop("message"), | ||
"title": data.pop("title", None), | ||
"tags": data.pop("tags", []), | ||
}, | ||
headers=headers, | ||
path_to_errors=self.path_to_errors, | ||
) | ||
return self.create_response(data, response, errors) |
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
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,46 @@ | ||
Notify | ||
------ | ||
|
||
Send notifications via `Notify <https://github.com/K0IN/Notify>`_ | ||
|
||
.. code-block:: python | ||
>>> from notifiers import get_notifier | ||
>>> notify = get_notifier('notify') | ||
>>> notify.required | ||
{'required': ['title', 'message', 'base_url']} | ||
>>> notify.notify(title='Hi!', message='my message', base_url='http://localhost:8787') | ||
# some instances may need a token | ||
>>> notify.notify(title='Hi!', message='my message', base_url='http://localhost:8787', token="send_key") | ||
Full schema: | ||
|
||
.. code-block:: yaml | ||
additionalProperties: false | ||
properties: | ||
title: | ||
title: Title of the message | ||
type: string | ||
message: | ||
title: Body of the message | ||
type: string | ||
base_url: | ||
title: URL of the Notify instance | ||
type: string | ||
description: | | ||
The URL of the Notify instance. For example, if you are using the the demo instance you would use ``https://notify-demo.deno.dev``. | ||
tags: | ||
title: Tags to send the notification to | ||
type: array | ||
items: | ||
type: string | ||
token: | ||
title: access token | ||
type: string | ||
required: | ||
- title | ||
- message | ||
- base_url | ||
type: object |
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,23 @@ | ||
import pytest | ||
|
||
provider = "notify" | ||
|
||
|
||
class TestNotify: | ||
""" | ||
Notify notifier tests | ||
""" | ||
|
||
@pytest.mark.online | ||
def test_notify_sanity(self, provider, test_message): | ||
"""Successful notify notification""" | ||
data = { | ||
"message": test_message, | ||
"title": "test", | ||
"base_url": "https://notify-demo.deno.dev", | ||
"tags": ["test"], | ||
"token": "mypassword", | ||
} | ||
|
||
rsp = provider.notify(**data) | ||
rsp.raise_on_errors() |