| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 The Chromium 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 """Provides functions to work with AppEngine endpoints""" |
| 6 |
| 7 import httplib2 |
| 8 import logging |
| 9 import time |
| 10 |
| 11 import apiclient.discovery |
| 12 import apiclient.errors |
| 13 import oauth2client.appengine |
| 14 |
| 15 |
| 16 AUTH_SCOPE = 'https://www.googleapis.com/auth/userinfo.email' |
| 17 |
| 18 |
| 19 # TODO(akuegel): Do we want to use a different timeout? Do we want to use a |
| 20 # cache? See documentation here: |
| 21 # https://github.com/jcgregorio/httplib2/blob/master/python2/httplib2/__init__.p
y#L1142 |
| 22 def _authenticated_http(http, scope): |
| 23 credentials = oauth2client.appengine.AppAssertionCredentials(scope=scope) |
| 24 return credentials.authorize(http or httplib2.Http()) |
| 25 |
| 26 |
| 27 def build_client(api_name, api_version, discovery_url, http=None, |
| 28 num_tries=5): |
| 29 """Creates HTTP endpoints client, retries connection errors. |
| 30 |
| 31 All requests to the endpoints will be authenticated with AppEngine app |
| 32 crendetials. |
| 33 |
| 34 Args: |
| 35 api_name: Name of the endpoints API. |
| 36 api_version: Version of the endpoints API. |
| 37 discovery_url: URL of the discovery endpoint. Should contain {api} and |
| 38 {apiVersion} placeholders, e.g. https://your-app.appspot.com/_ah/api/ |
| 39 discovery/v1/apis/{api}/{apiVersion}/rest. |
| 40 http: Optional HTTP object. If not specified httplib2.Http() will be used. |
| 41 num_retries: Maximum number of retries to create client. |
| 42 |
| 43 Returns: |
| 44 Constructed client. |
| 45 """ |
| 46 tries = 0 |
| 47 while True: |
| 48 tries += 1 |
| 49 try: |
| 50 return apiclient.discovery.build( |
| 51 api_name, api_version, |
| 52 discoveryServiceUrl=discovery_url, |
| 53 http=_authenticated_http(http, AUTH_SCOPE)) |
| 54 except apiclient.errors.HttpError as e: |
| 55 if tries == num_tries: |
| 56 logging.exception( |
| 57 'apiclient.discovery.build() failed for %s too many times.', |
| 58 api_name) |
| 59 raise e |
| 60 |
| 61 delay = 2 ** (tries - 1) |
| 62 logging.warn( |
| 63 'apiclient.discovery.build() failed for %s: %s', api_name, e) |
| 64 logging.warn( |
| 65 'Retrying apiclient.discovery.build() in %s seconds.', delay) |
| 66 time.sleep(delay) |
| 67 |
| 68 def retry_request(request, num_tries=5): |
| 69 """Retries provided endpoint request up to num_retries times.""" |
| 70 tries = 0 |
| 71 while True: |
| 72 tries += 1 |
| 73 try: |
| 74 return request.execute() |
| 75 except apiclient.errors.HttpError as e: |
| 76 # This retries internal server (500, 503) and quota (403) errors. |
| 77 # TODO(sergiyb): Figure out if we still need to retry 403 errors. They |
| 78 # were used by codesite to fail on quota errors, but it is unclear whether |
| 79 # Monorail uses same logic or not. |
| 80 if tries == num_tries or e.resp.status not in [403, 500, 503]: |
| 81 raise |
| 82 time.sleep(2 ** (tries - 1)) |
| OLD | NEW |