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

Side by Side Diff: appengine/findit/common/http_client_appengine.py

Issue 2079773004: [Findit] Add a Rietveld client to post a message to codereview of a culprit. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 4 years, 6 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
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import json 5 import json
6 import logging 6 import logging
7 7
8 from google.appengine.api import urlfetch 8 from google.appengine.api import urlfetch
9 9
10 from common import auth_util 10 from common import auth_util
11 from common.retry_http_client import RetryHttpClient 11 from common.retry_http_client import RetryHttpClient
12 12
13 13
14 #TODO(katesonia): Move this to config. 14 #TODO(katesonia): Move this to config.
lijeffrey 2016/06/21 17:42:52 nit: add a space after # to keep pylint from compl
stgao 2016/06/24 04:24:51 Done.
15 _INTERNAL_HOSTS_TO_SCOPES = { 15 _INTERNAL_HOSTS_TO_SCOPES = {
16 'https://chrome-internal.googlesource.com/': ( 16 'https://chrome-internal.googlesource.com/': (
17 'https://www.googleapis.com/auth/gerritcodereview') 17 'https://www.googleapis.com/auth/gerritcodereview'),
18 'https://codereview.chromium.org/': (
19 'https://www.googleapis.com/auth/userinfo.email'),
18 } 20 }
19 21
20 22
21 class HttpClientAppengine(RetryHttpClient): # pragma: no cover 23 class HttpClientAppengine(RetryHttpClient): # pragma: no cover
22 """A http client for running on appengine.""" 24 """A http client for running on appengine."""
23 25
24 def _ExpandAuthorizationHeaders(self, headers, scope): 26 def _ExpandAuthorizationHeaders(self, headers, scope):
25 headers['Authorization'] = 'Bearer ' + auth_util.GetAuthToken(scope) 27 headers['Authorization'] = 'Bearer ' + auth_util.GetAuthToken(scope)
26 28
27 def _ShouldLogError(self, status_code): 29 def _ShouldLogError(self, status_code):
28 if not self.no_error_logging_statuses: 30 if not self.no_error_logging_statuses:
29 return True 31 return True
30 return status_code not in self.no_error_logging_statuses 32 return status_code not in self.no_error_logging_statuses
31 33
32 def _SendRequest(self, url, method, data, timeout, headers=None): 34 def _SendRequest(self, url, method, data, timeout, headers=None):
33 # We wanted to validate certificate to avoid the man in the middle. 35 # We wanted to validate certificate to avoid the man in the middle.
34 if not headers: 36 if not headers:
35 headers = {} 37 headers = {}
36 38
37 # For google internal hosts, expand Oauth2.0 token to headers to authorize 39 # For google internal hosts, expand Oauth2.0 token to headers to authorize
38 # the requests. 40 # the requests.
39 for host, scope in _INTERNAL_HOSTS_TO_SCOPES.iteritems(): 41 for host, scope in _INTERNAL_HOSTS_TO_SCOPES.iteritems():
40 if url.startswith(host): 42 if url.startswith(host):
41 self._ExpandAuthorizationHeaders(headers, scope) 43 self._ExpandAuthorizationHeaders(headers, scope)
44 logging.debug('Authorization header of scope %s was added for %s',
45 scope, url)
42 break 46 break
43 47
44 if method in (urlfetch.POST, urlfetch.PUT): 48 if method in (urlfetch.POST, urlfetch.PUT):
45 result = urlfetch.fetch( 49 result = urlfetch.fetch(
46 url, payload=data, method=method, 50 url, payload=data, method=method, headers=headers, deadline=timeout,
47 headers=headers, deadline=timeout, validate_certificate=True) 51 follow_redirects=False, validate_certificate=True)
48 else: 52 else:
49 result = urlfetch.fetch( 53 result = urlfetch.fetch(
50 url, headers=headers, deadline=timeout, validate_certificate=True) 54 url, headers=headers, deadline=timeout,
55 follow_redirects=False, validate_certificate=True)
51 56
52 if (result.status_code != 200 and self._ShouldLogError(result.status_code)): 57 if (result.status_code != 200 and self._ShouldLogError(result.status_code)):
53 logging.error('Request to %s resulted in %d, headers:%s', url, 58 logging.error('Request to %s resulted in %d, headers:%s', url,
54 result.status_code, json.dumps(result.headers.items())) 59 result.status_code, json.dumps(result.headers.items()))
55 60
56 return result.status_code, result.content 61 return result.status_code, result.content
57 62
58 def _Get(self, url, timeout, headers): 63 def _Get(self, url, timeout, headers):
59 return self._SendRequest(url, urlfetch.GET, None, timeout, headers) 64 return self._SendRequest(url, urlfetch.GET, None, timeout, headers)
60 65
61 def _Post(self, url, data, timeout, headers): 66 def _Post(self, url, data, timeout, headers):
62 return self._SendRequest(url, urlfetch.POST, data, timeout, headers) 67 return self._SendRequest(url, urlfetch.POST, data, timeout, headers)
63 68
64 def _Put(self, url, data, timeout, headers): 69 def _Put(self, url, data, timeout, headers):
65 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers) 70 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698