Routing
For the REST API, we use Flask as a library.
Info
This part of the documentation further outlines the technical details about REST APIs and less about the project itself. Read this part if you want to learn more about adding your own endpoints. For the existing endpoints, please check out the endpoint documentation.
Routing in Python
Routes in Flask generally consist of a decorator and a method. You need to have initialized a Flask app first in order to set up routes.
The RestMaybe helper
When handling REST Calls, you may often wanna check first if the requests are any good at all. Here, we have developed a helpful tool called RestMaybe
. Using RestMaybe
, we can chain calls in a way to internally build some Request Object and pass it into a function. In the function you want to pass, you should return a valid Flask response like you would in a normal Flask endpoint function.
app = Flask('Math Solver')
@app.route('binary-op', methods=['POST'])
def binary_op():
return RestMaybe(request) \
.wellformed() \
.exists('operand1') \
.exists('operand2') \
.optional('operator') \
.run(lambda request, object: _binary_op(object))
def _binary_op(request):
operand1 = request['operand1']
operand2 = request['operand2']
operator = request.get('operator', 'plus')
# ...
return ({'result': result, 'message': 'That was quite difficult.'}, 200)
RestMaybe
RestMaybe is a class that should make checking if the request is valid easier.
Currently, the capabilities of RestMaybe are limited to checking if a given request is a Json and if a given request has valid keys.
__init__(request)
Passes initial Request to the RestMaybe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The sent HTTP Request to handle. |
required |
exists(key)
Checks if a given key exists in the request Json. Modifies the error variable, if not.
Warning
This function will not check if the request was a Json in the first place. Use wellformed
first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to check for if it is in the request. |
required |
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
Itself to chain the next call. |
optional(key)
Collects an optional key. Will not throw an error if the key isn't passed.
Warning
This function will not check if the request was a Json in the first place. Use wellformed
first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to check for if it is in the request. |
required |
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
Itself to chain the next call. |
run(function)
If every check made was true, this function will call another function that the user can pass. If not, the error will be returned directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable[[Request, Any], Any]
|
The function |
required |
Returns:
Name | Type | Description |
---|---|---|
response |
A valid Flask Response. |
wellformed()
Checks if the HTTP Request is a Json. Modifies the error variable, if not.
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
Itself to chain the next call. |