Skip to content

Commit

Permalink
add notify as provider (#466)
Browse files Browse the repository at this point in the history
* 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
K0IN authored May 1, 2024
1 parent 284ee16 commit 080b2a8
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions notifiers/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,4 +35,5 @@
"popcornnotify": popcornnotify.PopcornNotify,
"statuspage": statuspage.Statuspage,
"victorops": victorops.VictorOps,
"notify": notify.Notify,
}
65 changes: 65 additions & 0 deletions notifiers/providers/notify.py
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)
1 change: 1 addition & 0 deletions source/CLI.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions source/providers/notify.rst
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
23 changes: 23 additions & 0 deletions tests/providers/test_notify.py
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()

0 comments on commit 080b2a8

Please sign in to comment.