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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: appengine/chromium_try_flakes/endpoints/endpoints.py
diff --git a/appengine/chromium_try_flakes/endpoints/endpoints.py b/appengine/chromium_try_flakes/endpoints/endpoints.py
new file mode 100644
index 0000000000000000000000000000000000000000..b8024967cc78574cb04232de5a7bcf59bd5738c5
--- /dev/null
+++ b/appengine/chromium_try_flakes/endpoints/endpoints.py
@@ -0,0 +1,81 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Provides functions to work with AppEngine endpoints"""
+
+import httplib2
+import logging
+import time
+
+import apiclient.discovery
+import apiclient.errors
+import oauth2client.appengine
+
+
+# TODO(akuegel): Do we want to use a different timeout? Do we want to use a
+# cache? See documentation here:
+# https://github.com/jcgregorio/httplib2/blob/master/python2/httplib2/__init__.py#L1142
+def _authenticated_http(http, scope):
+ credentials = oauth2client.appengine.AppAssertionCredentials(scope=scope)
+ return credentials.authorize(http or httplib2.Http())
+
+
+def build_client(api_name, api_version, discovery_url, http=None,
+ num_tries=5):
+ """Creates HTTP endpoints client, retries connection errors.
+
+ All requests to the endpoints will be authenticated with AppEngine app
+ crendetials.
+
+ Args:
+ api_name: Name of the endpoints API.
+ api_version: Version of the endpoints API.
+ discovery_url: URL of the discovery endpoint. Should contain {api} and
+ {apiVersion} placeholders, e.g. https://your-app.appspot.com/_ah/api/
+ discovery/v1/apis/{api}/{apiVersion}/rest.
+ http: Optional HTTP object. If not specified httplib2.Http() will be used.
+ num_retries: Maximum number of retries to create client.
+
+ Returns:
+ Constructed client.
+ """
+ tries = 0
+ while True:
+ tries += 1
+ try:
+ auth_http = _authenticated_http(
+ 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.
+ return apiclient.discovery.build(
+ api_name, api_version,
+ discoveryServiceUrl=discovery_url,
+ http=auth_http)
+ except apiclient.errors.HttpError as e:
+ if tries == num_tries:
+ logging.exception(
+ 'apiclient.discovery.build() failed for %s too many times.',
+ api_name)
+ raise e
+
+ 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.
+ logging.warn(
+ 'apiclient.discovery.build() failed for %s: %s', api_name, e)
+ logging.warn(
+ 'Retrying apiclient.discovery.build() in %s seconds.', delay)
+ time.sleep(delay)
+
+def retry_request(request, num_tries=5):
+ """Retries provided endpoint request up to num_retries times."""
+ tries = 0
+ while True:
+ tries += 1
+ try:
+ return request.execute()
+ except apiclient.errors.HttpError as e:
+ # This retries internal server (500, 503) and quota (403) errors.
+ # TODO(sergiyb): Figure out if we still need to retry 403 errors. They
+ # were used by codesite to fail on quota errors, but it is unclear whether
+ # Monorail uses same logic or not.
+ if tries == num_tries or e.resp.status not in [403, 500, 503]:
+ raise
+ time.sleep(2**(tries-1))
stgao 2016/10/05 00:28:47 same here.
Sergiy Byelozyorov 2016/10/05 13:05:25 Done.
« 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