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