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.""" |
| 6 | |
| 7 | import apiclient |
| 8 | import httplib2 |
| 9 | import os |
| 10 | |
| 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): |
| 19 | """Raised when there is a general error.""" |
| 20 | |
| 21 | |
| 22 | class NoServiceRestClientError(RestClientError): |
| 23 | """Raised when there is no ready service for a google API.""" |
| 24 | |
| 25 | |
| 26 | class BaseRestClient(object): |
| 27 | """Base class of REST client for google APIs.""" |
| 28 | |
| 29 | def __init__(self, scopes, service_name, service_version): |
| 30 | """Initialize a REST client to connect to a google API. |
| 31 | |
| 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 |
| 41 | |
| 42 | @property |
| 43 | def service(self): |
| 44 | if not self._service: |
| 45 | raise NoServiceRestClientError('No service created for calling API') |
| 46 | |
| 47 | return self._service |
| 48 | |
| 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) |
| 65 | |
| 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( |
| 73 | file_getter.LOCAL_CLIENT_SECRETS_FILE, self._scopes) |
| 74 | else: |
| 75 | # Running in app-engine production |
| 76 | self._credentials = appengine.AppAssertionCredentials(self._scopes) |
| 77 | |
| 78 | |
| 79 | class AndroidBuildRestClient(object): |
| 80 | """REST client for android build API.""" |
| 81 | |
| 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() |
| 86 | |
| 87 | def get_latest_build_id(self, branch, target): |
| 88 | """Get the latest build id for a given branch and target. |
| 89 | |
| 90 | Args: |
| 91 | branch: an android build's branch |
| 92 | target: an android build's target |
| 93 | |
| 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 |
| 106 | |
| 107 | return builds['builds'][0]['buildId'] |
| 108 | |
| 109 | |
| 110 | class StorageRestClient(object): |
| 111 | """REST client for google storage API.""" |
| 112 | |
| 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() |
| 117 | |
| 118 | def ReadObject(self, input_bucket, input_object): |
| 119 | """Read the contents of input_object in input_bucket. |
| 120 | |
| 121 | Args: |
| 122 | input_bucket: the bucket for fetching. |
| 123 | input_object: the object for checking the contents. |
| 124 | |
| 125 | Returns: |
| 126 | the stripped string contents of the input object. |
| 127 | |
| 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() |
| 135 | |
| 136 | |
| 137 | class CalendarRestClient(object): |
| 138 | """Class of REST client for google calendar API.""" |
| 139 | |
| 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() |
| 144 | |
| 145 | def AddEvent(self, calendar_id, input_event): |
| 146 | """Add events of a given calendar. |
| 147 | |
| 148 | Args: |
| 149 | calendarId: the ID of the given calendar. |
| 150 | input_event: the event to be added. |
| 151 | kwargs: the parameters for adding events of the calendar. |
| 152 | """ |
| 153 | self._rest_client.service.events().insert( |
| 154 | calendarId=calendar_id, |
| 155 | body=input_event).execute() |
| 156 | |
| 157 | |
| 158 | class SwarmingRestClient(object): |
| 159 | """REST client for swarming proxy API.""" |
| 160 | |
| 161 | DISCOVERY_URL_PATTERN = '%s/discovery/v1/apis/%s/%s/rest' |
| 162 | |
| 163 | def __init__(self, rest_client, service_url): |
| 164 | self._rest_client = rest_client |
| 165 | discovery_url = self.DISCOVERY_URL_PATTERN % ( |
| 166 | service_url, service_name, service_version) |
| 167 | self._rest_client.create_service(discovery_url=discovery_url) |
| 168 | |
| 169 | def create_task(self, request): |
| 170 | """Create new task. |
| 171 | |
| 172 | Args: |
| 173 | request: a json-compatible dict expected by swarming server. |
| 174 | See _to_raw_request's output in swarming_lib.py for details. |
| 175 | """ |
| 176 | return self._rest_client.service.tasks().new( |
| 177 | fields='request,task_id', body=request).execute() |
| 178 | |
| 179 | def request_task(self, task_id): |
| 180 | """Get task details by a given task_id. |
| 181 | |
| 182 | Args: |
| 183 | task_id: A string, represents task id. |
| 184 | |
| 185 | Returns: |
| 186 | A json dict returned by API task.request. |
| 187 | """ |
| 188 | return self._rest_client.service.task().request( |
| 189 | task_id=task_id).execute() |