【问题标题】:Define Cloud Endpoints Response Message from a 3rd party API JSON response定义来自第 3 方 API JSON 响应的 Cloud Endpoints 响应消息
【发布时间】:2019-05-20 01:28:02
【问题描述】:

我正在尝试使用 Python 中的 App Engine 上托管的 Google Cloud Endpoints v2 创建自定义 API。自定义 API 是与外部 API 交互的 API。例如,自定义 API 将有一个 GET 方法,该方法在调用时会向 3rd 方 API 发出 GET 请求。

用例是让第 3 方 API 更易于在公司内使用,并添加额外检查以验证对返回数据的访问。

是否有一种简单的方法可以从我的自定义 API 返回来自 3rd 方 API 的已格式化 API 响应? 当我说简单时,我的意思是不必将 JSON 响应转换为端点消息。第 3 方 API 将返回如下内容:

{
    keyOne: "key one value",
    keyTwo: "key two value",
    keyThree: ["key three value array", "another string", "and another string"],
    keyFour: [
        {
            keyOne: "key one value",
            keyTwo: "key two value",
            keyThree: ["key three value array", "another string", "and another string"],
        },
        {
            keyOne: "key one value",
            keyTwo: "key two value",
            keyThree: ["key three value array", "another string", "and another string"],
        },
    ]

}

我正在尝试不将 JSON 转换为端点消息。

class GetResponse(messages.Message):
    keyOne = messages.StringField(1)
    keyTwo = messages.StringField(2, required=True)
    keyThree = messages.MessageField(SomeStringList, 3)
    keyFour = messages.MessageField(SomeJsonList, 4)

class SomeStringList(messages.Message):
    keyFive = messages.StringField(1, repeated=True)

class SomeJsonList(messages.Message):
    keySix = messages.MessageField(GetResponse, 1, repeated=True)

...

#Convert JSON
converted_json_list = []
for obj in resObj["keyFour"]:
    converted_json_list.append(GetResponse(
        keyOne=obj["keyOne"],
        keyTwo=obj["keyTwo"],
        keyThree=obj["keyThree"]
    ))

return GetResponse(
    keyOne=resObj["keyOne"],
    keyTwo=resObj["keyTwo"],
    keyThree=resObj["keyThree"]
    keyFour=converted_json_list
)

仅供参考,这是 JSON 的简化版本。我的实际转换代码更长更复杂。

我是否忽略了端点库或 Python 中将为我执行此转换的某些内容?

我最大的担心是从 3rd 方 API 响应转换响应的时间会导致自定义 API 响应时间大于等待 API 响应时典型的 30 秒超时。

【问题讨论】:

    标签: python-2.7 google-app-engine-python google-cloud-endpoints-v2


    【解决方案1】:

    不幸的是,Endpoints 框架仅适用于消息实例。由于历史原因,如果不对框架进行重大重新架构,这是不可能改变的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2016-07-10
      • 2014-07-29
      相关资源
      最近更新 更多