Skip to content

Commit

Permalink
Merge pull request #8 from HDE/update-project-metadata
Browse files Browse the repository at this point in the history
Update project metadata, black, version scheme
  • Loading branch information
dimaqq authored Jan 17, 2019
2 parents b39f334 + 7a0751b commit 62090b6
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 429 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2017 HDE Inc
Copyright 2019 HDE Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AAPNS

[![CircleCI](https://circleci.com/gh/hde/aapns/tree/master.svg?style=svg)](https://circleci.com/gh/hde/aapns/tree/master)
[![CircleCI](https://circleci.com/gh/HDE/aapns/tree/master.svg?style=svg)](https://circleci.com/gh/HDE/aapns/tree/master)
[![Documentation Status](https://readthedocs.org/projects/aapns/badge/?version=latest)](http://aapns.readthedocs.io/en/latest/?badge=latest)

Asynchronous Apple Push Notification Service client.
Expand Down
52 changes: 24 additions & 28 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@
extensions = []

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# General information about the project.
project = 'AAPNS'
copyright = '2017, HDE Inc'
author = 'HDE Inc'
project = "AAPNS"
copyright = "2019, HDE Inc"
author = "HDE Inc"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '0.1'
version = "19.1"
# The full version, including alpha/beta/rc tags.
release = '0.1'
release = "19.1"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand All @@ -69,10 +69,10 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False
Expand All @@ -83,7 +83,7 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
html_theme = "alabaster"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand All @@ -94,13 +94,13 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]


# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'AAPNSdoc'
htmlhelp_basename = "AAPNSdoc"


# -- Options for LaTeX output ---------------------------------------------
Expand All @@ -109,15 +109,12 @@
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -127,19 +124,15 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'AAPNS.tex', 'AAPNS Documentation',
'HDE Inc', 'manual'),
(master_doc, "AAPNS.tex", "AAPNS Documentation", "HDE Inc", "manual")
]


# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'aapns', 'AAPNS Documentation',
[author], 1)
]
man_pages = [(master_doc, "aapns", "AAPNS Documentation", [author], 1)]


# -- Options for Texinfo output -------------------------------------------
Expand All @@ -148,10 +141,13 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'AAPNS', 'AAPNS Documentation',
author, 'AAPNS', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"AAPNS",
"AAPNS Documentation",
author,
"AAPNS",
"One line description of project.",
"Miscellaneous",
)
]



52 changes: 29 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import os
from setuptools import setup, find_packages

with open(os.path.relpath(f"{__file__}/../README.md")) as f:
readme = f.read()

setup(
version='1.0.0.dev8',
name='aapns',
package_dir={'': 'src'},
packages=find_packages(where='src'),
python_requires='>=3.6',
install_requires=[
'h2>3',
'attrs',
'structlog',
],
extras_require={
'cli': ['click']
},
entry_points={
'console_scripts': [
'aapns = aapns.cli:main'
]
version="19.1",
name="aapns",
package_dir={"": "src"},
packages=find_packages(where="src"),
python_requires=">=3.6",
install_requires=["h2>3", "attrs", "structlog"],
extras_require={"cli": ["click"]},
entry_points={"console_scripts": ["aapns = aapns.cli:main"]},
author="Jonas Obrist",
author_email="ojiidotch@gmail.com",
description="Asynchronous Apple Push Notification Service client",
long_description=readme,
long_description_content_type="text/markdown",
url="https://github.com/HDE/aapns",
project_urls={
"Documentation": "https://aapns.readthedocs.io/en/latest/",
"Code": "https://github.com/HDE/aapns",
"Issue tracker": "https://github.com/HDE/aapns/issues",
},
license='APLv2',
license="APLv2",
classifiers=[
'Framework :: AsyncIO',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3 :: Only',
'License :: OSI Approved :: Apache Software License'
]
"Framework :: AsyncIO",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3 :: Only",
"License :: OSI Approved :: Apache Software License",
],
)
8 changes: 7 additions & 1 deletion src/aapns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from .config import Production, ProductionAltPort, Development, DevelopmentAltPort, Priority
from .config import (
Production,
ProductionAltPort,
Development,
DevelopmentAltPort,
Priority,
)
from .api import connect
from .models import Notification, Alert, Localized
96 changes: 49 additions & 47 deletions src/aapns/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ async def __aenter__(self) -> APNSProtocol:
if not self.do_connect:
raise errors.Disconnected()
self.connection = APNSProtocol(
self.server.host,
self.logger,
self.clear_connection
self.server.host, self.logger, self.clear_connection
)
await loop.create_connection(
lambda: self.connection,
self.server.host,
self.server.port,
ssl=self.ssl_context
ssl=self.ssl_context,
)
return self.connection

Expand All @@ -65,42 +63,44 @@ def close(self):
self.connection.close()


def encode_request(*,
server: config.Server,
token: str,
notification: models.Notification,
apns_id: Optional[str]=None,
expiration: Optional[int]=None,
priority: config.Priority=config.Priority.normal,
topic: Optional[str]=None,
collapse_id: Optional[str]=None) -> Tuple[Headers, bytes]:
def encode_request(
*,
server: config.Server,
token: str,
notification: models.Notification,
apns_id: Optional[str] = None,
expiration: Optional[int] = None,
priority: config.Priority = config.Priority.normal,
topic: Optional[str] = None,
collapse_id: Optional[str] = None,
) -> Tuple[Headers, bytes]:
request_body = notification.encode()
request_headers = [
(':method', 'POST'),
(':authority', server.host),
(':scheme', 'https'),
(':path', f'/3/device/{token}'),
('content-length', str(len(request_body))),
('apns-priority', str(priority.value)),
(":method", "POST"),
(":authority", server.host),
(":scheme", "https"),
(":path", f"/3/device/{token}"),
("content-length", str(len(request_body))),
("apns-priority", str(priority.value)),
]

if apns_id:
request_headers.append(('apns-id', apns_id))
request_headers.append(("apns-id", apns_id))
if expiration:
request_headers.append(('apns-expiration', str(expiration)))
request_headers.append(("apns-expiration", str(expiration)))
if topic:
request_headers.append(('apns-topic', topic))
request_headers.append(("apns-topic", topic))
if collapse_id:
request_headers.append(('apns-collapse-id', collapse_id))
request_headers.append(("apns-collapse-id", collapse_id))
return request_headers, request_body


def handle_response(response: connection.Response) -> str:
response_id = response.headers.get('apns-id', '')
response_id = response.headers.get("apns-id", "")

if response.status != 200:
try:
reason = json.loads(response.body)['reason']
reason = json.loads(response.body)["reason"]
except:
reason = response.body
exc = errors.get(reason, response_id)
Expand All @@ -113,6 +113,7 @@ def ensure_task(coro: Coroutine) -> Coroutine:
@wraps(coro)
async def wrapper(*args, **kwargs):
return await asyncio.get_event_loop().create_task(coro(*args, **kwargs))

return wrapper


Expand All @@ -122,15 +123,17 @@ class APNS:
server: config.Server = attr.ib()

@ensure_task
async def send_notification(self,
token: str,
notification: models.Notification,
*,
apns_id: Optional[str]=None,
expiration: Optional[int]=None,
priority: config.Priority=config.Priority.normal,
topic: Optional[str]=None,
collapse_id: Optional[str]=None) -> str:
async def send_notification(
self,
token: str,
notification: models.Notification,
*,
apns_id: Optional[str] = None,
expiration: Optional[int] = None,
priority: config.Priority = config.Priority.normal,
topic: Optional[str] = None,
collapse_id: Optional[str] = None,
) -> str:
request_headers, request_body = encode_request(
token=token,
notification=notification,
Expand All @@ -143,29 +146,28 @@ async def send_notification(self,
)

async with self.connector as conn:
response = await conn.request(
headers=request_headers,
body=request_body,
)
response = await conn.request(headers=request_headers, body=request_body)

return handle_response(response)

async def close(self):
self.connector.close()


async def connect(client_cert_path: str,
server: config.Server,
*,
ssl_context: Optional[ssl.SSLContext]=None,
logger: Optional[BoundLogger]=None,
auto_reconnect: bool=False,
timeout: Optional[float]=None) -> APNS:
async def connect(
client_cert_path: str,
server: config.Server,
*,
ssl_context: Optional[ssl.SSLContext] = None,
logger: Optional[BoundLogger] = None,
auto_reconnect: bool = False,
timeout: Optional[float] = None,
) -> APNS:
if ssl_context is None:
ssl_context: ssl.SSLContext = ssl.create_default_context()
ssl_context.set_alpn_protocols(['h2'])
ssl_context.set_alpn_protocols(["h2"])
try:
ssl_context.set_npn_protocols(['h2'])
ssl_context.set_npn_protocols(["h2"])
except AttributeError:
pass
ssl_context.load_cert_chain(client_cert_path)
Expand Down
Loading

0 comments on commit 62090b6

Please sign in to comment.