-
Notifications
You must be signed in to change notification settings - Fork 675
/
app.py
233 lines (188 loc) · 6.39 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from __future__ import absolute_import
import json
import os
from urlparse import urlparse
from flask import Flask, render_template, request, redirect, session
from flask_sslify import SSLify
from rauth import OAuth2Service
import requests
app = Flask(__name__, static_folder='static', static_url_path='')
app.requests_session = requests.Session()
app.secret_key = os.urandom(24)
sslify = SSLify(app)
with open('config.json') as f:
config = json.load(f)
def generate_oauth_service():
"""Prepare the OAuth2Service that is used to make requests later."""
return OAuth2Service(
client_id=os.environ.get('UBER_CLIENT_ID'),
client_secret=os.environ.get('UBER_CLIENT_SECRET'),
name=config.get('name'),
authorize_url=config.get('authorize_url'),
access_token_url=config.get('access_token_url'),
base_url=config.get('base_url'),
)
def generate_ride_headers(token):
"""Generate the header object that is used to make api requests."""
return {
'Authorization': 'bearer %s' % token,
'Content-Type': 'application/json',
}
@app.route('/health', methods=['GET'])
def health():
"""Check the status of this application."""
return ';-)'
@app.route('/', methods=['GET'])
def signup():
"""The first step in the three-legged OAuth handshake.
You should navigate here first. It will redirect to login.uber.com.
"""
params = {
'response_type': 'code',
'redirect_uri': get_redirect_uri(request),
'scopes': ','.join(config.get('scopes')),
}
url = generate_oauth_service().get_authorize_url(**params)
return redirect(url)
@app.route('/submit', methods=['GET'])
def submit():
"""The other two steps in the three-legged Oauth handshake.
Your redirect uri will redirect you here, where you will exchange
a code that can be used to obtain an access token for the logged-in use.
"""
params = {
'redirect_uri': get_redirect_uri(request),
'code': request.args.get('code'),
'grant_type': 'authorization_code'
}
response = app.requests_session.post(
config.get('access_token_url'),
auth=(
os.environ.get('UBER_CLIENT_ID'),
os.environ.get('UBER_CLIENT_SECRET')
),
data=params,
)
session['access_token'] = response.json().get('access_token')
return render_template(
'success.html',
token=response.json().get('access_token')
)
@app.route('/demo', methods=['GET'])
def demo():
"""Demo.html is a template that calls the other routes in this example."""
return render_template('demo.html', token=session.get('access_token'))
@app.route('/products', methods=['GET'])
def products():
"""Example call to the products endpoint.
Returns all the products currently available in San Francisco.
"""
url = config.get('base_uber_url') + 'products'
params = {
'latitude': config.get('start_latitude'),
'longitude': config.get('start_longitude'),
}
response = app.requests_session.get(
url,
headers=generate_ride_headers(session.get('access_token')),
params=params,
)
if response.status_code != 200:
return 'There was an error', response.status_code
return render_template(
'results.html',
endpoint='products',
data=response.text,
)
@app.route('/time', methods=['GET'])
def time():
"""Example call to the time estimates endpoint.
Returns the time estimates from the given lat/lng given below.
"""
url = config.get('base_uber_url') + 'estimates/time'
params = {
'start_latitude': config.get('start_latitude'),
'start_longitude': config.get('start_longitude'),
}
response = app.requests_session.get(
url,
headers=generate_ride_headers(session.get('access_token')),
params=params,
)
if response.status_code != 200:
return 'There was an error', response.status_code
return render_template(
'results.html',
endpoint='time',
data=response.text,
)
@app.route('/price', methods=['GET'])
def price():
"""Example call to the price estimates endpoint.
Returns the time estimates from the given lat/lng given below.
"""
url = config.get('base_uber_url') + 'estimates/price'
params = {
'start_latitude': config.get('start_latitude'),
'start_longitude': config.get('start_longitude'),
'end_latitude': config.get('end_latitude'),
'end_longitude': config.get('end_longitude'),
}
response = app.requests_session.get(
url,
headers=generate_ride_headers(session.get('access_token')),
params=params,
)
if response.status_code != 200:
return 'There was an error', response.status_code
return render_template(
'results.html',
endpoint='price',
data=response.text,
)
@app.route('/history', methods=['GET'])
def history():
"""Return the last 5 trips made by the logged in user."""
url = config.get('base_uber_url_v1_1') + 'history'
params = {
'offset': 0,
'limit': 5,
}
response = app.requests_session.get(
url,
headers=generate_ride_headers(session.get('access_token')),
params=params,
)
if response.status_code != 200:
return 'There was an error', response.status_code
return render_template(
'results.html',
endpoint='history',
data=response.text,
)
@app.route('/me', methods=['GET'])
def me():
"""Return user information including name, picture and email."""
url = config.get('base_uber_url') + 'me'
response = app.requests_session.get(
url,
headers=generate_ride_headers(session.get('access_token')),
)
if response.status_code != 200:
return 'There was an error', response.status_code
return render_template(
'results.html',
endpoint='me',
data=response.text,
)
def get_redirect_uri(request):
"""Return OAuth redirect URI."""
parsed_url = urlparse(request.url)
if parsed_url.hostname == 'localhost':
return 'http://{hostname}:{port}/submit'.format(
hostname=parsed_url.hostname, port=parsed_url.port
)
return 'https://{hostname}/submit'.format(hostname=parsed_url.hostname)
if __name__ == '__main__':
app.debug = os.environ.get('FLASK_DEBUG', True)
app.run(port=7000)