Skip to content

Commit

Permalink
Initial revision
Browse files Browse the repository at this point in the history
  • Loading branch information
vit1251 committed Apr 21, 2019
1 parent 68b8695 commit 61617aa
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
50 changes: 50 additions & 0 deletions aiopo/_PushoverClient.py
@@ -0,0 +1,50 @@

from logging import getLogger
from aiohttp import ClientSession


class PushoverClient(object):
def __init__(self, app_token, user_key):
self.__log = getLogger('aiopo')
self._url = 'https://api.pushover.net/1/messages.json'
self._app_token = app_token
self._user_key = user_key

async def notify(self, message):
""" Push message
Pushover API request is HTTPS POST request:
token (required) - your application's API token
user (required) - the user/group key (not e-mail address) of your user (or you), viewable when logged into our dashboard (often referred to as USER_KEY in our documentation and code examples)
message (required) - your message
Some optional parameters may be included:
attachment - an image attachment to send with the message; see attachments for more information on how to upload files
device - your user's device name to send the message directly to that device, rather than all of the user's devices (multiple devices may be separated by a comma)
title - your message's title, otherwise your app's name is used
url - a supplementary URL to show with your message
url_title - a title for your supplementary URL, otherwise just the URL is shown
priority - send as -2 to generate no notification/alert, -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user
sound - the name of one of the sounds supported by device clients to override the user's default sound choice
timestamp - a Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API
Response:
DEBUG:aiopo:200
DEBUG:aiopo:{"status":1,"request":"c36a3056-4377-4d88-9543-f6e5da94c781"}
"""
payload = {
'token': self._app_token,
'user': self._user_key,
'message': message,
}
async with ClientSession() as session:
async with session.post(self._url, data=payload) as resp:
self.__log.debug("status = {status!r}".format(status=resp.status))
if resp.status != 200:
raise RuntimeError('Pushover response code {status}'.format(status=resp.status))
values = await resp.json()
return values
2 changes: 2 additions & 0 deletions aiopo/__init__.py
@@ -0,0 +1,2 @@

from ._PushoverClient import PushoverClient
36 changes: 36 additions & 0 deletions example/01_aiopo_example.py
@@ -0,0 +1,36 @@
#!/usr/bin/python

from logging import getLogger, basicConfig, DEBUG
from asyncio import get_event_loop
from aiopo import PushoverClient

APP_TOKEN = '...'
USER_KEY = '...'
MESSAGE = 'Hello, world!'

class Application(object):
def __init__(self):
self.__log = getLogger('aiopo-example')
self.loop = None
self.po = None

async def start(self):
""" Start corotine
"""
self.po = PushoverClient(app_token=APP_TOKEN, user_key=USER_KEY)
await self.po.notify(message=MESSAGE)

async def stop(self):
""" Stop corotine
"""

def run(self):
self.loop = get_event_loop()
#
self.loop.create_task(self.start())
self.loop.run_forever()

if __name__ == "__main__":
basicConfig(filename="debug.log", level=DEBUG)
app = Application()
app.run()
45 changes: 45 additions & 0 deletions setup.py
@@ -0,0 +1,45 @@
#!/usr/bin/python3

from sys import version_info
from setuptools import setup

if version_info < (3, 5, 3):
raise RuntimeError("aiopo requires Python 3.5.3+")

setup(
name='aiopo',
version='1.0',
description='Async Pushover client (asyncio)',
classifiers=[
'Intended Audience :: Developers',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
# 'Programming Language :: Python :: 3.6',
# 'Programming Language :: Python :: 3.7',
'Operating System :: POSIX',
# 'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Development Status :: 4 - Beta',
# 'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: MIT License',
],
author='Vitold Sedyshev',
author_email='vit1251@gmail.com',
maintainer=', '.join([
'Vitold Sedyshev <vit1251@gmail.com>',
]),
maintainer_email='aiopo@googlegroups.com',
url='https://github.com/vit1251/aiopo',
project_urls={
# 'CI: Travis': '...',
# 'Coverage: codecov': '...',
# 'GitHub: issues': '',
# 'GitHub: repo': '',
},
license='MIT',
packages=['aiopo'],
python_requires='>=3.5.3',
install_requires=['aiohttp'],
include_package_data=True,
)

0 comments on commit 61617aa

Please sign in to comment.