Build the client object
The first thing you need to do is to build an object which will act as an abstraction of the Powertools API. API requests will then merely be a matter of method calls on the client object.
Building a client object in Python
from apiclient.discovery import build
DISCOVERY_URL = 'https://pt4dapi-dot-powertools-v2-prod.appspot.com/_ah/api/discovery/v1/apis/powertools/v2/rest'
client = build(
In the code snippet above, we start by importing the method
build from the apiclient library. We then define the
DISCOVERY_URL which is a constant that you can copy and paste. This URL allows the client to discover what endpoints are available on the API, and what kind of arguments / body they accept. We finally call the method
build and give it all the necessary parameters so that it can build the client for the v2 of the Powertools API.
Call methods on the client object
By calling methods on the client object, you obtain resources. On resources, you can then execute operations. For example, the API contains the
workspaces resource. On this resource, you can execute the
list (among others) operation:
Getting the list of workspaces
result = client.workspaces().list(
if 'workspaces' in result:
print 'Found workspaces!'
for ws in result['workspaces']:
print 'Workspace name:', ws['name']
In the example above, we first retrieve the
workspaces resource on the client before executing the
list operation on it. The method accepts the various parameters recognized by the endpoint. Most importantly, the three variables that you must include in each and every call is your API key, your G Suite domain name and a actionUser to impersonate a user.
The variable returned by the method is a Python dictionary, obtained thanks to the JSON data returned by the API. This dictionary may or may not have a
workspaces field which maps to a list of workspaces. It's a peculiarity of Google endpoints system that if a list is empty, then the corresponding field is altogether absent from the result.
It's also possible that you must get a resource from another resource. For example, if you want to add a user to a workspace, you need to call the
insert method on the
users resource of the
Add a user as reader of a workspace
In the example above, we inserted user
firstname.lastname@example.org as a reader of the workspace whose ID is a constant
WORKSPACE_ID for the sake of this example.
Parameters given as arguments of the operation method corresponds to the path parameters or the query string parameters of the HTTP method. Additionally, some endpoints accepts a body. To pass a body to an endpoint, pass to the
body argument of the operation method a dictionary containing the body's content:
Create a new workspace
In this last example, we added a new workspace. The underlying HTTP method is a POST method, so the endpoint accepts a request's body whose content describe the workspace to create. To do so, we pass a dictionary to the