So I have a Python library that contains a pile of super useful functions. I would like to be able to call the functions of this lib over a RESTful interface to make the functions available to any language, application or process that supports sockets and wishes to use them.
I don't want to have to code each function individually as a RESTful passthrough as there are hundreds available and they are all subject to change. Is there a standards compliant way to expose these functions to be accessed over REST?
Many thanks for any ideas, project links or advice anyone can give :)
FR
What I proposed in the comment under your question was pretty much that:
main.py
from flask import Flask
from flask import request
import functions
app = Flask(__name__)
@app.route('/call/<function_name>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def call_function(function_name: str):
function_to_call = getattr(functions, function_name)
body = request.json
return function_to_call(body)
app.run(host="0.0.0.0")
functions.py
def hello_world():
return "Hello world!"
def hello_name(params: dict):
name = params["name"]
return "Hello " + name
Requests examples:
http://localhost:5000/call/hello_world
http://localhost:5000/call/hello_name
{
"name": "Tom"
}
I am doing this exact same thing right now for a number of random devops tools we have. It's not going to be the golden standard of RESTful endpoint work, but sometimes you just need something quick and dirty. Here's what I did:
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With