Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: move Message and Registration handlers to own validated class
Browse files Browse the repository at this point in the history
Continuing the effort to break up endpoint.py, move Message and
Registration handlers to their own marshmallow validated classes. I have
tried not to disturb tests unless absolutely required (e.g. @patch for
some reason stopped modifying `uuid.uuid4()`)

closes #491
  • Loading branch information
jrconlin committed Oct 6, 2016
1 parent 8e5f52a commit ea3b48c
Show file tree
Hide file tree
Showing 40 changed files with 1,188 additions and 880 deletions.
344 changes: 18 additions & 326 deletions autopush/endpoint.py

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions autopush/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@ def __init__(self, message, status_code=400, errno=None, headers=None):
class VapidAuthException(Exception):
"""Exception if the VAPID Auth token fails"""
pass


class MissingTableException(Exception):
"""Exception for missing tables"""
pass


class APNSException(Exception):
pass


class RouterException(AutopushException):
"""Exception if routing has failed, may include a custom status_code and
body to write to the response.
"""
def __init__(self, message, status_code=500, response_body="",
router_data=None, headers=None, log_exception=True,
errno=None, logged_status=None, **kwargs):
"""Create a new RouterException"""
super(AutopushException, self).__init__(message)
self.status_code = status_code
self.headers = {} if headers is None else headers
self.log_exception = log_exception
self.response_body = response_body or message
self.errno = errno
self.logged_status = logged_status
self.extra = kwargs


class LogCheckError(Exception):
"""Exception raised on purpose to check logging functions"""
pass
61 changes: 35 additions & 26 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@

import autopush.db as db
import autopush.utils as utils
from autopush.endpoint import (
EndpointHandler,
MessageHandler,
RegistrationHandler,
)
from autopush.log_check import LogCheckHandler
from autopush.health import (HealthHandler, StatusHandler)
from autopush.endpoint import EndpointHandler
from autopush.logging import PushLogger
from autopush.settings import AutopushSettings
from autopush.ssl import AutopushSSLContextFactory
from autopush.web.health import HealthHandler, StatusHandler
from autopush.web.limitedhttpconnection import LimitedHTTPConnection
from autopush.web.log_check import LogCheckHandler
from autopush.web.message import MessageHandler
from autopush.web.simplepush import SimplePushHandler
from autopush.web.registration import RegistrationHandler
from autopush.web.webpush import WebPushHandler
from autopush.websocket import (
PushServerProtocol,
RouterHandler,
Expand All @@ -30,10 +31,6 @@
DefaultResource,
StatusResource,
)
from autopush.web.simplepush import SimplePushHandler
from autopush.web.webpush import WebPushHandler
from autopush.web.limitedhttpconnection import LimitedHTTPConnection


shared_config_files = [
'/etc/autopush_shared.ini',
Expand All @@ -43,6 +40,23 @@
]
log = Logger()

# These are the known entry points for autopush. These are used here and in
# testing for consistency.
endpoint_paths = {
'route': r"/push/([^\/]+)",
'notification': r"/notif/([^\/]+)(/([^\/]+))?",
'old': r"/push/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
'simple': r"/spush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
'webpush': r"/wpush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
'message': r"/m/(?P<message_id>[^\/]+)",
'registration': r"/v1/(?P<router_type>[^\/]+)/(?P<router_token>[^\/]+)/"
r"registration(?:/(?P<uaid>[^\/]+))?(?:/subscription)?"
r"(?:/(?P<chid>[^\/]+))?",
'logcheck': r"/v1/err(?:/(?P<err_type>[^\/]+))?",
'status': r"^/status",
'health': r"^/health",
}


def add_shared_args(parser):
"""Add's a large common set of shared arguments"""
Expand Down Expand Up @@ -428,8 +442,8 @@ def mount_health_handlers(site, settings):
"""Create a health check HTTP handler on a cyclone site object"""
h_kwargs = dict(ap_settings=settings)
site.add_handlers(".*$", [
(r"^/status", StatusHandler, h_kwargs),
(r"^/health", HealthHandler, h_kwargs),
(endpoint_paths['status'], StatusHandler, h_kwargs),
(endpoint_paths['health'], HealthHandler, h_kwargs),
])


Expand Down Expand Up @@ -468,8 +482,8 @@ def connection_main(sysargs=None, use_files=True):
# Internal HTTP notification router
h_kwargs = dict(ap_settings=settings)
site = cyclone.web.Application([
(r"/push/([^\/]+)", RouterHandler, h_kwargs),
(r"/notif/([^\/]+)(/([^\/]+))?", NotificationHandler, h_kwargs),
(endpoint_paths['route'], RouterHandler, h_kwargs),
(endpoint_paths['notification'], NotificationHandler, h_kwargs),
],
default_host=settings.router_hostname, debug=args.debug,
log_function=skip_request_logging
Expand Down Expand Up @@ -566,17 +580,12 @@ def endpoint_main(sysargs=None, use_files=True):
# Endpoint HTTP router
h_kwargs = dict(ap_settings=settings)
site = cyclone.web.Application([
(r"/push/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
EndpointHandler, h_kwargs),
(r"/spush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
SimplePushHandler, h_kwargs),
(r"/wpush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
WebPushHandler, h_kwargs),
(r"/m/([^\/]+)", MessageHandler, h_kwargs),
(r"/v1/([^\/]+)/([^\/]+)/registration(?:/([^\/]+))"
"?(?:/subscription)?(?:/([^\/]+))?",
RegistrationHandler, h_kwargs),
(r"/v1/err(?:/([^\/]+))?", LogCheckHandler, h_kwargs),
(endpoint_paths['old'], EndpointHandler, h_kwargs),
(endpoint_paths['simple'], SimplePushHandler, h_kwargs),
(endpoint_paths['webpush'], WebPushHandler, h_kwargs),
(endpoint_paths['message'], MessageHandler, h_kwargs),
(endpoint_paths['registration'], RegistrationHandler, h_kwargs),
(endpoint_paths['logcheck'], LogCheckHandler, h_kwargs),
],
default_host=settings.hostname, debug=args.debug,
log_function=skip_request_logging
Expand Down
3 changes: 2 additions & 1 deletion autopush/router/apns2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from hyper import HTTP20Connection
from hyper.http20.exceptions import HTTP20Error

from autopush.router.interface import RouterException
from autopush.exceptions import RouterException


SANDBOX = 'api.development.push.apple.com'
SERVER = 'api.push.apple.com'
Expand Down
8 changes: 5 additions & 3 deletions autopush/router/apnsrouter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""APNS Router"""
import uuid

from twisted.logger import Logger
from twisted.internet.threads import deferToThread

from autopush.exceptions import RouterException
from autopush.router.apns2 import (
APNSClient,
APNS_MAX_CONNECTIONS,
)
from twisted.logger import Logger
from twisted.internet.threads import deferToThread
from autopush.router.interface import RouterException, RouterResponse
from autopush.router.interface import RouterResponse


# https://github.com/djacobs/PyAPNs
Expand Down
5 changes: 3 additions & 2 deletions autopush/router/fcm.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""FCM Router"""
import pyfcm

import pyfcm
from twisted.internet.threads import deferToThread
from twisted.logger import Logger

from autopush.router.interface import RouterException, RouterResponse
from autopush.exceptions import RouterException
from autopush.router.interface import RouterResponse


class FCMRouter(object):
Expand Down
7 changes: 4 additions & 3 deletions autopush/router/gcm.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""GCM Router"""
import gcmclient

import gcmclient
from twisted.internet.threads import deferToThread
from twisted.logger import Logger

from autopush.router.interface import RouterException, RouterResponse
from autopush.websocket import ms_time
from autopush.exceptions import RouterException
from autopush.router.interface import RouterResponse
from autopush.utils import ms_time


class GCMRouter(object):
Expand Down
20 changes: 0 additions & 20 deletions autopush/router/interface.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
"""Router interface"""
from autopush.exceptions import AutopushException


class RouterException(AutopushException):
"""Exception if routing has failed, may include a custom status_code and
body to write to the response.
"""
def __init__(self, message, status_code=500, response_body="",
router_data=None, headers=None, log_exception=True,
errno=None, logged_status=None, **kwargs):
"""Create a new RouterException"""
super(AutopushException, self).__init__(message)
self.status_code = status_code
self.headers = {} if headers is None else headers
self.log_exception = log_exception
self.response_body = response_body or message
self.errno = errno
self.logged_status = logged_status
self.extra = kwargs


class RouterResponse(object):
Expand Down
6 changes: 2 additions & 4 deletions autopush/router/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
from twisted.web._newclient import ResponseFailed
from twisted.web.client import FileBodyProducer

from autopush.exceptions import RouterException
from autopush.protocol import IgnoreBody
from autopush.router.interface import (
RouterException,
RouterResponse,
)
from autopush.router.interface import RouterResponse


class SimpleRouter(object):
Expand Down
7 changes: 4 additions & 3 deletions autopush/router/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
from twisted.internet.threads import deferToThread
from twisted.web.client import FileBodyProducer

from autopush.protocol import IgnoreBody
from autopush.router.interface import RouterException, RouterResponse
from autopush.router.simple import SimpleRouter
from autopush.db import (
dump_uaid,
hasher,
normalize_id,
)
from autopush.exceptions import RouterException
from autopush.protocol import IgnoreBody
from autopush.router.interface import RouterResponse
from autopush.router.simple import SimpleRouter

TTL_URL = "https://webpush-wg.github.io/webpush-protocol/#rfc.section.6.2"

Expand Down
Loading

0 comments on commit ea3b48c

Please sign in to comment.