This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract a simple BaseHandler
- Loading branch information
Showing
12 changed files
with
76 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import uuid | ||
|
||
import cyclone.web | ||
from twisted.logger import Logger | ||
from twisted.python import failure | ||
|
||
|
||
class BaseHandler(cyclone.web.RequestHandler): | ||
"""Base cyclone RequestHandler for autopush""" | ||
|
||
log = Logger() | ||
|
||
def initialize(self, ap_settings): | ||
"""Setup basic attributes from AutopushSettings""" | ||
self.ap_settings = ap_settings | ||
self._client_info = self._init_info() | ||
|
||
def _init_info(self): | ||
return dict( | ||
ami_id=self.ap_settings.ami_id, | ||
request_id=str(uuid.uuid4()), | ||
user_agent=self.request.headers.get('user-agent', ""), | ||
remote_ip=self.request.headers.get('x-forwarded-for', | ||
self.request.remote_ip), | ||
authorization=self.request.headers.get('authorization', ""), | ||
message_ttl=self.request.headers.get('ttl', ""), | ||
) | ||
|
||
def write_error(self, code, **kwargs): | ||
"""Write the error (otherwise unhandled exception when dealing with | ||
unknown method specifications.) | ||
This is a Cyclone API Override method used by endpoint and | ||
websocket. | ||
""" | ||
self.set_status(code) | ||
if 'exc_info' in kwargs: | ||
self.log.failure( | ||
format=kwargs.get('format', "Exception"), | ||
failure=failure.Failure(*kwargs['exc_info']), | ||
**self._client_info) | ||
else: | ||
self.log.failure("Error in handler: %s" % code, | ||
**self._client_info) | ||
self.finish() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.