如何在 Python REST API 中触发函数

发布于 2025-01-09 10:59:20 字数 335 浏览 1 评论 0原文

我有一个本地 Flask REST API,它运行一些查询并返回 JSON 响应。我的想法是向 API 发送 POST 请求并触发能够更新数据库的函数。

这个想法背后的灵感来自于我们在公共云中创建资源的方式。示例:假设我们在任何公共云上创建虚拟云网络。云架构为每个模块提供了多个 API,VCN 的创建会通过对每种资源类型的 GET 或 POST 请求来触发 NAT 网关、路由表、安全列表、子网等的创建。

受到这种架构的启发,我想以一种简化和本地的方式来做到这一点。我的第一个猜测是,一旦请求到达端点,就使用多处理库来生成进程,但我不知道这是否是最佳方法或良好实践。任何人都可以告诉我如何做到这一点或者我是否走在正确的道路上。

I have a local Flask REST API that runs some queries and return JSON responses. My idea is to send a POST request to the API and trigger a function capable of updating my database.

The inspiration behind this idea is the way we create resources in a public Cloud. Example: let's say we create a Virtual Cloud Network on any public cloud. The cloud architecture has several APIs for each module and the creation of a VCN trigger the creation of NAT Gateways, Route Tables, Security Lists, Subnets and so on through a GET or POST request to each resource type.

So inspired by this architecture, I want to do this in a simplified and local manner. My first guess is to use the multiprocessing library to spawn processes as soon as a request hits the endpoint, but I don't know if this the best way or a good practice. Can anyone give me an idea on how to do this or if I am in the right path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2025-01-16 10:59:20

我不太明白...但我可以尽力帮助你,
以下代码是相同 API 的代码,但您可以获得不同的响应,我不知道您要做什么,所以我添加了自己的示例。

from flask import *
app = Flask(__name__)



@app.route('/api/route/1', methods=['POST'])
def api_1():
    if request.method == 'POST':
        # Some code here 
        #Eg:
        # data = request.get_json()
        # print(data)
        #return data.thing

        #OR

        return "Hello from route 1"

@app.route('/api/route/2', methods=['POST'])
def api_2():
    if request.method == 'POST':
        # Some code here 
        #Eg:
        # data = request.get_json()
        # print(data)
        #return data.thing

        #OR

        return "Hello from route 2"
@app.route('/api/route/3', methods=['POST'])
def api_3():
    if request.method == 'POST':
        # Some code here 
        #Eg:
        # data = request.get_json()
        # print(data)
        #return data.thing

        #OR

        return "Hello from route 3"



if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8000, debug=True)

i dont really understand... but i can try to help you,
the following code is a code that is the same API but you can get different responses, i dont know what are you going to do with so i added my own examples.

from flask import *
app = Flask(__name__)



@app.route('/api/route/1', methods=['POST'])
def api_1():
    if request.method == 'POST':
        # Some code here 
        #Eg:
        # data = request.get_json()
        # print(data)
        #return data.thing

        #OR

        return "Hello from route 1"

@app.route('/api/route/2', methods=['POST'])
def api_2():
    if request.method == 'POST':
        # Some code here 
        #Eg:
        # data = request.get_json()
        # print(data)
        #return data.thing

        #OR

        return "Hello from route 2"
@app.route('/api/route/3', methods=['POST'])
def api_3():
    if request.method == 'POST':
        # Some code here 
        #Eg:
        # data = request.get_json()
        # print(data)
        #return data.thing

        #OR

        return "Hello from route 3"



if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8000, debug=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文