Skip to content
Closed
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
15 changes: 2 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,8 @@ Or you can install it directly from GitHub_:
Overview
--------

After being imported, the module must be initialized by calling the ``init``
function with a valid application token. Thus, a typical use of the
``pushover`` module looks like this:

.. code-block:: python

from pushover import init, Client

init("<token>")
Client("<user-key>").send_message("Hello!", title="Hello")

You can also pass the ``api_token`` optional argument to ``Client`` to
initialize the module at the same time:
After being imported, the module must be initialized by passing the
``user_key`` and the ``api_token`` to ``Client``:

.. code-block:: python

Expand Down
35 changes: 8 additions & 27 deletions pushover.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import requests

__all__ = ["init", "get_sounds", "Client", "MessageRequest",
"InitError", "RequestError", "UserError"]
__all__ = ["get_sounds", "Client", "MessageRequest",
"APITokenError", "RequestError", "UserError"]

BASE_URL = "https://api.pushover.net/1/"
MESSAGE_URL = BASE_URL + "messages.json"
Expand All @@ -32,7 +32,6 @@
GLANCE_URL = BASE_URL + "glances.json"

SOUNDS = None
TOKEN = None


def get_sounds():
Expand All @@ -49,25 +48,13 @@ def get_sounds():
return SOUNDS


def init(token, sound=False):
"""Initialize the module by setting the application token which will be
used to send messages. If ``sound`` is ``True`` also returns the list of
valid sounds by calling the :func:`get_sounds` function.
"""
global TOKEN
TOKEN = token
if sound:
return get_sounds()


class InitError(Exception):
class APITokenError(Exception):
"""Exception which is raised when trying to send a message before
initializing the module.
"""

def __str__(self):
return ("No api_token provided. Init the pushover module by "
"calling the init function")
return ("No api_token attribute provided.")


class UserError(Exception):
Expand Down Expand Up @@ -100,10 +87,6 @@ class Request:
"""

def __init__(self, request_type, url, payload, files={}):
if not TOKEN:
raise InitError

payload["token"] = TOKEN
request = getattr(requests, request_type)(url, params=payload, files=files)
self.answer = request.json()
if 400 <= request.status_code < 500:
Expand Down Expand Up @@ -213,6 +196,9 @@ def __init__(self, user_key=None, device=None, api_token=None,
self.user_key = params["user_key"]
if not self.user_key:
raise UserError
self.api_token = params["api_token"]
if not self.api_token:
raise APITokenError
self.device = params["device"]
self.devices = []

Expand Down Expand Up @@ -251,7 +237,7 @@ def send_message(self, message, attachment=None, **kwords):
"timestamp", "url", "url_title", "device",
"retry", "expire", "html"]

payload = {"message": message, "user": self.user_key}
payload = {"message": message, "user": self.user_key, "token": self.api_token}
files = {'attachment': attachment} if attachment else {}
if self.device:
payload["device"] = self.device
Expand Down Expand Up @@ -317,11 +303,6 @@ def _get_config(profile='Default', config_path='~/.pushoverrc',
if device:
params["device"] = device

if not TOKEN:
init(params["api_token"])
if not TOKEN:
raise InitError

return params


Expand Down