简体   繁体   English

云端点和App Engine

[英]Cloud Endpoints and App Engine

I've just started on Google Cloud and I'm creating an iOS app to interact with Google Cloud services via a mobile backend. 我刚刚开始使用Google Cloud,现在正在创建一个iOS应用,以便通过移动后端与Google Cloud服务进行交互。 I'm using Python to write the backend for App Engine. 我正在使用Python编写App Engine的后端。 I've gone through the tutorials in creating an API based on endpoints - but I have a question. 我已经完成了基于端点创建API的教程-但我有一个问题。

Do I have to create a Cloud Endpoints API, and then an app on App Engine? 我必须先创建Cloud Endpoints API,然后在App Engine上创建一个应用程序吗? Basically, I want to be able to register accounts on my iOS app, call an API which then makes use of Google Datastore to store the account details. 基本上,我希望能够在我的iOS应用程序上注册帐户,然后调用一个API,然后该API使用Google数据存储来存储帐户详细信息。 From looking at the tutorials (both the cloud endpoints one and then the guestbook one), am I meant to expose Google Datastore, cloud storage etc. within the endpoints api? 通过查看教程(两个云端点,然后一个访客留言簿),我是否打算在端点api中公开Goog​​le数据存储,云存储等? Or does that link into another app where that is all done? 还是可以链接到其他已完成的应用程序?

Sorry if this sounds a bit silly, but I just want to make sure! 抱歉,这听起来有点愚蠢,但我只想确定一下!

Thanks in advance. 提前致谢。

In a nutshell, your Cloud Endpoints API is your application. 简而言之,您的Cloud Endpoints API 您的应用程序。 Some of the documentation regarding Cloud Endpoints can be a bit confusing (or vague), but on the server side it's essentially a bunch of Python decorators or Java annotations that allow you to expose your application logic as a REST API. 有关Cloud Endpoints的一些文档可能会有些混乱(或含糊),但是在服务器端,实际上是一堆Python装饰器或Java批注,使您可以将应用程序逻辑公开为REST API。

I find the Java implementation of Cloud Endpoints more intuitive than the Python one, which requires a bit more work to (de-)serialise your objects. 我发现Cloud Endpoints的Java实现比Python更加直观,这需要更多的工作来对对象进行反序列化。 You could look at endpoints_proto_datastore.ndb.EndpointsModel which might take some of the boilerplate stuff out of the equation (defining messages). 您可以看一下endpoints_proto_datastore.ndb.EndpointsModel ,它可能会使方程式中的一些样板内容(定义消息)消失。

Essentially, when you write your API, each endpoint maps to a python function. 本质上,在编写API时,每个端点都映射到python函数。 Inside that function you can do what you like, but typically it will be either: 在该函数内,您可以执行自己喜欢的操作,但通常可以是:

  1. Deserialise your POSTed JSON, validate it, and write some entities to Datastore (or Cloud SQL, BigTable, wherever). 反序列化您发布的JSON,进行验证,然后将一些实体写入数据存储区(或Cloud SQL,BigTable,无论在何处)。

  2. Read one or more entities from Datastore and serialize them to JSON and return them to the client. 从数据存储区读取一个或多个实体,并将其序列化为JSON,然后将其返回给客户端。

For example, you might define your API (the whole collection of endpoint functions) as 例如,您可以将API(端点函数的整个集合)定义为

@endpoints.api(name='cafeApi', version='v1', description='Cafe API', audiences=[endpoints.API_EXPLORER_CLIENT_ID])
class CafeApi(remote.Service):
    # endpoints here

For example, you might have an endpoint to get nearby cafes: 例如,您可能有一个到达附近咖啡馆的终点:

@endpoints.method(GEO_RESOURCE, CafeListResponse, path='cafes/nearby', http_method='GET', name='cafes.nearby')
def get_nearby_cafes(self, request):
    """Get cafes close to specified lat,long"""
    cafes = list()
    for c in search.get_nearby_cafes(request.lat, request.lon):
        cafes.append(c.response_message())

    return CafeListResponse(cafes=cafes)

A couple of things to highlight here. 这里有两点要强调。 With the Python Endpoints implementation, you need to define your resource and message classes - these are used to encapsulate request data and response bodies. 使用Python Endpoints实现,您需要定义资源和消息类-它们用于封装请求数据和响应主体。

So, in the above example, GEO_RESOURCE encapsulates the fields required to make a GeoPoint (so we can search by location using Search API, but you might just search Datastore for Cafes with a 5-star rating): 因此,在上面的示例中, GEO_RESOURCE封装了创建GeoPoint所需的字段(因此我们可以使用Search API按位置进行搜索,但您可能只是在数据存储区中搜索5星评分的Cafes):

    GEO_RESOURCE = endpoints.ResourceContainer(
        message_types.VoidMessage,
        lat=messages.FloatField(1, required=True),
        lon=messages.FloatField(2, required=True)
    )

and the CafeListResponse would just encapsulate a list of CafeResponse objects (with Cloud Endpoints you return a single object): 并且CafeListResponse只会封装一个CafeResponse对象list (使用Cloud Endpoints,您将返回一个对象):

class CafeListResponse(messages.Message):
    locations = messages.MessageField(CafeResponse, 1, required=False, repeated=True)

where the CafeResponse is the message that defines how you want your objects (typically Datastore entities) serialised by your API. 其中, CafeResponse是一条消息,它定义了您希望如何通过API序列化对象(通常是数据存储区实体)。 eg, 例如,

class LocationResponse(messages.Message):
    id = messages.StringField(1, required=False)
    coordinates = messages.MessageField(GeoMessage, 3, required=True)
    name = messages.StringField(4, required=False)

With that endpoint signature, you can access it via an HTTP GET at /cafeApi/v1/cafes/nearby?lat=...&lon=... or via, say, the Javascript API client with `cafeApi.cafes.nearby(...). 使用该端点签名,您可以通过/cafeApi/v1/cafes/nearby?lat=...&lon=...上的HTTP GET或使用带有cafeApi.cafes.nearby( ...)。

Personally, I found Flask a bit more flexible with working with Python to create a REST API. 就个人而言,我发现Flask在使用Python创建REST API时更加灵活。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM