-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathutils.py
More file actions
189 lines (148 loc) · 5.55 KB
/
utils.py
File metadata and controls
189 lines (148 loc) · 5.55 KB
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
# Copyright (c) 2017 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""General utilities for command line examples."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from collections import namedtuple
from yaml import safe_load
from uber_rides.client import UberRidesClient
from uber_rides.session import OAuth2Credential
from uber_rides.session import Session
# set your app credentials here
CREDENTIALS_FILENAME = 'example/config.rider.yaml'
# where your OAuth 2.0 credentials are stored
STORAGE_FILENAME = 'example/oauth2_session_store.yaml'
DEFAULT_CONFIG_VALUES = frozenset([
'INSERT_CLIENT_ID_HERE',
'INSERT_CLIENT_SECRET_HERE',
'INSERT_REDIRECT_URL_HERE',
])
Colors = namedtuple('Colors', 'response, success, fail, end')
COLORS = Colors(
response='\033[94m',
success='\033[92m',
fail='\033[91m',
end='\033[0m',
)
def success_print(message):
"""Print a message in green text.
Parameters
message (str)
Message to print.
"""
print(COLORS.success, message, COLORS.end)
def response_print(message):
"""Print a message in blue text.
Parameters
message (str)
Message to print.
"""
print(COLORS.response, message, COLORS.end)
def fail_print(error):
"""Print an error in red text.
Parameters
error (HTTPError)
Error object to print.
"""
print(COLORS.fail, error.message, COLORS.end)
def paragraph_print(message):
"""Print message with padded newlines.
Parameters
message (str)
Message to print.
"""
paragraph = '\n{}\n'
print(paragraph.format(message))
def import_app_credentials(filename=CREDENTIALS_FILENAME):
"""Import app credentials from configuration file.
Parameters
filename (str)
Name of configuration file.
Returns
credentials (dict)
All your app credentials and information
imported from the configuration file.
"""
with open(filename, 'r') as config_file:
config = safe_load(config_file)
client_id = config['client_id']
client_secret = config['client_secret']
redirect_url = config['redirect_url']
config_values = [client_id, client_secret, redirect_url]
for value in config_values:
if value in DEFAULT_CONFIG_VALUES:
exit('Missing credentials in {}'.format(filename))
credentials = {
'client_id': client_id,
'client_secret': client_secret,
'redirect_url': redirect_url,
'scopes': set(config['scopes']),
}
return credentials
def import_oauth2_credentials(filename=STORAGE_FILENAME):
"""Import OAuth 2.0 session credentials from storage file.
Parameters
filename (str)
Name of storage file.
Returns
credentials (dict)
All your app credentials and information
imported from the configuration file.
"""
with open(filename, 'r') as storage_file:
storage = safe_load(storage_file)
# depending on OAuth 2.0 grant_type, these values may not exist
client_secret = storage.get('client_secret')
redirect_url = storage.get('redirect_url')
refresh_token = storage.get('refresh_token')
credentials = {
'access_token': storage['access_token'],
'client_id': storage['client_id'],
'client_secret': client_secret,
'expires_in_seconds': storage['expires_in_seconds'],
'grant_type': storage['grant_type'],
'redirect_url': redirect_url,
'refresh_token': refresh_token,
'scopes': storage['scopes'],
}
return credentials
def create_uber_client(credentials):
"""Create an UberRidesClient from OAuth 2.0 credentials.
Parameters
credentials (dict)
Dictionary of OAuth 2.0 credentials.
Returns
(UberRidesClient)
An authorized UberRidesClient to access API resources.
"""
oauth2credential = OAuth2Credential(
client_id=credentials.get('client_id'),
access_token=credentials.get('access_token'),
expires_in_seconds=credentials.get('expires_in_seconds'),
scopes=credentials.get('scopes'),
grant_type=credentials.get('grant_type'),
redirect_url=credentials.get('redirect_url'),
client_secret=credentials.get('client_secret'),
refresh_token=credentials.get('refresh_token'),
)
session = Session(oauth2credential=oauth2credential)
return UberRidesClient(session, sandbox_mode=True)