From 925322ee02f7f9bc575d0bea641668b1e68e3ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 11:29:11 +0200 Subject: [PATCH 1/7] Refactor. This allows to create a client without forcing to use an app_id. --- pbsonesignal/__init__.py | 22 ++++++++++++++++++---- pbsonesignal/exceptions.py | 11 ++++++++++- tests/test_onesignal.py | 13 ------------- tests/test_push_msg.py | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/pbsonesignal/__init__.py b/pbsonesignal/__init__.py index f8aa474..331ebbf 100644 --- a/pbsonesignal/__init__.py +++ b/pbsonesignal/__init__.py @@ -33,14 +33,16 @@ class PybossaOneSignal(object): api_url = 'https://onesignal.com/api/v1/notifications' api_apps = 'https://onesignal.com/api/v1/apps' - def __init__(self, api_key, app_id=None, app_ids=None, auth_key=None): + def __init__(self, api_key=None, app_id=None, app_ids=None, auth_key=None): """Initiate.""" try: + if api_key is None and auth_key is None: + msg = "Provide some credentials API key or AUTH key." + raise ApiAuthKeysMissing self.api_key = api_key self.auth_key = auth_key - if app_id is None and app_ids is None: - msg = "You should provide an app_id or an array of app_ids" - raise AppIdMissing(msg) + self.app_id = app_id + self.app_ids = app_ids if app_id and app_ids: msg = "You can only provide or an app_id or a list of app_ids" raise AppIdDuplicate(msg) @@ -79,6 +81,14 @@ def push_msg(self, contents={"en": "English Message"}, """Push notification message.""" try: + if self.api_key is None: + msg = "API key is missing" + raise ApiKeyMissing(msg) + + if self.app_id is None and self.app_ids is None: + msg = "You should provide an app_id or an array of app_ids" + raise AppIdMissing(msg) + payload = { "included_segments": included_segments, "excluded_sements": excluded_sements, @@ -135,6 +145,10 @@ def create_app(self, name, **kwargs): """Create a OneSignal app.""" try: + if self.auth_key is None: + msg = "Auth key missing." + raise AuthKeyMissing(msg) + payload = dict(name=name, chrome_web_origin=chrome_web_origin, chrome_web_default_notification_icon=chrome_web_default_notification_icon, diff --git a/pbsonesignal/exceptions.py b/pbsonesignal/exceptions.py index 6e6544e..555634d 100644 --- a/pbsonesignal/exceptions.py +++ b/pbsonesignal/exceptions.py @@ -25,7 +25,8 @@ """ __all__ = ['AppIdsMissing', 'AppIdMissing', 'ApiKeyMissing', 'AppIdDuplicate', - 'CreateNotification', 'CreateApp'] + 'CreateNotification', 'CreateApp', 'AuthKeyMissing', + 'ApiAuthKeysMissing'] class AppIdMissing(Exception): @@ -44,6 +45,14 @@ class ApiKeyMissing(Exception): pass +class AuthKeyMissing(Exception): + pass + + +class ApiAuthKeysMissing(Exception): + pass + + class CreateNotification(Exception): pass diff --git a/tests/test_onesignal.py b/tests/test_onesignal.py index 18bbc8c..814b375 100644 --- a/tests/test_onesignal.py +++ b/tests/test_onesignal.py @@ -46,25 +46,12 @@ def check_error_output(self, res, err): for k in err.keys(): assert err[k] == res[k], err - @raises(AppIdMissing) - def test_init_no_app_id(self): - """Test init without app_id.""" - PybossaOneSignal(api_key="something") - - @raises(AppIdMissing) - def test_init_no_app_ids(self): - """Test init without app_ids.""" - PybossaOneSignal(api_key="something") @raises(AppIdDuplicate) def test_init_no_app_id_duplicates(self): """Test init using app_id and app_ids.""" PybossaOneSignal(app_id="1", app_ids=["a"], api_key="something") - @raises(TypeError) - def test_init_no_api_key(self): - """Test init without api_key.""" - PybossaOneSignal(app_id="1") def test_headers(self): """Test headers method.""" diff --git a/tests/test_push_msg.py b/tests/test_push_msg.py index 3948e75..93efdb0 100644 --- a/tests/test_push_msg.py +++ b/tests/test_push_msg.py @@ -46,6 +46,23 @@ def setUp(self): "chrome_web_image": "https://yourimage.com", "chrome_web_icon": "https://image"} + @raises(AppIdMissing) + def test_push_msg_no_app_id(self): + """Test push_msg without app_id.""" + client = PybossaOneSignal(api_key="something") + client.push_msg() + + @raises(AppIdMissing) + def test_push_msg_no_app_ids(self): + """Test push_msg without app_ids.""" + client = PybossaOneSignal(api_key="something") + client.push_msg() + + @raises(ApiKeyMissing) + def test_push_msg_no_api_key(self): + """Test push_msg without api_key.""" + client = PybossaOneSignal(app_id="1", auth_key="something") + client.push_msg() @patch('pbsonesignal.requests.post') def test_push_msg(self, mock): From 5838bdcfbf6bbf7cf7486004f373bad4dcc38eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 11:59:07 +0200 Subject: [PATCH 2/7] Cover no keys. --- tests/test_create_app.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_create_app.py b/tests/test_create_app.py index aa7d749..bf66d2e 100644 --- a/tests/test_create_app.py +++ b/tests/test_create_app.py @@ -67,6 +67,10 @@ def setUp(self): "chrome_web_image": "https://yourimage.com", "chrome_web_icon": "https://image"} + @raises(ApiAuthKeysMissing) + def test_create_client(self): + """Test create client without keys.""" + PybossaOneSignal() @patch('pbsonesignal.requests.post') def test_create_app(self, mock): From 9121d09a84f1e5ed5527b6a885a80aca22f38736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 11:59:45 +0200 Subject: [PATCH 3/7] Fixes. --- tests/test_create_app.py | 4 ---- tests/test_onesignal.py | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_create_app.py b/tests/test_create_app.py index bf66d2e..aa7d749 100644 --- a/tests/test_create_app.py +++ b/tests/test_create_app.py @@ -67,10 +67,6 @@ def setUp(self): "chrome_web_image": "https://yourimage.com", "chrome_web_icon": "https://image"} - @raises(ApiAuthKeysMissing) - def test_create_client(self): - """Test create client without keys.""" - PybossaOneSignal() @patch('pbsonesignal.requests.post') def test_create_app(self, mock): diff --git a/tests/test_onesignal.py b/tests/test_onesignal.py index 814b375..7f3b81b 100644 --- a/tests/test_onesignal.py +++ b/tests/test_onesignal.py @@ -52,6 +52,10 @@ def test_init_no_app_id_duplicates(self): """Test init using app_id and app_ids.""" PybossaOneSignal(app_id="1", app_ids=["a"], api_key="something") + @raises(ApiAuthKeysMissing) + def test_create_client(self): + """Test create client without keys.""" + PybossaOneSignal() def test_headers(self): """Test headers method.""" From 08968c9aff30420e4d1a5d3572217af434ddad20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 12:02:14 +0200 Subject: [PATCH 4/7] Fixes. --- tests/test_create_app.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_create_app.py b/tests/test_create_app.py index aa7d749..0dc220f 100644 --- a/tests/test_create_app.py +++ b/tests/test_create_app.py @@ -67,6 +67,11 @@ def setUp(self): "chrome_web_image": "https://yourimage.com", "chrome_web_icon": "https://image"} + @raises(AuthKeyMissing) + def test_create_app_no_auth_key(self): + """Test create_app without auth_key.""" + client = PybossaOneSignal(api_key="key") + client.create_app('name', 'https://scifabric.com', 'https://scifabric.com/img') @patch('pbsonesignal.requests.post') def test_create_app(self, mock): From 650409bfc6e28663909921a9afd2c1127147814f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 12:10:53 +0200 Subject: [PATCH 5/7] Fixes. --- pbsonesignal/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pbsonesignal/__init__.py b/pbsonesignal/__init__.py index 331ebbf..08326c7 100644 --- a/pbsonesignal/__init__.py +++ b/pbsonesignal/__init__.py @@ -59,7 +59,6 @@ def header(self, auth): return {"Content-Type": "application/json; charset=utf-8", "Authorization": "Basic %s" % auth} - def push_msg(self, contents={"en": "English Message"}, headings={"en": "Heading"}, launch_url="https://yoursite.com/", From 4094f4b77b99d8bd962b2da36f30e6e121082819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 12:19:11 +0200 Subject: [PATCH 6/7] Update docs. --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 95c662b..938212a 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,17 @@ client.push_msg(contents={"en": "Your message in English", "es": "Tu mensaje en ``` +In the case that you want to create a new app, just do the following: + +```python +from pbsonesignal import PybossaOneSignal + +client = PybossaOneSignal(auth_key="yourkey") + +client.create_app('name', 'https://yoursite.com', 'https://yoursite.com/icon') +``` + + ## Arguments for push_msg The following is a list of all the arguments you can use with this client: @@ -161,12 +172,46 @@ client = PybossaOneSignal(api_key="yourkey", app_id="ID", auth_key="yourkey") client.create_app('name_app', 'https://yourdoamin.com', 'https://yourdomain/icon.png') ``` + +## Arguments for create_app + +You can use the following arguments for this method. + +### name + +This is a string. The name of the app you will be creating in OneSignal. + +```python +name='yourappname' +``` + +### chrome_web_origin + +This is a string. The URL of the site that you will be linking to the app. + +```python +chrome_web_origin='https://yoursite.com' +``` + +**WARNING**: Read their docs, but you need **full HTTPS in your site** so this can work. + +### chrome_web_default_notification_icon + +This is a string. The URL of the default notification icon + +```python +chrome_web_default_notification_icon='https://yoursite.com/assets/img/icon.png' +``` + +**WARNING**: Read their docs, but you need **full HTTPS in your site** so this can work. + + ## Exceptions -If you build the wrong push message, you will get in the console and also an exception with information about it. +If you build the wrong push message or you create the wrong app, you will get in the console and also an exception with information about it. ## Copyright / License -Copyright 2017 SciFabric LTD. +Copyright 2017 Scifabric LTD. Source Code License: The GNU Affero General Public License, either version 3 of the License or (at your option) any later version. (see COPYING file) From 6b5efe763c84a0c7c6da7d5081e783537fcc85d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 27 Apr 2017 12:23:31 +0200 Subject: [PATCH 7/7] Bump version. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ec9bf41..4cb3306 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='pybossa-onesignal', - version='0.2', + version='1.0', packages=find_packages(), install_requires=['requests>=0.13.0'], # metadata for upload to PyPI