4

我正在使用 Google AppEngine Endpoints 构建 Web API。我将使用用 Python 编写的客户端来使用它。我知道提供了脚本来生成 Android 和 iOS 客户端 API,但 Python 似乎没有任何可比性。

重新编写所有代码似乎是多余的。例如,消息定义基本相同。

无论如何,它更容易完成这项工作吗?

谢谢

4

2 回答 2

12

您可以使用与端点兼容的适用于 Python 的 Google API 客户端库。

通常,您会构建一个客户端service = build(api, version, http=http),例如service = build("plus", "v1", http=http)构建一个访问 Google+ API 的客户端。

要为您的端点使用该库,您将使用:

service = build("your_api", "your_api_version", http=http, 
  discoveryServiceUrl=("https://yourapp.appspot.com/_ah/api/discovery/v1/"
                       "apis/{api}/{apiVersion}/rest"))

然后,您可以使用

result = service.resource().method([parameters]).execute()
于 2013-05-01T14:03:37.750 回答
3

以下是端点 helloworld 问候示例发生的情况:

__author__ = 'robertking'

import httplib2
from apiclient.discovery import build

http = httplib2.Http()

service = build("helloworld", "v1", http=http,
  discoveryServiceUrl=("http://localhost:8080/_ah/api/discovery/v1/apis/helloworld/v1/rest"))

print service.greetings().listGreeting().execute()['items']

"""
prints
[{u'message': u'hello world!'}, {u'message': u'goodbye world!'}]
"""

现在我正在使用http。

于 2014-02-04T21:07:46.800 回答