Created
December 28, 2011 21:49
-
-
Save adngdb/1529952 to your computer and use it in GitHub Desktop.
Generic Socorro Service
This file contains hidden or 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
class Service(JsonWebServiceBase): | |
""" | |
Base class for new-style REST API services. | |
Provide methods for arguments parsing and implementation finding. | |
""" | |
def __init__(self, uri): | |
uri_parts = uri.split("/") | |
self.implementation_class = uri_parts[0].capitalize() | |
self.implementation_method = None | |
if len(uri_parts) > 1: | |
self.implementation_method = uri_parts[1] | |
self.uri = "%s/(.*)" % uri | |
def get(self, *args): | |
"""Return the result of a GET HTTP method on this service's URL. """ | |
self.process("get", *args) | |
def post(self, *args): | |
"""Return the result of a POST HTTP method on this service's URL. """ | |
return self.process("create", *args) | |
def put(self, *args): | |
"""Return the result of a PUT HTTP method on this service's URL. """ | |
return self.process("update", *args) | |
def delete(self, *args): | |
"""Return the result of a DELETE HTTP method on this service's URL. """ | |
return self.process("delete", *args) | |
def process(self, action, *args): | |
""" | |
Return data from the implementation module of this service. | |
Given the configuration and an eventual force_api_impl argument, find | |
the right external module to retrieve data from. | |
Given the url and action, find the right class and method in that | |
module and call it. | |
Raise web-specific exceptions when necessary. | |
""" | |
if self.method is None: | |
method = action | |
else: | |
method = "%s_%s" % (action, self.method) | |
params = parse_query_string(args[0]) | |
impl = self.get_impl_class(self.cls, params) | |
try: | |
return getattr(impl, method)(**params) | |
except WrongOrMissingParameter: | |
raise web.webapi.BadRequest() | |
except Exception: | |
raise web.webapi.InternalError() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment