Mapping python collections builtin methods to REST API patterns #9
Replies: 4 comments
-
Dear Geppetto, We have tools to map various data systems (local/remote files or blob storage, data bases, etc.) and formats to facades that use builtin python collections methods as their interface (as in, those listed in the "Collections Abstract Base Classes" table that can be found here: https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes). Now, we'd like to map these methods to standard, best-practice, REST web-service methods or design patterns. We'd like to have a list, as exhaustive as possible, that shows how we can map these python collection operations to, and from, REST web-service calls. Geppetto: c requirements of your application and the nature of your data.
|
Beta Was this translation helpful? Give feedback.
-
Our web-service is going to forward requests to a python backend, which is going to call one or several of these builtin collection methods. This may result in some errors, which need to then be forwarded to the the web-service caller, using various standard HTTP status codes, and standard message formats. Here again, we need to figure out what a good mapping from the python errors (type and message) to REST web-service (http status code and message) should be. What are the 10 most useful mappings we should implement? Geppetto:
|
Beta Was this translation helpful? Give feedback.
-
Here's some base python function that you can use to handle the error mapping described above. dflt_error_mapping = {
"IndexError": (404, "Resource not found."),
"TypeError": (400, "Invalid type provided."),
"ValueError": (400, "Invalid value provided."),
"KeyError": (404, "Resource not found."),
"AttributeError": (400, "Invalid attribute."),
"StopIteration": (410, "No more items to iterate."),
"NotImplementedError": (501, "Method not implemented."),
"MemoryError": (507, "Server is out of memory."),
"TimeoutError": (408, "Request timed out."),
"PermissionError": (403, "Permission denied.")
}
def python_error_object_to_http_error_json(py_error: Exception, error_mapping: dict = None):
"""Takes a `py_error` object and returns some info for a http service error.
That is, a json-serializable dict can then use in the http service response.
This function does it's best to parse out extra information from the python error
message and use it as extra info in the web service response.
Example usage:
>>> try:
... # Some code that raises an exception...
... raise ValueError("Invalid value: 'abc'")
... except Exception as e:
... error_json = python_error_object_to_http_error_json(e)
... print(json.dumps(error_json, indent=4))
{
"status": 400,
"message": "Invalid value provided.",
"details": "Invalid value: 'abc'"
}
"""
if error_mapping is None:
error_mapping = dflt_error_mapping
error_type = type(py_error).__name__
if error_type in error_mapping:
http_status_code, http_message = error_mapping[error_type]
response = {
"status": http_status_code,
"message": http_message,
"details": str(py_error)
}
else:
# If the error type is not in the mapping, return a generic server error
response = {
"status": 500,
"message": "Internal Server Error",
"details": str(py_error)
}
return response |
Beta Was this translation helpful? Give feedback.
-
Note that a mechanism to be able to dispatch any object has been added. Here's an example: handlers = [
dict(
endpoint=my_store,
name='some_store',
attr_names=['__iter__', '__getitem__'],
),
my_func,
]
mk_app(handlers, **config) |
Beta Was this translation helpful? Give feedback.
-
We have packages like dol and creek to help use build python-builtin-like interfaces to various data systems and formats.
That is, we aim at making operations on data should be done through a facade with maximum reuse between various use-case. Whenever possible (and not a ridiculous stretch) we chose our facade's base/inner_facing interface from python builtin interfaces. For example, from the abstract methods of python collections. from any data particulars.
Here, we want tools to map these python data facades to REST design patterns, automatically, via py2http tools.
Proposed interface:
In the above, we didn't have to specify any methods that need to be "exposed", because the function had it's own "routing" rules that associated, automatically, a set of methods for that collection (based, possibly, on it's type, name, and any other properties).
These rules should be parametrizable (plugin interface).
Optionally, the user can explicitly state the methods they want to expose.
In the example below, we want to be able to "list" objects as well as "get" them.
Perhaps the
collection
is a mapping, and we want to list keys (i.e. "ids", or "references") that can then be used to actually retrieve the object.Beta Was this translation helpful? Give feedback.
All reactions