Skip to content

Commit

Permalink
Merge pull request #3 from Scifabric/refactor
Browse files Browse the repository at this point in the history
Refactor.
  • Loading branch information
teleyinex authored Apr 27, 2017
2 parents 9c1b41b + 6b5efe7 commit 7a942f5
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 22 deletions.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
23 changes: 18 additions & 5 deletions pbsonesignal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -57,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/",
Expand All @@ -79,6 +80,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,
Expand Down Expand Up @@ -135,6 +144,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,
Expand Down
11 changes: 10 additions & 1 deletion pbsonesignal/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"""

__all__ = ['AppIdsMissing', 'AppIdMissing', 'ApiKeyMissing', 'AppIdDuplicate',
'CreateNotification', 'CreateApp']
'CreateNotification', 'CreateApp', 'AuthKeyMissing',
'ApiAuthKeysMissing']


class AppIdMissing(Exception):
Expand All @@ -44,6 +45,14 @@ class ApiKeyMissing(Exception):
pass


class AuthKeyMissing(Exception):
pass


class ApiAuthKeysMissing(Exception):
pass


class CreateNotification(Exception):
pass

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 4 additions & 13 deletions tests/test_onesignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,16 @@ 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")
@raises(ApiAuthKeysMissing)
def test_create_client(self):
"""Test create client without keys."""
PybossaOneSignal()

def test_headers(self):
"""Test headers method."""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_push_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7a942f5

Please sign in to comment.