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

Commit

Permalink
refactor: ap_settings -> conf
Browse files Browse the repository at this point in the history
  • Loading branch information
pjenvey committed Jul 31, 2017
1 parent fe2259b commit e53a2ed
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 107 deletions.
8 changes: 4 additions & 4 deletions autopush/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def initialize(self):
self._client_info = self._init_info()

@property
def ap_settings(self):
def conf(self):
# type: () -> AutopushConfig
return self.application.ap_settings
return self.application.conf

@property
def db(self):
Expand All @@ -38,7 +38,7 @@ def metrics(self):

def _init_info(self):
return dict(
ami_id=self.ap_settings.ami_id,
ami_id=self.conf.ami_id,
request_id=str(uuid.uuid4()),
user_agent=self.request.headers.get('user-agent', ""),
remote_ip=self.request.headers.get('x-forwarded-for',
Expand Down Expand Up @@ -78,7 +78,7 @@ def authenticate_peer_cert(self):
if cert:
cert_signature = cert.digest('sha256')
cn = cert.get_subject().CN
auth = self.ap_settings.client_certs.get(cert_signature)
auth = self.conf.client_certs.get(cert_signature)
if auth is not None:
# TLS authenticated
self._client_info.update(tls_auth=auth,
Expand Down
46 changes: 23 additions & 23 deletions autopush/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ class BaseHTTPFactory(cyclone.web.Application):
)

def __init__(self,
ap_settings, # type: AutopushConfig
conf, # type: AutopushConfig
db, # type: DatabaseManager
handlers=None, # type: APHandlers
log_function=skip_request_logging, # type: CycloneLogger
**kwargs):
# type: (...) -> None
self.ap_settings = ap_settings
self.conf = conf
self.db = db
self.noisy = ap_settings.debug
self.noisy = conf.debug

cyclone.web.Application.__init__(
self,
handlers=self.ap_handlers if handlers is None else handlers,
default_host=self._hostname,
debug=ap_settings.debug,
debug=conf.debug,
log_function=log_function,
**kwargs
)
Expand All @@ -90,12 +90,12 @@ def add_health_handlers(self):

@property
def _hostname(self):
return self.ap_settings.hostname
return self.conf.hostname

@classmethod
def for_handler(cls,
handler_cls, # Type[BaseHTTPFactory]
ap_settings, # type: AutopushConfig
conf, # type: AutopushConfig
db=None, # type: Optional[DatabaseManager]
**kwargs):
# type: (...) -> BaseHTTPFactory
Expand All @@ -113,9 +113,9 @@ def for_handler(cls,
for pattern, handler in cls.ap_handlers + cls.health_ap_handlers:
if handler is handler_cls:
if db is None:
db = DatabaseManager.from_config(ap_settings)
db = DatabaseManager.from_config(conf)
return cls._for_handler(
ap_settings,
conf,
db=db,
handlers=[(pattern, handler)],
**kwargs
Expand All @@ -124,7 +124,7 @@ def for_handler(cls,
handler_cls)) # pragma: nocover

@classmethod
def _for_handler(cls, ap_settings, **kwargs):
def _for_handler(cls, conf, **kwargs):
# type: (AutopushConfig, **Any) -> BaseHTTPFactory
"""Create an instance w/ default kwargs for for_handler"""
raise NotImplementedError # pragma: nocover
Expand Down Expand Up @@ -153,18 +153,18 @@ class EndpointHTTPFactory(BaseHTTPFactory):
protocol = LimitedHTTPConnection

def __init__(self,
ap_settings, # type: AutopushConfig
conf, # type: AutopushConfig
db, # type: DatabaseManager
routers, # type: Dict[str, IRouter]
**kwargs):
# type: (...) -> None
if ap_settings.enable_simplepush:
if conf.enable_simplepush:
self.ap_handlers += (
(r"/spush/(?:(?P<api_ver>v\d+)\/)?(?P<token>[^\/]+)",
SimplePushHandler),
)
self.ap_handlers = tuple(self.ap_handlers)
BaseHTTPFactory.__init__(self, ap_settings, db=db, **kwargs)
BaseHTTPFactory.__init__(self, conf, db=db, **kwargs)
self.routers = routers

def ssl_cf(self):
Expand All @@ -175,18 +175,18 @@ def ssl_cf(self):
values.
"""
settings = self.ap_settings
settings = self.conf
return settings.ssl.cf(require_peer_certs=settings.enable_tls_auth)

@classmethod
def _for_handler(cls, ap_settings, db, routers=None, **kwargs):
def _for_handler(cls, conf, db, routers=None, **kwargs):
if routers is None:
routers = routers_from_config(
ap_settings,
conf,
db=db,
agent=agent_from_config(ap_settings)
agent=agent_from_config(conf)
)
return cls(ap_settings, db=db, routers=routers, **kwargs)
return cls(conf, db=db, routers=routers, **kwargs)


class InternalRouterHTTPFactory(BaseHTTPFactory):
Expand All @@ -197,17 +197,17 @@ class InternalRouterHTTPFactory(BaseHTTPFactory):
)

def __init__(self,
ap_settings, # type: AutopushConfig
conf, # type: AutopushConfig
db, # type: DatabaseManager
clients, # type: Dict[str, PushServerProtocol]
**kwargs):
# type: (...) -> None
BaseHTTPFactory.__init__(self, ap_settings, db, **kwargs)
BaseHTTPFactory.__init__(self, conf, db, **kwargs)
self.clients = clients

@property
def _hostname(self):
return self.ap_settings.router_hostname
return self.conf.router_hostname

def ssl_cf(self):
# type: () -> Optional[AutopushSSLContextFactory]
Expand All @@ -217,13 +217,13 @@ def ssl_cf(self):
values.
"""
return self.ap_settings.router_ssl.cf()
return self.conf.router_ssl.cf()

@classmethod
def _for_handler(cls, ap_settings, db, clients=None, **kwargs):
def _for_handler(cls, conf, db, clients=None, **kwargs):
if clients is None:
clients = {}
return cls(ap_settings, db=db, clients=clients, **kwargs)
return cls(conf, db=db, clients=clients, **kwargs)


class MemUsageHTTPFactory(BaseHTTPFactory):
Expand Down
13 changes: 5 additions & 8 deletions autopush/router/apnsrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,25 @@ def _connect(self, rel_channel, load_connections=True):
metrics=self.metrics,
load_connections=load_connections)

def __init__(self, ap_settings, router_conf, metrics,
load_connections=True):
def __init__(self, conf, router_conf, metrics, load_connections=True):
"""Create a new APNS router and connect to APNS
:param ap_settings: Configuration settings
:type ap_settings: autopush.config.AutopushConfig
:param conf: Configuration settings
:type conf: autopush.config.AutopushConfig
:param router_conf: Router specific configuration
:type router_conf: dict
:param load_connections: (used for testing)
:type load_connections: bool
"""
self.ap_settings = ap_settings
self.conf = conf
self.router_conf = router_conf
self.metrics = metrics
self._base_tags = ["platform:apns"]
self.apns = dict()
for rel_channel in router_conf:
self.apns[rel_channel] = self._connect(rel_channel,
load_connections)
self.ap_settings = ap_settings
self.log.debug("Starting APNS router...")

def register(self, uaid, router_data, app_id, *args, **kwargs):
Expand Down Expand Up @@ -166,8 +164,7 @@ def _route(self, notification, router_data):
response_body="APNS returned an error processing request",
)

location = "%s/m/%s" % (self.ap_settings.endpoint_url,
notification.version)
location = "%s/m/%s" % (self.conf.endpoint_url, notification.version)
self.metrics.increment("notification.bridge.sent",
tags=make_tags(self._base_tags,
application=rel_channel))
Expand Down
7 changes: 3 additions & 4 deletions autopush/router/fcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class FCMRouter(object):
}
}

def __init__(self, ap_settings, router_conf, metrics):
def __init__(self, conf, router_conf, metrics):
"""Create a new FCM router and connect to FCM"""
self.ap_settings = ap_settings
self.conf = conf
self.router_conf = router_conf
self.metrics = metrics
self.min_ttl = router_conf.get("ttl", 60)
Expand Down Expand Up @@ -272,8 +272,7 @@ def _process_reply(self, reply, notification, router_data, ttl):
notification.data_length,
tags=make_tags(self._base_tags,
destination="Direct"))
location = "%s/m/%s" % (self.ap_settings.endpoint_url,
notification.version)
location = "%s/m/%s" % (self.conf.endpoint_url, notification.version)
return RouterResponse(status_code=201, response_body="",
headers={"TTL": ttl,
"Location": location},
Expand Down
7 changes: 3 additions & 4 deletions autopush/router/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class GCMRouter(object):
collapseKey = "simplepush"
MAX_TTL = 2419200

def __init__(self, ap_settings, router_conf, metrics):
def __init__(self, conf, router_conf, metrics):
"""Create a new GCM router and connect to GCM"""
self.ap_settings = ap_settings
self.conf = conf
self.router_conf = router_conf
self.metrics = metrics
self.min_ttl = router_conf.get("ttl", 60)
Expand Down Expand Up @@ -198,8 +198,7 @@ def _process_reply(self, reply, uaid_data, ttl, notification):
notification.data_length,
tags=make_tags(self._base_tags,
destination='Direct'))
location = "%s/m/%s" % (self.ap_settings.endpoint_url,
notification.version)
location = "%s/m/%s" % (self.conf.endpoint_url, notification.version)
return RouterResponse(status_code=201, response_body="",
headers={"TTL": ttl,
"Location": location},
Expand Down
12 changes: 5 additions & 7 deletions autopush/router/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class WebPushRouter(object):
"""
log = Logger()

def __init__(self, ap_settings, router_conf, db, agent):
def __init__(self, conf, router_conf, db, agent):
"""Create a new Router"""
self.ap_settings = ap_settings
self.conf = conf
self.router_conf = router_conf
self.db = db
self.agent = agent
Expand Down Expand Up @@ -171,8 +171,7 @@ def delivered_response(self, notification):
self.metrics.increment("notification.message_data",
notification.data_length,
tags=make_tags(destination='Stored'))
location = "%s/m/%s" % (self.ap_settings.endpoint_url,
notification.location)
location = "%s/m/%s" % (self.conf.endpoint_url, notification.location)
return RouterResponse(status_code=201, response_body="",
headers={"Location": location,
"TTL": notification.ttl or 0},
Expand All @@ -182,8 +181,7 @@ def stored_response(self, notification):
self.metrics.increment("notification.message_data",
notification.data_length,
tags=make_tags(destination='Direct'))
location = "%s/m/%s" % (self.ap_settings.endpoint_url,
notification.location)
location = "%s/m/%s" % (self.conf.endpoint_url, notification.location)
return RouterResponse(status_code=201, response_body="",
headers={"Location": location,
"TTL": notification.ttl},
Expand Down Expand Up @@ -243,7 +241,7 @@ def _save_notification(self, uaid_data, notification):
log_exception=False,
)
if notification.ttl == 0:
location = "%s/m/%s" % (self.ap_settings.endpoint_url,
location = "%s/m/%s" % (self.conf.endpoint_url,
notification.version)
raise RouterException("Finished Routing", status_code=201,
log_exception=False,
Expand Down
8 changes: 4 additions & 4 deletions autopush/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ def test_cors(self):
ch1 = "Access-Control-Allow-Origin"
ch2 = "Access-Control-Allow-Methods"
reg = self.reg
reg.ap_settings.cors = False
reg.conf.cors = False
ok_(reg._headers.get(ch1) != "*")
ok_(reg._headers.get(ch2) != self.CORS_HEAD)

reg.clear_header(ch1)
reg.clear_header(ch2)
reg.ap_settings.cors = True
reg.conf.cors = True
reg.prepare()
eq_(reg._headers[ch1], "*")
eq_(reg._headers[ch2], self.CORS_HEAD)
Expand All @@ -238,7 +238,7 @@ def test_cors_head(self):
ch1 = "Access-Control-Allow-Origin"
ch2 = "Access-Control-Allow-Methods"
reg = self.reg
reg.ap_settings.cors = True
reg.conf.cors = True
reg.prepare()
reg.head(None)
eq_(reg._headers[ch1], "*")
Expand All @@ -248,7 +248,7 @@ def test_cors_options(self):
ch1 = "Access-Control-Allow-Origin"
ch2 = "Access-Control-Allow-Methods"
reg = self.reg
reg.ap_settings.cors = True
reg.conf.cors = True
reg.prepare()
reg.options(None)
eq_(reg._headers[ch1], "*")
Expand Down
8 changes: 4 additions & 4 deletions autopush/tests/test_web_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ def test_cors(self):
ch3 = "Access-Control-Allow-Headers"
ch4 = "Access-Control-Expose-Headers"
base = self.base
base.ap_settings.cors = False
base.conf.cors = False
ok_(base._headers.get(ch1) != "*")
ok_(base._headers.get(ch2) != self.CORS_METHODS)
ok_(base._headers.get(ch3) != self.CORS_HEADERS)
ok_(base._headers.get(ch4) != self.CORS_RESPONSE_HEADERS)

base.clear_header(ch1)
base.clear_header(ch2)
base.ap_settings.cors = True
base.conf.cors = True
self.base.prepare()
eq_(base._headers[ch1], "*")
eq_(base._headers[ch2], self.CORS_METHODS)
Expand All @@ -91,7 +91,7 @@ def test_cors_head(self):
ch3 = "Access-Control-Allow-Headers"
ch4 = "Access-Control-Expose-Headers"
base = self.base
base.ap_settings.cors = True
base.conf.cors = True
base.prepare()
args = {"api_ver": "v1", "token": "test"}
base.head(args)
Expand All @@ -109,7 +109,7 @@ def test_cors_options(self):
# autopush.main.endpoint_main
args = {"api_ver": "v1", "token": "test"}
base = self.base
base.ap_settings.cors = True
base.conf.cors = True
base.prepare()
base.options(args)
eq_(base._headers[ch1], "*")
Expand Down
2 changes: 1 addition & 1 deletion autopush/tests/test_web_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _write_validation_err(rh, errors):
app.ui_methods = dict()
vr = ValidateRequest(app, request)
vr._timings = dict()
vr.ap_settings = Mock()
vr.conf = Mock()
vr.metrics = Mock()
vr.db = Mock()
vr.routers = Mock()
Expand Down
Loading

0 comments on commit e53a2ed

Please sign in to comment.