简体   繁体   English

如何在 python 中使用 Falcon 将客户端“GET”请求从一个 API 路由到另一个具有相同端点的 API?

[英]How to route the client 'GET' request from one API to another another API with same end points using Falcon in python?

I have two APIs with same end points, but different URLs.我有两个端点相同但 URL 不同的 API。 Like this:像这样:

API_1 = /takeout/{t_note}
        /serve/{t_note}/{m_note}

API_2 = /takeout/{t_note}
        /serve/{t_note}/{m_note}

A client send a 'GET' request to API_1 but instead of giving a response back, it just route to API_2 and show the response of API_2, where all the logic happens.客户端向 API_1 发送“GET”请求,但没有返回响应,它只是路由到 API_2 并显示 API_2 的响应,所有逻辑都发生在这里。 Client thinks that it communicate with API_2 but in reality send the request to API_1.客户端认为它与 API_2 通信,但实际上将请求发送到 API_1。 So, at the end API_1 is just some proxy, which only route the request to API_2 for response.所以,最后 API_1 只是一些代理,它只将请求路由到 API_2 进行响应。

Any idea how to achieve this?知道如何实现这一目标吗? What i am looking for is this:我正在寻找的是:

class TakeOut(object):
    def on_get (self, resp, req, t_note:str):
        resp.body = json.dumps() # response of API_2 here for endpoint /takeout/{t_note}
                                 # t_note is just a id in string format which sent by the client

Something like this maybe.可能是这样的。 I am new to falcon and APIs.我是 falcon 和 API 的新手。

app2.py:应用程序2.py:

import falcon


class Api2(object):
    def on_get(self, _req, resp, arg1: str):
        resp.status = falcon.HTTP_200 if arg1 == '200' else falcon.HTTP_400
        resp.media = {'arg': arg1}


app = falcon.API()
api2 = Api2()
app.add_route('/api2/{arg1}', api2)

app1.py:应用程序1.py:

import falcon
import requests


class Api1(object):
    def on_get(self, _req, resp, arg1: str):
        api2_resp = requests.get(f'http://127.0.0.1:8000/api2/{arg1}')
        resp.status = falcon.HTTP_200 if api2_resp.status_code == 200 else falcon.HTTP_400
        dic = api2_resp.json()
        dic["api1"] = 'payload'
        resp.media = dic


app = falcon.API()
api1 = Api1()
app.add_route('/api1/{arg1}', api1)

gunicorn app2:app

gunicorn -b '127.0.0.1:8001' app1:app

curl -i http://127.0.0.1:8001/api1/200 => HTTP/1.1 200 OK... {"arg": "200", "api1": "payload"} curl -i http://127.0.0.1:8001/api1/200 => HTTP/1.1 200 OK... {"arg": "200", "api1": "payload"}

curl -i http://127.0.0.1:8001/api1/201 => HTTP/1.1 400 Bad Request... {"arg": "201", "api1": "payload"} curl -i http://127.0.0.1:8001/api1/201 => HTTP/1.1 400 Bad Request... {"arg": "201", "api1": "payload"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 Falcon API 和 Python 设置 cookie - Unable to set cookies using Falcon API and Python 在 Python Flask 应用程序中使用 app.route() 和 api.add_resource() 配置端点有什么区别? - What is the difference in configuring the end points using app.route() and api.add_resource() in a Python Flask application? 如何从 python django 中的另一个 function 获取数据 API - How to get a data API from another function in python django 如何在同一个 class 的另一个 api 中调用 python api 方法? - How to call python api method inside another api in same class? 如何在Falcon Request对象中获取client_addr? - How to get client_addr in Falcon Request object ? 如何使用 minikube 在内部(使用 DNS)从 Kubernetes 命名空间中的 Flask API 向另一个命名空间中的另一个 Flask API 发出请求 - How to make a request from a Flask API in a Kubernetes Namespace to another Flask API in another namespace internally (Using the DNS) using minikube 使用python脚本中的JSON数据从API GET到POST到另一个API - Using JSON data from API GET to POST to another API via python script Python Flask-如何将值从一种途径传递到另一种途径? - Python Flask - How to pass values from one route to another? 使用 Python Speech Client 从 Google Speech 向文本 API 请求“获取操作” - Request a “get operation” from Google Speech to text API using Python Speech Client 来自另一个API python的API调用 - API call from another API python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM