Chromium Code Reviews| OLD | NEW |
|---|---|
| 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.retry_http_client import RetryHttpClient | 11 from common.retry_http_client import RetryHttpClient |
| 11 | 12 |
| 13 _CHROME_INTERNAL_HOST = 'https://chrome-internal.googlesource.com' | |
|
stgao
2016/05/06 19:11:24
Is it better for this to be a list?
Sharu Jiang
2016/05/06 20:27:27
Done.
| |
| 14 | |
| 12 | 15 |
| 13 class HttpClientAppengine(RetryHttpClient): # pragma: no cover | 16 class HttpClientAppengine(RetryHttpClient): # pragma: no cover |
| 14 """A http client for running on appengine.""" | 17 """A http client for running on appengine.""" |
| 18 | |
| 19 def _GetExpandedHeaders(self, url, headers): | |
|
stgao
2016/05/06 19:11:24
nit: this function is only for adding Oauth2.0 tok
Sharu Jiang
2016/05/06 20:27:27
Done.
| |
| 20 #TODO(katesonia): Move this to config. | |
|
stgao
2016/05/06 19:11:24
nit: The host above needs to be moved to config, n
Sharu Jiang
2016/05/06 20:27:27
Done.
| |
| 21 host_to_expanded_headers = { | |
| 22 _CHROME_INTERNAL_HOST: { | |
| 23 'Authorization': 'Bearer ' + auth_util.GetAuthToken()} | |
|
stgao
2016/05/06 19:11:24
This might involve an api request, and is only use
Sharu Jiang
2016/05/06 20:27:27
Done.
| |
| 24 } | |
| 25 | |
| 26 if not headers: | |
| 27 headers = {} | |
| 28 | |
| 29 for host, expanded_headers in host_to_expanded_headers.iteritems(): | |
| 30 if url.startswith(host): | |
| 31 return headers.update(expanded_headers) | |
| 32 | |
| 33 return headers | |
| 34 | |
| 15 def _ShouldLogError(self, status_code): | 35 def _ShouldLogError(self, status_code): |
| 16 if not self.no_error_logging_statuses: | 36 if not self.no_error_logging_statuses: |
| 17 return True | 37 return True |
| 18 return status_code not in self.no_error_logging_statuses | 38 return status_code not in self.no_error_logging_statuses |
| 19 | 39 |
| 20 def _SendRequest(self, url, method, data, timeout, headers=None): | 40 def _SendRequest(self, url, method, data, timeout, headers=None): |
| 21 # We wanted to validate certificate to avoid the man in the middle. | 41 # We wanted to validate certificate to avoid the man in the middle. |
| 22 if not headers: | 42 headers = self._GetExpandedHeaders(url, headers) |
| 23 headers = {} | 43 |
| 24 if method in (urlfetch.POST, urlfetch.PUT): | 44 if method in (urlfetch.POST, urlfetch.PUT): |
| 25 result = urlfetch.fetch( | 45 result = urlfetch.fetch( |
| 26 url, payload=data, method=method, | 46 url, payload=data, method=method, |
| 27 headers=headers, deadline=timeout, validate_certificate=True) | 47 headers=headers, deadline=timeout, validate_certificate=True) |
| 28 else: | 48 else: |
| 29 result = urlfetch.fetch( | 49 result = urlfetch.fetch( |
| 30 url, headers=headers, deadline=timeout, validate_certificate=True) | 50 url, headers=headers, deadline=timeout, validate_certificate=True) |
| 31 | 51 |
| 32 if (result.status_code != 200 and self._ShouldLogError(result.status_code)): | 52 if (result.status_code != 200 and self._ShouldLogError(result.status_code)): |
| 33 logging.error('Request to %s resulted in %d, headers:%s', url, | 53 logging.error('Request to %s resulted in %d, headers:%s', url, |
| 34 result.status_code, json.dumps(result.headers.items())) | 54 result.status_code, json.dumps(result.headers.items())) |
| 35 | 55 |
| 36 return result.status_code, result.content | 56 return result.status_code, result.content |
| 37 | 57 |
| 38 def _Get(self, url, timeout, headers): | 58 def _Get(self, url, timeout, headers): |
| 39 return self._SendRequest(url, urlfetch.GET, None, timeout, headers) | 59 return self._SendRequest(url, urlfetch.GET, None, timeout, headers) |
| 40 | 60 |
| 41 def _Post(self, url, data, timeout, headers): | 61 def _Post(self, url, data, timeout, headers): |
| 42 return self._SendRequest(url, urlfetch.POST, data, timeout, headers) | 62 return self._SendRequest(url, urlfetch.POST, data, timeout, headers) |
| 43 | 63 |
| 44 def _Put(self, url, data, timeout, headers): | 64 def _Put(self, url, data, timeout, headers): |
| 45 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers) | 65 return self._SendRequest(url, urlfetch.PUT, data, timeout, headers) |
| OLD | NEW |