From ec33f8082db445d1d5831561e8008a0ac7a2e4ba Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sun, 20 Sep 2020 12:51:29 +0000 Subject: [PATCH 1/2] Add support for AstroNote --- README.rst | 12 ++++++ docs/ntfy.backends.rst | 7 ++++ ntfy/backends/astronote.py | 53 ++++++++++++++++++++++++ tests/test_astronote.py | 85 ++++++++++++++++++++++++++++++++++++++ tests/test_integration.py | 12 ++++++ 5 files changed, 169 insertions(+) create mode 100644 ntfy/backends/astronote.py create mode 100644 tests/test_astronote.py diff --git a/README.rst b/README.rst index 899b184..d89aae1 100644 --- a/README.rst +++ b/README.rst @@ -393,6 +393,18 @@ Required parameters: For more info, see _`ntfy-webpush` `_ +`AstroNote `_ - ``astronote`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Required parameters: + * ``token`` + +Optional parameters: + * ``category`` + * ``display_category`` + * ``persistent`` + * ``sound`` + +For more info see the _AstroNote API Documentation _ 3rd party backends ~~~~~~~~~~~~~~~~~~ diff --git a/docs/ntfy.backends.rst b/docs/ntfy.backends.rst index 983bf72..deaa491 100644 --- a/docs/ntfy.backends.rst +++ b/docs/ntfy.backends.rst @@ -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 --------------- diff --git a/ntfy/backends/astronote.py b/ntfy/backends/astronote.py new file mode 100644 index 0000000..79af1dc --- /dev/null +++ b/ntfy/backends/astronote.py @@ -0,0 +1,53 @@ +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): + """ + 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() diff --git a/tests/test_astronote.py b/tests/test_astronote.py new file mode 100644 index 0000000..d03903c --- /dev/null +++ b/tests/test_astronote.py @@ -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() diff --git a/tests/test_integration.py b/tests/test_integration.py index d50cc7e..1a4f6d0 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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() From 50f1853e03a7aa052cafa316ba1634cecca98613 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sun, 20 Sep 2020 13:17:28 +0000 Subject: [PATCH 2/2] Add support for AstroNote --- ntfy/backends/astronote.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ntfy/backends/astronote.py b/ntfy/backends/astronote.py index 79af1dc..71bf954 100644 --- a/ntfy/backends/astronote.py +++ b/ntfy/backends/astronote.py @@ -13,7 +13,8 @@ def notify(title, category=None, display_category=None, persistent=None, - sound=None): + sound=None, + retcode=None): """ Required parameters: * ``token``