Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: appengine/chromium_try_flakes/endpoints/endpoints.py

Issue 2387153002: Report all flakes reported to issue tracker also to FindIt (Closed)
Patch Set: Refactor and tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 # TODO(akuegel): Do we want to use a different timeout? Do we want to use a
17 # cache? See documentation here:
18 # https://github.com/jcgregorio/httplib2/blob/master/python2/httplib2/__init__.p y#L1142
19 def _authenticated_http(http, scope):
20 credentials = oauth2client.appengine.AppAssertionCredentials(scope=scope)
21 return credentials.authorize(http or httplib2.Http())
22
23
24 def build_client(api_name, api_version, discovery_url, http=None,
25 num_tries=5):
26 """Creates HTTP endpoints client, retries connection errors.
27
28 All requests to the endpoints will be authenticated with AppEngine app
29 crendetials.
30
31 Args:
32 api_name: Name of the endpoints API.
33 api_version: Version of the endpoints API.
34 discovery_url: URL of the discovery endpoint. Should contain {api} and
35 {apiVersion} placeholders, e.g. https://your-app.appspot.com/_ah/api/
36 discovery/v1/apis/{api}/{apiVersion}/rest.
37 http: Optional HTTP object. If not specified httplib2.Http() will be used.
38 num_retries: Maximum number of retries to create client.
39
40 Returns:
41 Constructed client.
42 """
43 tries = 0
44 while True:
45 tries += 1
46 try:
47 auth_http = _authenticated_http(
48 http, 'https://www.googleapis.com/auth/userinfo.email')
stgao 2016/10/05 00:28:47 nit: make this a constant var
Sergiy Byelozyorov 2016/10/05 13:05:25 Done.
49 return apiclient.discovery.build(
50 api_name, api_version,
51 discoveryServiceUrl=discovery_url,
52 http=auth_http)
53 except apiclient.errors.HttpError as e:
54 if tries == num_tries:
55 logging.exception(
56 'apiclient.discovery.build() failed for %s too many times.',
57 api_name)
58 raise e
59
60 delay = 2**(tries-1)
stgao 2016/10/05 00:28:47 nit: space before and after "-". And "**" too?
Sergiy Byelozyorov 2016/10/05 13:05:25 Done.
61 logging.warn(
62 'apiclient.discovery.build() failed for %s: %s', api_name, e)
63 logging.warn(
64 'Retrying apiclient.discovery.build() in %s seconds.', delay)
65 time.sleep(delay)
66
67 def retry_request(request, num_tries=5):
68 """Retries provided endpoint request up to num_retries times."""
69 tries = 0
70 while True:
71 tries += 1
72 try:
73 return request.execute()
74 except apiclient.errors.HttpError as e:
75 # This retries internal server (500, 503) and quota (403) errors.
76 # TODO(sergiyb): Figure out if we still need to retry 403 errors. They
77 # were used by codesite to fail on quota errors, but it is unclear whether
78 # Monorail uses same logic or not.
79 if tries == num_tries or e.resp.status not in [403, 500, 503]:
80 raise
81 time.sleep(2**(tries-1))
stgao 2016/10/05 00:28:47 same here.
Sergiy Byelozyorov 2016/10/05 13:05:25 Done.
OLDNEW
« no previous file with comments | « appengine/chromium_try_flakes/endpoints/__init__.py ('k') | appengine/chromium_try_flakes/endpoints/test/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698