xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for interacting with google APIs.""" |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 6 | # pylint: disable=g-bad-import-order |
| 7 | |
| 8 | import httplib2 |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 9 | |
| 10 | import apiclient |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 11 | import constants |
| 12 | import file_getter |
| 13 | |
| 14 | from oauth2client import service_account |
| 15 | from oauth2client.contrib import appengine |
| 16 | |
| 17 | |
| 18 | class RestClientError(Exception): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 19 | """Raised when there is a general error.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class NoServiceRestClientError(RestClientError): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 23 | """Raised when there is no ready service for a google API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class BaseRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 27 | """Base class of REST client for google APIs.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 28 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 29 | def __init__(self, scopes, service_name, service_version): |
| 30 | """Initialize a REST client to connect to a google API. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 31 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 32 | Args: |
| 33 | scopes: the scopes of the to-be-connected API. |
| 34 | service_name: the service name of the to-be-connected API. |
| 35 | service_version: the service version of the to-be-connected API. |
| 36 | """ |
| 37 | self.running_env = constants.environment() |
| 38 | self.scopes = scopes |
| 39 | self.service_name = service_name |
| 40 | self.service_version = service_version |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 41 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 42 | @property |
| 43 | def service(self): |
| 44 | if not self._service: |
| 45 | raise NoServiceRestClientError('No service created for calling API') |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 46 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 47 | return self._service |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 48 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 49 | def create_service(self, discovery_url=None): |
| 50 | """Create the service for a google API.""" |
| 51 | self._init_credentials() |
| 52 | # Explicitly specify timeout for http to avoid DeadlineExceededError. |
| 53 | # It's used for services like AndroidBuild API, which raise such error |
| 54 | # when being triggered too many calls in a short time frame. |
| 55 | # http://stackoverflow.com/questions/14698119/httpexception-deadline-exceeded-while-waiting-for-http-response-from-url-dead |
| 56 | http_auth = self._credentials.authorize(httplib2.Http(timeout=30)) |
| 57 | if discovery_url is None: |
| 58 | self._service = apiclient.discovery.build( |
| 59 | self.service_name, self.service_version, |
| 60 | http=http_auth) |
| 61 | else: |
| 62 | self._service = apiclient.discovery.build( |
| 63 | self.service_name, self.service_version, http=http_auth, |
| 64 | discoveryServiceUrl=discovery_url) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 65 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 66 | def _init_credentials(self): |
| 67 | """Initialize the credentials for a google API.""" |
| 68 | if (self.running_env == constants.RunningEnv.ENV_STANDALONE or |
| 69 | self.running_env == constants.RunningEnv.ENV_DEVELOPMENT_SERVER): |
| 70 | # Running locally |
| 71 | service_credentials = service_account.ServiceAccountCredentials |
| 72 | self._credentials = service_credentials.from_json_keyfile_name( |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 73 | file_getter.STAGING_CLIENT_SECRETS_FILE, self.scopes) |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 74 | else: |
| 75 | # Running in app-engine production |
| 76 | self._credentials = appengine.AppAssertionCredentials(self.scopes) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class AndroidBuildRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 80 | """REST client for android build API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 81 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 82 | def __init__(self, rest_client): |
| 83 | """Initialize a REST client for connecting to Android Build API.""" |
| 84 | self._rest_client = rest_client |
| 85 | self._rest_client.create_service() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 86 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 87 | def get_latest_build_id(self, branch, target): |
| 88 | """Get the latest build id for a given branch and target. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 89 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 90 | Args: |
| 91 | branch: an android build's branch |
| 92 | target: an android build's target |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 93 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 94 | Returns: |
| 95 | A string representing latest build id. |
| 96 | """ |
| 97 | request = self._rest_client.service.build().list( |
| 98 | buildType='submitted', |
| 99 | branch=branch, |
| 100 | target=target, |
| 101 | successful=True, |
| 102 | maxResults=1) |
| 103 | builds = request.execute(num_retries=10) |
| 104 | if not builds or not builds['builds']: |
| 105 | return None |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 106 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 107 | return builds['builds'][0]['buildId'] |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 108 | |
| 109 | |
| 110 | class StorageRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 111 | """REST client for google storage API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 112 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 113 | def __init__(self, rest_client): |
| 114 | """Initialize a REST client for connecting to Google storage API.""" |
| 115 | self._rest_client = rest_client |
| 116 | self._rest_client.create_service() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 117 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 118 | def read_object(self, input_bucket, input_object): |
| 119 | """Read the contents of input_object in input_bucket. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 120 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 121 | Args: |
| 122 | input_bucket: the bucket for fetching. |
| 123 | input_object: the object for checking the contents. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 124 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 125 | Returns: |
| 126 | the stripped string contents of the input object. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 127 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 128 | Raises: |
| 129 | apiclient.errors.HttpError |
| 130 | """ |
| 131 | req = self._rest_client.service.objects().get_media( |
| 132 | bucket=input_bucket, |
| 133 | object=input_object) |
| 134 | return req.execute() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 135 | |
| 136 | |
| 137 | class CalendarRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 138 | """Class of REST client for google calendar API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 139 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 140 | def __init__(self, rest_client): |
| 141 | """Initialize a REST client for connecting to Google calendar API.""" |
| 142 | self._rest_client = rest_client |
| 143 | self._rest_client.create_service() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 144 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 145 | def add_event(self, calendar_id, input_event): |
| 146 | """Add events of a given calendar. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 147 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 148 | Args: |
| 149 | calendar_id: the ID of the given calendar. |
| 150 | input_event: the event to be added. |
| 151 | """ |
| 152 | self._rest_client.service.events().insert( |
| 153 | calendarId=calendar_id, |
| 154 | body=input_event).execute() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | class SwarmingRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 158 | """REST client for swarming proxy API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 159 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 160 | DISCOVERY_URL_PATTERN = '%s/discovery/v1/apis/%s/%s/rest' |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 161 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 162 | def __init__(self, rest_client, service_url): |
| 163 | self._rest_client = rest_client |
| 164 | discovery_url = self.DISCOVERY_URL_PATTERN % ( |
| 165 | service_url, rest_client.service_name, rest_client.service_version) |
| 166 | self._rest_client.create_service(discovery_url=discovery_url) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 167 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 168 | def create_task(self, request): |
| 169 | """Create new task. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 170 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 171 | Args: |
| 172 | request: a json-compatible dict expected by swarming server. |
| 173 | See _to_raw_request's output in swarming_lib.py for details. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 174 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 175 | Returns: |
| 176 | A json dict returned by API task.new. |
| 177 | """ |
| 178 | return self._rest_client.service.tasks().new( |
| 179 | fields='request,task_id', body=request).execute() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 180 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 181 | def get_task_result(self, task_id): |
| 182 | """Get task results by a given task_id. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 183 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 184 | Args: |
| 185 | task_id: A string, represents task id. |
| 186 | |
| 187 | Returns: |
| 188 | A json dict returned by API task.result. |
| 189 | """ |
| 190 | return self._rest_client.service.task().result( |
| 191 | task_id=task_id).execute() |