Reputation: 841
I learning how to implement REST API's in python using falcon lib. For example I have the following dict:
db_data = {
"servers" : {
"1": {
# 'server_id':1,
"plugins":{
"1": {
"metrics_by_time":{
"2014":{"1":"Hello World 0"}
"2015":{"1":"Hello World 1"}
"2016":{"1":"Hello World 2"}
}
}
}
}
}
}
I want to create an API that will return to return metrics by a period of time (for example between 2014 and 2016). How could I do it? For example I have now the following code that returns metrics by server_id and plugin_id:
class By_Plugin_ID(object):
def on_get(self, req, resp, server_id, plugin_id):
resp.status = falcon.HTTP_200
resp.body = (json.dumps(db_data["servers"][str(server_id)]["plugins"][str(plugin_id)], ensure_ascii=False, default=lambda x:str(x)))
import falcon
app = application = falcon.API()
plugins_metrics = By_Plugin_ID()
app.add_route('/metrics/{server_id}/{plugin_id}', plugins_metrics)
I thought about something like this, but how to handle the queries after "?" :
metrics/{server_id}/{plugin_id}?from_time={timestamp}&to_time={timestamp}
Upvotes: 1
Views: 919
Reputation: 853
I am not familiar with Falcon, I usually work with Flask, however looking at their documentation you should get them with req.get_param()
from_time = req.get_param('from_time')
to_time = req.get_param('to_time')
you should put this in your on_get()
function
def on_get(self, req, resp, server_id, plugin_id):
from_time = req.get_param('from_time')
to_time = req.get_param('to_time')
# do whatever you want and return your response
Upvotes: 1
Reputation:
After the ?
come the query parameters. Falcon let's you access them using Request.params
.
Upvotes: 1