OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 """General functions which are useful throughout this project.""" | 5 """General functions which are useful throughout this project.""" |
6 | 6 |
7 import base64 | 7 import base64 |
8 import binascii | 8 import binascii |
9 import json | 9 import json |
10 import logging | 10 import logging |
11 import os | 11 import os |
12 import re | 12 import re |
13 import time | 13 import time |
14 | 14 |
15 from apiclient import discovery | 15 from apiclient import discovery |
16 from google.appengine.api import urlfetch | 16 from google.appengine.api import urlfetch |
| 17 from google.appengine.api import urlfetch_errors |
17 from google.appengine.api import users | 18 from google.appengine.api import users |
18 from google.appengine.ext import ndb | 19 from google.appengine.ext import ndb |
19 from oauth2client.client import GoogleCredentials | 20 from oauth2client.client import GoogleCredentials |
20 | 21 |
21 from dashboard import stored_object | 22 from dashboard import stored_object |
22 | 23 |
23 INTERNAL_DOMAIN_KEY = 'internal_domain_key' | 24 INTERNAL_DOMAIN_KEY = 'internal_domain_key' |
24 SHERIFF_DOMAINS_KEY = 'sheriff_domains_key' | 25 SHERIFF_DOMAINS_KEY = 'sheriff_domains_key' |
25 IP_WHITELIST_KEY = 'ip_whitelist' | 26 IP_WHITELIST_KEY = 'ip_whitelist' |
26 _PROJECT_ID_KEY = 'project_id' | 27 _PROJECT_ID_KEY = 'project_id' |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 % (expected_type, actual_type)) | 332 % (expected_type, actual_type)) |
332 missing = set(expected.keys()) - set(actual.keys()) | 333 missing = set(expected.keys()) - set(actual.keys()) |
333 if missing: | 334 if missing: |
334 raise ValueError('Missing the following properties: %s' | 335 raise ValueError('Missing the following properties: %s' |
335 % ','.join(missing)) | 336 % ','.join(missing)) |
336 for key in expected: | 337 for key in expected: |
337 Validate(expected[key], actual[key]) | 338 Validate(expected[key], actual[key]) |
338 elif not IsValidType(expected, actual): | 339 elif not IsValidType(expected, actual): |
339 raise ValueError('Invalid type. Expected: %s. Actual: %s.' % | 340 raise ValueError('Invalid type. Expected: %s. Actual: %s.' % |
340 (expected, actual_type)) | 341 (expected, actual_type)) |
| 342 |
| 343 |
| 344 def FetchURL(request_url, skip_status_code=False): |
| 345 """Wrapper around URL fetch service to make request. |
| 346 |
| 347 Args: |
| 348 request_url: URL of request. |
| 349 skip_status_code: Skips return code check when True, default is False. |
| 350 |
| 351 Returns: |
| 352 Response object return by URL fetch, otherwise None when there's an error. |
| 353 """ |
| 354 logging.info('URL being fetched: ' + request_url) |
| 355 try: |
| 356 response = urlfetch.fetch(request_url) |
| 357 except urlfetch_errors.DeadlineExceededError: |
| 358 logging.error('Deadline exceeded error checking %s', request_url) |
| 359 return None |
| 360 except urlfetch_errors.DownloadError as err: |
| 361 # DownloadError is raised to indicate a non-specific failure when there |
| 362 # was not a 4xx or 5xx status code. |
| 363 logging.error(err) |
| 364 return None |
| 365 if skip_status_code: |
| 366 return response |
| 367 elif response.status_code != 200: |
| 368 logging.error( |
| 369 'ERROR %s checking %s', response.status_code, request_url) |
| 370 return None |
| 371 return response |
OLD | NEW |