Sample Test Script

This Python script demonstrates how to test against the Android Partner API compatibility service and list most recent builds.

As the developer, you must specify the following:

  • A consumer project client ID.
  • A consumer project API key.
  • A file system location for storing the token.
 Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import httplib
import sys

from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from oauth2client import tools

BASE_URL = 'androidpartner.googleapis.com'
DEPLOYMENTS_PATH = '/v1/compatibility/builds'
STORAGE_FILE = 'androidPartner.dat'
SCOPES = 'https://www.googleapis.com/auth/androidPartner'
APPLICATION_NAME = 'androidpartner-cmdline-sample/1.7'


def GetBuildsList(header):
  conn = httplib.HTTPSConnection(BASE_URL)
  path = DEPLOYMENTS_PATH
  headers = header
  conn.request('GET', path, '', headers)
  DumpResponseAndCloseConection(conn)

def GetResponseAndCloseConection(conn):
  resp = conn.getresponse().read()
  conn.close()

def DumpResponseAndCloseConection(conn):
  resp = conn.getresponse().read()
  print 'the response is:\n' + resp
  conn.close()

  return resp

def CheckCredentials(client_id, client_secret):
  """Checks the OAuth credentials before making a request."""
  storage = Storage(STORAGE_FILE)
  credentials = storage.get()
  if (not credentials or credentials.invalid or
      credentials.token_expiry < datetime.datetime.utcnow()):
    flow = OAuth2WebServerFlow(
        client_id=client_id,
        client_secret=client_secret,
        scope=SCOPES,
        user_agent=APPLICATION_NAME)
    credentials = tools.run_flow(flow, storage)
  return {'Authorization': 'Bearer ' + credentials.access_token}

def main(argv):

  """Developer must pass the project client ID and API key."""
  header = CheckCredentials('project-client-id', 'project-api-key')

  GetBuildsList(header)

if __name__ == '__main__':
  main(sys.argv)