From 37b313fff96f0880227f165a8d40a9661171f613 Mon Sep 17 00:00:00 2001 From: K0IN Date: Mon, 25 Dec 2023 01:43:43 +0100 Subject: [PATCH 1/9] add notify as provider --- notifiers/providers/__init__.py | 2 + notifiers/providers/notify.py | 65 +++++++++++++++++++++++++++++++++ tests/providers/test_notify.py | 10 +++++ 3 files changed, 77 insertions(+) create mode 100644 notifiers/providers/notify.py create mode 100644 tests/providers/test_notify.py diff --git a/notifiers/providers/__init__.py b/notifiers/providers/__init__.py index 2ae02e27..0403cb35 100644 --- a/notifiers/providers/__init__.py +++ b/notifiers/providers/__init__.py @@ -15,6 +15,7 @@ from . import twilio from . import victorops from . import zulip +from . import notify _all_providers = { "pushover": pushover.Pushover, @@ -34,4 +35,5 @@ "popcornnotify": popcornnotify.PopcornNotify, "statuspage": statuspage.Statuspage, "victorops": victorops.VictorOps, + "notify": notify.Notify, } diff --git a/notifiers/providers/notify.py b/notifiers/providers/notify.py new file mode 100644 index 00000000..fd3a0948 --- /dev/null +++ b/notifiers/providers/notify.py @@ -0,0 +1,65 @@ +from typing import Optional +from ..core import Provider +from ..core import ProviderResource +from ..core import Response +from ..exceptions import ResourceError +from ..utils import requests +from ..utils.schema.helpers import list_to_commas +from ..utils.schema.helpers import one_or_more + + +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: Optional[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}"} if token else {} + + +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")) + headers = self._get_headers(data.pop("token", None)) + 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) diff --git a/tests/providers/test_notify.py b/tests/providers/test_notify.py new file mode 100644 index 00000000..6bdd0b09 --- /dev/null +++ b/tests/providers/test_notify.py @@ -0,0 +1,10 @@ +import pytest + +provider = "notify" + + +class TestNotify: + @pytest.mark.online + def test_sanity(self, provider, test_message): + data = {"message": test_message} + provider.notify(**data, raise_on_errors=True) From bc890fe2ec487453141963f2b4bcb9db82a5cdce Mon Sep 17 00:00:00 2001 From: K0IN Date: Mon, 25 Dec 2023 01:51:07 +0100 Subject: [PATCH 2/9] Update notify.py * Fix format * remove unused imports --- notifiers/providers/notify.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/notifiers/providers/notify.py b/notifiers/providers/notify.py index fd3a0948..28022f96 100644 --- a/notifiers/providers/notify.py +++ b/notifiers/providers/notify.py @@ -1,11 +1,7 @@ from typing import Optional from ..core import Provider -from ..core import ProviderResource from ..core import Response -from ..exceptions import ResourceError from ..utils import requests -from ..utils.schema.helpers import list_to_commas -from ..utils.schema.helpers import one_or_more class NotifyMixin: @@ -33,10 +29,13 @@ class Notify(NotifyMixin, Provider): _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"}, + "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", From a1fdd3669b06afeb63e9c01b150ed54f1e88a0a4 Mon Sep 17 00:00:00 2001 From: K0IN Date: Mon, 25 Dec 2023 01:55:44 +0100 Subject: [PATCH 3/9] Upate import order * Update import order --- notifiers/providers/__init__.py | 2 +- notifiers/providers/notify.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/notifiers/providers/__init__.py b/notifiers/providers/__init__.py index 0403cb35..f51ca26e 100644 --- a/notifiers/providers/__init__.py +++ b/notifiers/providers/__init__.py @@ -4,6 +4,7 @@ from . import icloud from . import join from . import mailgun +from . import notify from . import pagerduty from . import popcornnotify from . import pushbullet @@ -15,7 +16,6 @@ from . import twilio from . import victorops from . import zulip -from . import notify _all_providers = { "pushover": pushover.Pushover, diff --git a/notifiers/providers/notify.py b/notifiers/providers/notify.py index 28022f96..d4acecdd 100644 --- a/notifiers/providers/notify.py +++ b/notifiers/providers/notify.py @@ -1,4 +1,3 @@ -from typing import Optional from ..core import Provider from ..core import Response from ..utils import requests @@ -10,18 +9,19 @@ class NotifyMixin: base_url = "{base_url}/api/notify" path_to_errors = ("message",) - def _get_headers(self, token: Optional[str]) -> dict: + 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}"} if token else {} + return {"Authorization": f"Bearer {token}"} class Notify(NotifyMixin, Provider): """Send Notify notifications""" + site_url = "https://github.com/K0IN/Notify" name = "notify" @@ -34,7 +34,7 @@ class Notify(NotifyMixin, Provider): "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" + "title": "your application's send key, see https://github.com/K0IN/Notify/blob/main/doc/docker.md", }, "tags": { "type": "array", @@ -50,7 +50,8 @@ def _prepare_data(self, data: dict) -> dict: def _send_notification(self, data: dict) -> Response: url = self.base_url.format(base_url=data.pop("base_url")) - headers = self._get_headers(data.pop("token", None)) + token = data.pop("token", None) + headers = self._get_headers(token) if token else {} response, errors = requests.post( url, json={ From 54d39e71a31bff901d86fe1002fb360a5b5f2e93 Mon Sep 17 00:00:00 2001 From: K0IN Date: Tue, 26 Dec 2023 00:54:26 +0100 Subject: [PATCH 4/9] Add notify to readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f73b531..f63b6549 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Got an app or service, and you want to enable your users to use notifications wi # Supported providers -[Pushover](https://pushover.net/), [SimplePush](https://simplepush.io/), [Slack](https://api.slack.com/), [Gmail](https://www.google.com/gmail/about/), Email (SMTP), [Telegram](https://telegram.org/), [Gitter](https://gitter.im), [Pushbullet](https://www.pushbullet.com), [Join](https://joaoapps.com/join/), [Zulip](https://zulipchat.com/), [Twilio](https://www.twilio.com/), [Pagerduty](https://www.pagerduty.com), [Mailgun](https://www.mailgun.com/), [PopcornNotify](https://popcornnotify.com), [StatusPage.io](https://statuspage.io), [iCloud](https://www.icloud.com/mail), [VictorOps (Splunk)](https://www.splunk.com/en_us/investor-relations/acquisitions/splunk-on-call.html) +[Pushover](https://pushover.net/), [SimplePush](https://simplepush.io/), [Slack](https://api.slack.com/), [Gmail](https://www.google.com/gmail/about/), Email (SMTP), [Telegram](https://telegram.org/), [Gitter](https://gitter.im), [Pushbullet](https://www.pushbullet.com), [Join](https://joaoapps.com/join/), [Zulip](https://zulipchat.com/), [Twilio](https://www.twilio.com/), [Pagerduty](https://www.pagerduty.com), [Mailgun](https://www.mailgun.com/), [PopcornNotify](https://popcornnotify.com), [StatusPage.io](https://statuspage.io), [iCloud](https://www.icloud.com/mail), [VictorOps (Splunk)](https://www.splunk.com/en_us/investor-relations/acquisitions/splunk-on-call.html), [Notify](https://github.com/K0IN/Notify) # Advantages From bdf10f14471a5a7f7bedc243b4011e207431cab4 Mon Sep 17 00:00:00 2001 From: K0IN Date: Tue, 26 Dec 2023 01:40:22 +0100 Subject: [PATCH 5/9] Add documentation for Notify --- source/CLI.rst | 1 + source/providers/notify.rst | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 source/providers/notify.rst diff --git a/source/CLI.rst b/source/CLI.rst index b4e43087..e770e19e 100644 --- a/source/CLI.rst +++ b/source/CLI.rst @@ -34,6 +34,7 @@ To view the main help just enter ``notifiers`` or ``notifiers --help``: telegram Options for 'telegram' zulip Options for 'zulip' victorops Options for 'victorops' + notify Options for 'notify' To view all providers use the ``providers`` command like so: diff --git a/source/providers/notify.rst b/source/providers/notify.rst new file mode 100644 index 00000000..8ac7e12e --- /dev/null +++ b/source/providers/notify.rst @@ -0,0 +1,46 @@ +Notify +------ + +Send notifications via `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 From cb9164d52ec79d7996e948b03eb8c0f031e2b192 Mon Sep 17 00:00:00 2001 From: K0IN Date: Tue, 26 Dec 2023 01:57:44 +0100 Subject: [PATCH 6/9] Update test_notify.py --- tests/providers/test_notify.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/providers/test_notify.py b/tests/providers/test_notify.py index 6bdd0b09..246a6377 100644 --- a/tests/providers/test_notify.py +++ b/tests/providers/test_notify.py @@ -4,7 +4,5 @@ class TestNotify: - @pytest.mark.online - def test_sanity(self, provider, test_message): - data = {"message": test_message} - provider.notify(**data, raise_on_errors=True) + pass + From c080515cf5b349ee1cf1f919fd785853d6a8435d Mon Sep 17 00:00:00 2001 From: K0IN Date: Mon, 29 Apr 2024 21:47:07 +0200 Subject: [PATCH 7/9] Fix formating --- source/conf.py | 2 +- tests/providers/test_notify.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/source/conf.py b/source/conf.py index 6f39d52c..ec3e4771 100644 --- a/source/conf.py +++ b/source/conf.py @@ -76,7 +76,7 @@ # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. -language = 'en' +language = "en" # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. diff --git a/tests/providers/test_notify.py b/tests/providers/test_notify.py index 246a6377..3412fbf3 100644 --- a/tests/providers/test_notify.py +++ b/tests/providers/test_notify.py @@ -5,4 +5,3 @@ class TestNotify: pass - From 30538f16d30e901601e1e02b413bf44c6d280263 Mon Sep 17 00:00:00 2001 From: K0IN Date: Mon, 29 Apr 2024 21:52:11 +0200 Subject: [PATCH 8/9] Remove unused import --- tests/providers/test_notify.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/providers/test_notify.py b/tests/providers/test_notify.py index 3412fbf3..5a2ef792 100644 --- a/tests/providers/test_notify.py +++ b/tests/providers/test_notify.py @@ -1,5 +1,3 @@ -import pytest - provider = "notify" From 34d4e46a333f8f352239b8aba3c37d2fe646778b Mon Sep 17 00:00:00 2001 From: K0IN Date: Mon, 29 Apr 2024 22:47:36 +0200 Subject: [PATCH 9/9] Add tests * Add sanity test * Fix documentation --- source/providers/notify.rst | 2 +- tests/providers/test_notify.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/source/providers/notify.rst b/source/providers/notify.rst index 8ac7e12e..92438776 100644 --- a/source/providers/notify.rst +++ b/source/providers/notify.rst @@ -43,4 +43,4 @@ Full schema: - title - message - base_url -type: object + type: object diff --git a/tests/providers/test_notify.py b/tests/providers/test_notify.py index 5a2ef792..f92a80f2 100644 --- a/tests/providers/test_notify.py +++ b/tests/providers/test_notify.py @@ -1,5 +1,23 @@ +import pytest + provider = "notify" class TestNotify: - pass + """ + 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()