python-2.7google-app-engine-pythongoogle-cloud-endpoints-v2

Define Cloud Endpoints Response Message from a 3rd party API JSON response


I am trying to create a custom API with Google Cloud Endpoints v2 hosted on App Engine in Python. The custom API is an API that interacts with an external API. For example, the custom API will have a GET method that when called makes a GET request to a 3rd party API.

The use case is to make the 3rd party API easier to use within a company and adding additional checks to verify access to the returned data.

Is there an easy way to return the already formatted API response from the 3rd party API from my custom API? When I say easy, I mean not having to convert the JSON response into an endpoint message. The 3rd party API will return something like:

{
    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"],
        },
    ]

}

I am trying not to convert the JSON into an endpoints message.

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
)

FYI, this is a simplified version of the JSON. My actual conversion code is much longer and more complex.

Am I overlooking something in the endpoints library or in Python that will do this conversion for me?

My biggest fear is that the time to convert the response from the 3rd party API response will cause the custom API response time to be greater than the typical 30 second timeout when waiting for an API response.


Solution

  • Unfortunately, the Endpoints framework works exclusively with message instances. Due to historical reasons, this is not possible to change without major rearchitecting of the framework.