I would like to create an "Echo" API in Python. That would be an API that will return all the details of the request that reached to the server, but in an organized data structure like JSON. For example, if it's invoked like this: GET https://subdom.dom.com:9997/api/rest/resource/item?name=John
the API will return a JSON as follows
{
"method" : "GET",
"schema" : "https",
"hostname" : "subdom.dom.com",
"port" : "9997",
"basepath" : "/api/rest",
"resource" : "/resource/item",
"params" : {
"name" : "John",
},
"headers" : {
"x-client-ip" : "123.234.234.123",
},
...
}
- Is there any available open-source framework able to do that?
- If not, what would be the best way to create one from scratch?
Flask, FastAPI and other similar frameworks are not a good answer since I don't want to create the route entry for any possible resource that will be invoked, so it will have to digest anything that comes after "/". (If possible, same thing should do with methods: accept all).
Therefore it should be the most non-explicit API alive, I know that this is not a good programming practice but it will be used as a test framework for my project, ensuring the my api management service works flawless before and after any update).
CodePudding user response:
You can do one thing with flask itself (I know you denied Flask & FastAPI), but I think you should give it a try.
There's a decorator after_request
in flask, which will be executed after every request.
Flask will first get the request, find the endpoint, execute the endpoint function and then will execute the function decorated with after_request
decorator. This decorated function is in-general used to post-process the response codes (For example, you want to add a header to every response, then you add it here and don't write that in every endpoint).
Here, you can add all this data like path, pathParas, queryParams, headers, requestMethod etc to the response of every request.
I hope this will work.!