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

Unified Diff: chrome/common/extensions/docs/server2/url_fetcher.py

Issue 1151283007: Docserver overhaul: Gitiles away from me. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove inform_users template to fix presubmit failure (it's now a redirect) Created 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/url_fetcher.py
diff --git a/chrome/common/extensions/docs/server2/url_fetcher.py b/chrome/common/extensions/docs/server2/url_fetcher.py
new file mode 100644
index 0000000000000000000000000000000000000000..9daa5fae553abe8e13370218623b3016784eaade
--- /dev/null
+++ b/chrome/common/extensions/docs/server2/url_fetcher.py
@@ -0,0 +1,60 @@
+# Copyright 2015 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.
+
+import posixpath
+
+from environment import GetAppVersion
+from future import Future
+from urllib import urlencode
+from urlparse import urlparse, parse_qs
+
+def _MakeHeaders(headers={}):
+ headers['User-Agent'] = 'Chomium docserver %s' % GetAppVersion()
+ headers['Cache-Control'] = 'max-age=0'
+ return headers
+
+
+def _AddQueryToUrl(url, new_query):
+ """Adds query paramters to a URL. This merges the given set of query params
+ with any that were already a part of the URL.
+ """
+ parsed_url = urlparse(url)
+ query = parse_qs(parsed_url.query)
+ query.update(new_query)
+ query_string = urlencode(query, True)
+ return '%s://%s%s?%s#%s' % (parsed_url.scheme, parsed_url.netloc,
+ parsed_url.path, query_string, parsed_url.fragment)
+
+
+class UrlFetcher(object):
+ def __init__(self):
+ self._base_path = None
+
+ def SetBasePath(self, base_path):
+ assert base_path is None or not base_path.endswith('/'), base_path
+ self._base_path = base_path
+
+ def Fetch(self, url, headers={}, query={}):
+ return self.FetchImpl(_AddQueryToUrl(self._FromBasePath(url), query),
+ _MakeHeaders(headers))
+
+ def FetchAsync(self, url, headers={}, query={}):
+ return self.FetchAsyncImpl(_AddQueryToUrl(self._FromBasePath(url), query),
+ _MakeHeaders(headers))
+
+ def FetchImpl(self, url, headers):
+ """Fetches a URL synchronously.
+ """
+ raise NotImplementedError(self.__class__)
+
+ def FetchAsyncImpl(self, url, headers):
+ """Fetches a URL asynchronously and returns a Future with the result.
+ """
+ raise NotImplementedError(self.__class__)
+
+ def _FromBasePath(self, url):
+ assert not url.startswith('/'), url
+ if self._base_path is not None:
+ url = posixpath.join(self._base_path, url) if url else self._base_path
+ return url
« no previous file with comments | « chrome/common/extensions/docs/server2/url_constants.py ('k') | chrome/common/extensions/docs/server2/url_fetcher_appengine.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698