Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AstroNote #214

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ Required parameters:

For more info, see _`ntfy-webpush` <https://github.com/dschep/ntfy-webpush>`_

`AstroNote <https://astronote.app>`_ - ``astronote``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Required parameters:
* ``token``

Optional parameters:
* ``category``
* ``display_category``
* ``persistent``
* ``sound``

For more info see the _AstroNote API Documentation <https://astronote.app/api>_

3rd party backends
~~~~~~~~~~~~~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions docs/ntfy.backends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ ntfy.backends.matrix module
:undoc-members:
:show-inheritance:

ntfy.backends.astronote module
-------------------------

.. automodule:: ntfy.backends.astronote
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------
Expand Down
54 changes: 54 additions & 0 deletions ntfy/backends/astronote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from __future__ import print_function

import logging

import requests

from ..config import USER_AGENT


def notify(title,
message,
token=None,
category=None,
display_category=None,
persistent=None,
sound=None,
retcode=None):
"""
Required parameters:
* ``token``

Optional parameters:
* ``category``
* ``display_category``
* ``persistent``
* ``sound``
"""

data = {
'body': message,
'title': title,
}

if category:
data['category'] = category

if display_category is not None:
data['display_category'] = display_category

if persistent is not None:
data['persistent'] = persistent

if sound:
data['sound'] = sound

resp = requests.post(
'https://api.astronote.app/1/notify',
json=data,
headers={
'Authorization': 'token %s' % token,
'User-Agent': USER_AGENT,
})

resp.raise_for_status()
85 changes: 85 additions & 0 deletions tests/test_astronote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from unittest import TestCase, main

from mock import patch
from ntfy.backends.astronote import notify
from ntfy.config import USER_AGENT


class TestAstroNote(TestCase):
@patch('requests.post')
def test_basic(self, mock_post):
notify('title', 'message', token='token')
mock_post.assert_called_once_with(
'https://api.astronote.app/1/notify',
json={
'body': 'message',
'title': 'title',
},
headers={
'Authorization': 'token token',
'User-Agent': USER_AGENT,
})

@patch('requests.post')
def test_persistent(self, mock_post):
notify('title', 'message', token='token', persistent=False)
mock_post.assert_called_once_with(
'https://api.astronote.app/1/notify',
json={
'body': 'message',
'title': 'title',
'persistent': False,
},
headers={
'Authorization': 'token token',
'User-Agent': USER_AGENT,
})

@patch('requests.post')
def test_category(self, mock_post):
notify('title', 'message', token='token', category='category')
mock_post.assert_called_once_with(
'https://api.astronote.app/1/notify',
json={
'body': 'message',
'title': 'title',
'category': 'category',
},
headers={
'Authorization': 'token token',
'User-Agent': USER_AGENT,
})

@patch('requests.post')
def test_display_category(self, mock_post):
notify('title', 'message', token='token', display_category=True)
mock_post.assert_called_once_with(
'https://api.astronote.app/1/notify',
json={
'body': 'message',
'title': 'title',
'display_category': True,
},
headers={
'Authorization': 'token token',
'User-Agent': USER_AGENT,
})

@patch('requests.post')
def test_sound(self, mock_post):
notify('title', 'message', token='token', sound='silent')
mock_post.assert_called_once_with(
'https://api.astronote.app/1/notify',
json={
'body': 'message',
'title': 'title',
'sound': 'silent',
},
headers={
'Authorization': 'token token',
'User-Agent': USER_AGENT,
})


if __name__ == '__main__':
main()
12 changes: 12 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ def test_instapush(self, mock_yamlload):
}
ntfy_main(['send', 'ms'])

@patch(builtin_module + '.open', mock_open())
@patch('ntfy.config.safe_load')
@patch('ntfy.backends.astronote.requests.post')
def test_astronote(self, mock_post, mock_yamlload):
mock_yamlload.return_value = {
'backends': ['astronote'],
'astronote': {
'token': 'token',
},
}
self.assertEqual(0, ntfy_main(['send', 'foobar']))


if __name__ == '__main__':
main()