Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/oauth2dl/utils/dummy_wsgi_app.py

Issue 29833582: Issue 4954 - Implement a downloader script supporting OAuth2 authentication/authorization (Closed)
Patch Set: Created July 18, 2018, 1:41 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sitescripts/oauth2dl/utils/dummy_wsgi_app.py
diff --git a/sitescripts/oauth2dl/utils/dummy_wsgi_app.py b/sitescripts/oauth2dl/utils/dummy_wsgi_app.py
new file mode 100644
index 0000000000000000000000000000000000000000..9265464570aec0c0552dadec39a7588e850df79c
--- /dev/null
+++ b/sitescripts/oauth2dl/utils/dummy_wsgi_app.py
@@ -0,0 +1,94 @@
+# This file is part of the Adblock Plus web scripts,
Vasily Kuznetsov 2018/07/20 21:00:01 I would just move this into the test folder. Or is
Tudor Avram 2018/07/23 19:29:03 Acknowledged.
Tudor Avram 2018/07/31 09:20:18 Done.
+# Copyright (C) 2006-present eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+from wsgiref.simple_server import make_server
+import json
+import re
+from urlparse import parse_qs
+
+_AUTH_RESPONSE = {
+ 'access_token': '1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M',
+ 'token_type': 'Bearer',
+ 'expires_in': 3600,
+}
+
+_NOT_FOUND_RESPONSE = {
+ 'error': {
+ 'errors': [{
+ 'domain': 'global',
+ 'reason': 'notFound',
+ 'message': 'File not found: {}',
+ 'locationType': 'parameter',
+ 'location': 'fileId',
+ }],
+ 'code': '404',
+ 'message': 'File not found: {}',
+ },
+}
+
+
+def download(environ, start_response):
+ """Send a file from a given path to the client."""
+ d = parse_qs(environ['QUERY_STRING'])
+ path = d.get('path')
+ headers = list()
+ try:
+ file = open(path[0], 'rb')
+ start_response('200 OK', headers)
+ if 'wsgi.file_wrapper' in environ:
+ return environ['wsgi.file_wrapper'](file, 1024)
+ else:
+ return iter(lambda: file.read(1024), '')
+ except IOError:
+ response = _NOT_FOUND_RESPONSE
+ response['error']['errors'][0]['message'] = \
+ response['error']['errors'][0]['message'].format(path)
+ response['error']['message'] = \
+ response['error']['message'].format(path)
+
+ headers = [('content-type', 'text/plain')]
+ start_response('404 NOT FOUND', headers)
+ return [json.dumps(response, encoding='utf-8')]
+
+
+def auth(environ, start_response):
+ """Simulate a successful authentication using Google Oauth2."""
+ headers = [('content-type', 'application/json')]
+ response = json.dumps(_AUTH_RESPONSE, encoding='utf-8')
+
+ start_response('200 OK', headers)
+
+ return [response]
+
+
+_URLS = [
+ (r'oauth2/v4/token/?$', auth),
+ (r'download/?$', download),
+]
+
+
+def main(environ, start_response):
+ """Serve as ain entry point for the dummy google API."""
+ path = environ.get('PATH_INFO', '').lstrip('/')
+ for regex, callback in _URLS:
+ match = re.search(regex, path)
+ if match is not None:
+ environ['myapp.url_args'] = match.groups()
+ return callback(environ, start_response)
+
+
+if __name__ == '__main__':
+ app = make_server('localhost', 8080, main)
+ app.serve_forever()

Powered by Google App Engine
This is Rietveld