Index: dashboard/dashboard/utils.py |
diff --git a/dashboard/dashboard/utils.py b/dashboard/dashboard/utils.py |
index bbeccbc4843f055f7c451ab465ff534454f7cd79..6ba4a0131e39d56ee44c2d3f3c383060346f2d62 100644 |
--- a/dashboard/dashboard/utils.py |
+++ b/dashboard/dashboard/utils.py |
@@ -13,6 +13,7 @@ import time |
from apiclient import discovery |
from google.appengine.api import urlfetch |
+from google.appengine.api import urlfetch_errors |
from google.appengine.api import users |
from google.appengine.ext import ndb |
from oauth2client.client import GoogleCredentials |
@@ -278,3 +279,33 @@ def DownloadChromiumFile(path): |
logging.error('Failed to decode "%s" from "%s".', response.content, url) |
return None |
return plaintext_content |
+ |
+ |
+def FetchURL(request_url, skip_status_code=False): |
+ """Wrapper around URL fetch service to make request. |
+ |
+ Args: |
+ request_url: URL of request. |
+ skip_status_code: Skips return code check when True, default is False. |
+ |
+ Returns: |
+ Response object return by URL fetch, otherwise None when there's an error. |
+ """ |
+ logging.info('URL being fetched: ' + request_url) |
+ try: |
+ response = urlfetch.fetch(request_url) |
+ except urlfetch_errors.DeadlineExceededError: |
+ logging.error('Deadline exceeded error checking %s', request_url) |
+ return None |
+ except urlfetch_errors.DownloadError as err: |
+ # DownloadError is raised to indicate a non-specific failure when there |
+ # was not a 4xx or 5xx status code. |
+ logging.error(err) |
+ return None |
+ if skip_status_code: |
+ return response |
+ elif response.status_code != 200: |
+ logging.error( |
+ 'ERROR %s checking %s', response.status_code, request_url) |
+ return None |
+ return response |