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

Commit

Permalink
refactor: clarify intent of write methods returning nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
pjenvey committed Aug 4, 2016
1 parent 966ada8 commit 94ab213
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
43 changes: 23 additions & 20 deletions autopush/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _router_response(self, response):
self.write(response.response_body)
self.finish()
else:
return self._write_response(
self._write_response(
response.status_code,
errno=response.errno or 999,
message=response.response_body)
Expand Down Expand Up @@ -497,8 +497,8 @@ def _uaid_lookup_results(self, result):
self.log.debug(
format="Invalid router requested", status_code=400,
errno=108, **self._client_info)
return self._write_response(400, 108,
message="Invalid router")
self._write_response(400, 108, message="Invalid router")
return

# Only simplepush uses version/data out of body/query, GCM/APNS will
# use data out of the request body 'WebPush' style.
Expand All @@ -516,14 +516,16 @@ def _uaid_lookup_results(self, result):
for x in req_fields]):
self.log.debug(format="Client error", status_code=400,
errno=101, **self._client_info)
return self._write_response(
self._write_response(
400, errno=101, message="Missing necessary crypto keys.")
return
if ("encryption-key" in self.request.headers and
"crypto-key" in self.request.headers):
self.log.debug(format="Client error", status_code=400,
errno=110, **self._client_info)
return self._write_response(
self._write_response(
400, 110, message="Invalid crypto headers")
return
self._client_info["message_size"] = len(data) if data else 0

if "ttl" not in self.request.headers:
Expand All @@ -535,13 +537,14 @@ def _uaid_lookup_results(self, result):
else:
self.log.debug(format="Client error", status_code=400,
errno=112, **self._client_info)
return self._write_response(400, 112, message="Invalid TTL header")
self._write_response(400, 112, message="Invalid TTL header")
return

if data and len(data) > self.ap_settings.max_data:
self.log.debug(format="Client error", status_code=400, errno=104,
**self._client_info)
return self._write_response(
413, 104, message="Data payload too large")
self._write_response(413, 104, message="Data payload too large")
return

if use_simplepush:
self._route_notification(self.version, result, data)
Expand Down Expand Up @@ -663,14 +666,14 @@ def post(self, router_type="", router_token="", uaid="", chid=""):
self.log.debug(format="Invalid router requested",
status_code=400, errno=108,
**self._client_info)
return self._write_response(
400, 108, message="Invalid router")
self._write_response(400, 108, message="Invalid router")
return
router = self.ap_settings.routers[router_type]

if uaid:
if not self._validate_auth(uaid):
return self._write_unauthorized_response(
message="Unauthorized")
self._write_unauthorized_response(message="Unauthorized")
return
else:
# No UAID supplied, make our own
uaid = uuid.uuid4().hex
Expand Down Expand Up @@ -703,17 +706,17 @@ def put(self, router_type="", router_token="", uaid="", chid=""):
self.start_time = time.time()

if not self._validate_auth(uaid):
return self._write_unauthorized_response(
message="Invalid Authentication")
self._write_unauthorized_response(message="Invalid Authentication")
return

params = self._load_params()
self.uaid = uaid
router_data = params
if router_type not in self.ap_settings.routers or not router_data:
self.log.debug(format="Invalid router requested", status_code=400,
errno=108, **self._client_info)
return self._write_response(
400, 108, message="Invalid router")
self._write_response(400, 108, message="Invalid router")
return
router = self.ap_settings.routers[router_type]

self.add_header("Content-Type", "application/json")
Expand Down Expand Up @@ -751,14 +754,14 @@ def delete(self, router_type="", router_token="", uaid="", chid=""):
"""
if not self._validate_auth(uaid):
return self._write_unauthorized_response(
message="Invalid Authentication")
self._write_unauthorized_response(message="Invalid Authentication")
return
if router_type not in self.ap_settings.routers:
self.log.debug(format="Invalid router requested",
status_code=400, errno=108,
**self._client_info)
return self._write_response(
400, 108, message="Invalid router")
self._write_response(400, 108, message="Invalid router")
return

if chid:
# mark channel as dead
Expand Down
2 changes: 1 addition & 1 deletion autopush/web/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def _router_response(self, response):
self.write(response.response_body)
self.finish()
else:
return self._write_response(
self._write_response(
response.status_code,
errno=response.errno or 999,
message=response.response_body)
Expand Down
7 changes: 4 additions & 3 deletions autopush/web/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def _validate_request(self, request_handler):
def _call_func(self, result, func, request_handler, *args, **kwargs):
output, errors = result
if errors:
return request_handler._write_validation_err(errors)
request_handler.valid_input = output
return func(request_handler, *args, **kwargs)
request_handler._write_validation_err(errors)
else:
request_handler.valid_input = output
return func(request_handler, *args, **kwargs)

def _decorator(self, func):
@wraps(func)
Expand Down
16 changes: 10 additions & 6 deletions autopush/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,18 +1318,20 @@ def put(self, uaid):
if not client:
self.set_status(404)
settings.metrics.increment("updates.router.disconnected")
return self.write("Client not connected.")
self.write("Client not connected.")
return

if client.paused:
self.set_status(503)

settings.metrics.increment("updates.router.busy")
return self.write("Client busy.")
self.write("Client busy.")
return

update = json.loads(self.request.body)
client.send_notifications(update)
settings.metrics.increment("updates.router.received")
return self.write("Client accepted for delivery")
self.write("Client accepted for delivery")


class NotificationHandler(cyclone.web.RequestHandler, ErrorLogger):
Expand All @@ -1346,14 +1348,16 @@ def put(self, uaid, *args):
if not client:
self.set_status(404)
settings.metrics.increment("updates.notification.disconnected")
return self.write("Client not connected.")
self.write("Client not connected.")
return

if client.paused:
# Client already busy waiting for stuff, flag for check
client._check_notifications = True
self.set_status(202)
settings.metrics.increment("updates.notification.flagged")
return self.write("Flagged for Notification check")
self.write("Flagged for Notification check")
return

# Client is online and idle, start a notification check
client.process_notifications()
Expand All @@ -1369,7 +1373,7 @@ def delete(self, uaid, ignored, connectionTime):
client = self.ap_settings.clients.get(uaid)
if client and client.ps.connected_at == int(connectionTime):
client.sendClose()
return self.write("Terminated duplicate")
self.write("Terminated duplicate")


class DefaultResource(Resource):
Expand Down

0 comments on commit 94ab213

Please sign in to comment.