Google API Python Client: Google Fusion Tables

Наконец-то я получил доступ к google fusion tables API с помощью google-api-python-client. У этой библиотеки действительно странноватый интерфейс, но всё стало гораздо понятней после недавнего полноценного переезда Fusion API на новую версию.

Рабочий код:

import httplib2
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

# Here's the file you get from API Console -> Service Account.
f = file('key.p12', 'rb')
key = f.read()
f.close()

# Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
credentials = SignedJwtAssertionCredentials(
    '...@developer.gserviceaccount.com',
    key,
    scope='https://www.googleapis.com/auth/fusiontables')
http = httplib2.Http()
http = credentials.authorize(http)

service = build("fusiontables", "v1", http=http)
# For example, let make SQL query to SELECT ALL from Table with
# id = 1gvB3SedL89vG5r1128nUN5ICyyw7Wio5g1w1mbk
print(service.query().sql(sql='SELECT * FROM 1gvB3SedL89vG5r1128nUN5ICyyw7Wio5g1w1mbk').execute())

Можно использовать функцию help, чтобы получить документацию по методам:

service = build("fusiontables", "v1")
help(service)
help(service.query())

Для последней команды вы получите что-то типа такого:


class Resource(__builtin__.object)
 |  A class for interacting with a resource.
 |
 |  Methods defined here:
 |
 |  __init__(self)
 |
 |  sql = method(self, **kwargs)
 |      Executes an SQL SELECT/INSERT/UPDATE/DELETE/SHOW/DESCRIBE statement.
 |
 |      Args:
 |        pp: string, A parameter
 |        hdrs: boolean, Should column names be included (in the first row)?. Default is true.
 |        typed: boolean, Should typed values be returned in the (JSON) response -- numbers for numeric values and parsed geometries for KML values? Default is true.
 |        strict: string, A parameter
 |        userip: string, A parameter
 |        sql: string, An SQL SELECT/SHOW/DESCRIBE/INSERT/UPDATE/DELETE statement. (required)
 |        trace: string, A parameter
 |
 |      Returns:
 |        An object of the form:
 |
 |          { # Represents a response to an sql statement.
 |          "kind": "fusiontables#sqlresponse", # Type name: a template for an individual table.
 |          "rows": [ # The rows in the table. For each cell we print out whatever cell value (e.g., numeric, string) exists. Thus it is important that each cell contains only one value.
 |            [
 |              [
 |                "",
 |              ],
 |            ],
 |          ],
 |          "columns": [ # Columns in the table.
 |            "A String",
 |          ],
 |        }
 |
 |  sqlGet = method(self, **kwargs)
 |      Executes an SQL SELECT/SHOW/DESCRIBE statement.
 |
 |      Args:
 |        pp: string, A parameter
 |        hdrs: boolean, Should column names be included (in the first row)?. Default is true.
 |        typed: boolean, Should typed values be returned in the (JSON) response -- numbers for numeric values and parsed geometries for KML values? Default is true.
 |        strict: string, A parameter
 |        userip: string, A parameter
 |        sql: string, An SQL SELECT/SHOW/DESCRIBE statement. (required)
 |        trace: string, A parameter
 |
 |      Returns:
 |        An object of the form:
 |
 |          { # Represents a response to an sql statement.
 |          "kind": "fusiontables#sqlresponse", # Type name: a template for an individual table.
 |          "rows": [ # The rows in the table. For each cell we print out whatever cell value (e.g., numeric, string) exists. Thus it is important that each cell contains only one value.
 |            [
 |              [
 |                "",
 |              ],
 |            ],
 |          ],
 |          "columns": [ # Columns in the table.
 |            "A String",
 |          ],
 |        }
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

Up.: нашел список методов доступа к Fusion Tables. В нём вся та же информация, что даёт функция help(), но в виде веб-страниц. Кстати, там есть инфа и по другим API.