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

Delta Between Two Patch Sets: sitescripts/oauth2dl/test/dummy_wsgi_app.py

Issue 29833582: Issue 4954 - Implement a downloader script supporting OAuth2 authentication/authorization (Closed)
Left Patch Set: Created July 18, 2018, 1:41 p.m.
Right Patch Set: removed .Python from .gitignore and added .DS_Store to .hgignore Created Aug. 2, 2018, 9:24 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/oauth2dl/test/__init__.py ('k') | sitescripts/oauth2dl/test/test_oauth2dl.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 import json
17 import re
18 from urlparse import parse_qs
19
20 from wsgiref.simple_server import make_server
21
22 _AUTH_RESPONSE = {
23 'access_token': '1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M',
24 'token_type': 'Bearer',
25 'expires_in': 3600,
26 }
27
28 _NOT_FOUND_RESPONSE = {
29 'error': {
30 'errors': [{
31 'domain': 'global',
32 'reason': 'notFound',
33 'message': 'File not found: {}',
34 'locationType': 'parameter',
35 'location': 'fileId',
36 }],
37 'code': '404',
38 'message': 'File not found: {}',
39 },
40 }
41
42
43 def download(environ, start_response):
44 """Send a file from a given path to the client."""
45 d = parse_qs(environ['QUERY_STRING'])
46 path = d.get('path')
47 headers = list()
48 try:
49 file = open(path[0], 'rb')
50 start_response('200 OK', headers)
51 if 'wsgi.file_wrapper' in environ:
52 return environ['wsgi.file_wrapper'](file, 1024)
53
54 return iter(lambda: file.read(1024), '')
55 except IOError:
56 response = _NOT_FOUND_RESPONSE
57 response['error']['errors'][0]['message'] = \
58 response['error']['errors'][0]['message'].format(path)
59 response['error']['message'] = \
60 response['error']['message'].format(path)
61
62 headers = [('content-type', 'text/plain')]
63 start_response('404 NOT FOUND', headers)
64 return [json.dumps(response, encoding='utf-8')]
65
66
67 def auth(environ, start_response):
68 """Simulate a successful authentication using Google Oauth2."""
69 headers = [('content-type', 'application/json')]
70 response = json.dumps(_AUTH_RESPONSE, encoding='utf-8')
71
72 start_response('200 OK', headers)
73
74 return [response]
75
76
77 _URLS = [
78 (r'oauth2/v4/token/?$', auth),
79 (r'download/?$', download),
80 ]
81
82
83 def main(environ, start_response):
84 """Serve as ain entry point for the dummy google API."""
85 path = environ.get('PATH_INFO', '').lstrip('/')
86 for regex, callback in _URLS:
87 match = re.search(regex, path)
88 if match is not None:
89 environ['myapp.url_args'] = match.groups()
90 return callback(environ, start_response)
91
92
93 if __name__ == '__main__':
94 app = make_server('localhost', 8080, main)
95 app.serve_forever()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld