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

Commit

Permalink
refactor: cleanup of Handler initialize/ap_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pjenvey committed Aug 26, 2016
1 parent 2f783fe commit 031e144
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 63 deletions.
4 changes: 4 additions & 0 deletions autopush/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def _finish_response(self, results):

class StatusHandler(cyclone.web.RequestHandler):
"""HTTP Status Handler"""

def initialize(self, ap_settings):
self.ap_settings = ap_settings

def get(self):
"""HTTP Get
Expand Down
36 changes: 14 additions & 22 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,10 @@ def skip_request_logging(handler):

def mount_health_handlers(site, settings):
"""Create a health check HTTP handler on a cyclone site object"""
status = StatusHandler
status.ap_settings = settings
health = HealthHandler
health.ap_settings = settings
h_kwargs = dict(ap_settings=settings)
site.add_handlers(".*$", [
(r"^/status", status),
(r"^/health", health),
(r"^/status", StatusHandler, h_kwargs),
(r"^/health", HealthHandler, h_kwargs),
])


Expand Down Expand Up @@ -432,15 +429,11 @@ def connection_main(sysargs=None, use_files=True):
if not settings:
return 1 # pragma: nocover

r = RouterHandler
r.ap_settings = settings
n = NotificationHandler
n.ap_settings = settings

# Internal HTTP notification router
h_kwargs = dict(ap_settings=settings)
site = cyclone.web.Application([
(r"/push/([^\/]+)", r),
(r"/notif/([^\/]+)(/([^\/]+))?", n),
(r"/push/([^\/]+)", RouterHandler, h_kwargs),
(r"/notif/([^\/]+)(/([^\/]+))?", NotificationHandler, h_kwargs),
],
default_host=settings.router_hostname, debug=args.debug,
log_function=skip_request_logging
Expand Down Expand Up @@ -536,20 +529,19 @@ def endpoint_main(sysargs=None, use_files=True):
return 1

# Endpoint HTTP router
h_kwargs = dict(ap_settings=settings)
site = cyclone.web.Application([
(r"/push/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)", EndpointHandler,
dict(ap_settings=settings)),
(r"/push/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
EndpointHandler, h_kwargs),
(r"/spush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
SimplePushHandler, dict(ap_settings=settings)),
SimplePushHandler, h_kwargs),
(r"/wpush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
WebPushHandler, dict(ap_settings=settings)),
(r"/m/([^\/]+)", MessageHandler, dict(ap_settings=settings)),
WebPushHandler, h_kwargs),
(r"/m/([^\/]+)", MessageHandler, h_kwargs),
(r"/v1/([^\/]+)/([^\/]+)/registration(?:/([^\/]+))"
"?(?:/subscription)?(?:/([^\/]+))?",
RegistrationHandler,
dict(ap_settings=settings)),
(r"/v1/err(?:/([^\/]+))?", LogCheckHandler,
dict(ap_settings=settings)),
RegistrationHandler, h_kwargs),
(r"/v1/err(?:/([^\/]+))?", LogCheckHandler, h_kwargs),
],
default_host=settings.hostname, debug=args.debug,
log_function=skip_request_logging
Expand Down
22 changes: 10 additions & 12 deletions autopush/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ def write(self, data):
class MessageTestCase(unittest.TestCase):
def setUp(self):
twisted.internet.base.DelayedCall.debug = True
settings = self.ap_settings = endpoint.MessageHandler.ap_settings =\
AutopushSettings(
hostname="localhost",
statsd_host=None,
crypto_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
)
settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
crypto_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
)
self.fernet_mock = settings.fernet = Mock(spec=Fernet)
self.metrics_mock = settings.metrics = Mock(spec=Metrics)
self.router_mock = settings.router = Mock(spec=Router)
Expand Down Expand Up @@ -1423,12 +1422,11 @@ class RegistrationTestCase(unittest.TestCase):

def setUp(self):
twisted.internet.base.DelayedCall.debug = True
settings = endpoint.RegistrationHandler.ap_settings =\
AutopushSettings(
hostname="localhost",
statsd_host=None,
bear_hash_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB=',
)
settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
bear_hash_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB=',
)
self.fernet_mock = settings.fernet = Mock(spec=Fernet)
self.metrics_mock = settings.metrics = Mock(spec=Metrics)
self.router_mock = settings.router = Mock(spec=Router)
Expand Down
13 changes: 7 additions & 6 deletions autopush/tests/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def setUp(self):
self.mock_dynamodb2 = mock_dynamodb2()
self.mock_dynamodb2.start()

ap_settings = self.settings = AutopushSettings(
settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
)
self.router_table = self.settings.router.table
self.storage_table = self.settings.storage.table
self.router_table = settings.router.table
self.storage_table = settings.storage.table

self.request_mock = Mock()
self.health = HealthHandler(Application(),
self.request_mock,
ap_settings=ap_settings)
ap_settings=settings)
self.health.log = self.log_mock = Mock(spec=Logger)
self.status_mock = self.health.set_status = Mock()
self.write_mock = self.health.write = Mock()
Expand Down Expand Up @@ -134,12 +134,13 @@ def setUp(self):
self.mock_dynamodb2 = mock_dynamodb2()
self.mock_dynamodb2.start()

self.settings = StatusHandler.ap_settings = AutopushSettings(
settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
)
self.request_mock = Mock()
self.status = StatusHandler(Application(), self.request_mock)
self.status = StatusHandler(Application(), self.request_mock,
ap_settings=settings)
self.write_mock = self.status.write = Mock()

def tearDown(self):
Expand Down
21 changes: 8 additions & 13 deletions autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,10 @@ def setUp(self):

# Websocket HTTP router
# Internal HTTP notification router
r = RouterHandler
r.ap_settings = settings
n = NotificationHandler
n.ap_settings = settings
h_kwargs = dict(ap_settings=settings)
ws_site = cyclone.web.Application([
(r"/push/([^\/]+)", r),
(r"/notif/([^\/]+)(/([^\/]+))?", n),
(r"/push/([^\/]+)", RouterHandler, h_kwargs),
(r"/notif/([^\/]+)(/([^\/]+))?", NotificationHandler, h_kwargs),
],
default_host=settings.router_hostname,
log_function=skip_request_logging,
Expand All @@ -374,18 +371,16 @@ def setUp(self):

# Endpoint HTTP router
site = cyclone.web.Application([
(r"/push/(v\d+)?/?([^\/]+)", EndpointHandler,
dict(ap_settings=settings)),
(r"/push/(v\d+)?/?([^\/]+)", EndpointHandler, h_kwargs),
(r"/spush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
SimplePushHandler, dict(ap_settings=settings)),
(r"/m/([^\/]+)", MessageHandler, dict(ap_settings=settings)),
SimplePushHandler, h_kwargs),
(r"/m/([^\/]+)", MessageHandler, h_kwargs),
(r"/wpush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
WebPushHandler, dict(ap_settings=settings)),
WebPushHandler, h_kwargs),

# PUT /register/ => connect info
# GET /register/uaid => chid + endpoint
(r"/register(?:/(.+))?", RegistrationHandler,
dict(ap_settings=settings)),
(r"/register(?:/(.+))?", RegistrationHandler, h_kwargs),
],
default_host=settings.hostname,
log_function=skip_request_logging,
Expand Down
18 changes: 8 additions & 10 deletions autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,15 +1937,14 @@ class RouterHandlerTestCase(unittest.TestCase):
def setUp(self):
twisted.internet.base.DelayedCall.debug = True

self.ap_settings = AutopushSettings(
self.ap_settings = settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
)
self.ap_settings.metrics = Mock(spec=Metrics)
h = RouterHandler
h.ap_settings = self.ap_settings
settings.metrics = Mock(spec=Metrics)
self.mock_request = Mock()
self.handler = h(Application(), self.mock_request)
self.handler = RouterHandler(Application(), self.mock_request,
ap_settings=settings)
self.handler.set_status = self.status_mock = Mock()
self.handler.write = self.write_mock = Mock()

Expand Down Expand Up @@ -1981,15 +1980,14 @@ class NotificationHandlerTestCase(unittest.TestCase):
def setUp(self):
twisted.internet.base.DelayedCall.debug = True

self.ap_settings = AutopushSettings(
self.ap_settings = settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
)
self.ap_settings.metrics = Mock(spec=Metrics)
h = NotificationHandler
h.ap_settings = self.ap_settings
settings.metrics = Mock(spec=Metrics)
self.mock_request = Mock()
self.handler = h(Application(), self.mock_request)
self.handler = NotificationHandler(Application(), self.mock_request,
ap_settings=settings)
self.handler.set_status = self.status_mock = Mock()
self.handler.write = self.write_mock = Mock()

Expand Down
6 changes: 6 additions & 0 deletions autopush/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,9 @@ class RouterHandler(cyclone.web.RequestHandler, ErrorLogger):
"""

def initialize(self, ap_settings):
self.ap_settings = ap_settings

def put(self, uaid):
"""HTTP Put
Expand Down Expand Up @@ -1340,6 +1343,9 @@ def put(self, uaid):

class NotificationHandler(cyclone.web.RequestHandler, ErrorLogger):

def initialize(self, ap_settings):
self.ap_settings = ap_settings

def put(self, uaid, *args):
"""HTTP Put
Expand Down

0 comments on commit 031e144

Please sign in to comment.