-
Notifications
You must be signed in to change notification settings - Fork 4
/
rest.py
281 lines (221 loc) · 9.11 KB
/
rest.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright 2021 RelationalAI, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Low level HTTP interface to the RelationalAI REST API."""
import json
import logging
from os import path, makedirs
from urllib.error import URLError
from urllib.parse import urlencode, urlsplit, quote
from urllib.request import Request, urlopen
from .__init__ import __version__
from .credentials import (
AccessToken,
Credentials,
ClientCredentials,
)
__all__ = ["Context", "get", "put", "post", "request"]
ACCESS_KEY_TOKEN_KEY = "access_token"
CLIENT_ID_KEY = "client_id"
CLIENT_SECRET_KEY = "client_secret"
AUDIENCE_KEY = "audience"
GRANT_TYPE_KEY = "grant_type"
CLIENT_CREDENTIALS_KEY = "client_credentials"
EXPIRES_IN_KEY = "expires_in"
SCOPE = "scope"
# logger
logger = logging.getLogger(__package__)
# Context contains the state required to make rAI REST API calls.
class Context(object):
def __init__(self, region: str = None, credentials: Credentials = None, retries: int = 0):
if retries < 0:
raise ValueError("Retries must be a non-negative integer")
self.region = region or "us-east"
self.credentials = credentials
self.service = "transaction"
self.retries = retries
# Answers if the keys of the passed dict contain a case insensitive match
# for the given term.
def _contains_insensitive(items: dict, term: str) -> bool:
term = term.casefold()
for item in items:
item = item.casefold()
if term == item:
return True
return False
# Fill in any missing headers.
def _default_headers(url: str, headers: dict = None) -> dict:
headers = headers or {}
if not _contains_insensitive(headers, "accept"):
headers["Accept"] = "application/json"
if not _contains_insensitive(headers, "content-type"):
headers["Content-Type"] = "application/json"
if not _contains_insensitive(headers, "host"):
headers["Host"] = _get_host(url)
if not _contains_insensitive(headers, "user-agent"):
headers["User-Agent"] = _default_user_agent()
return headers
def _default_user_agent() -> str:
return f"rai-sdk-python/{__version__}"
def _encode(data) -> bytes:
if not data:
return data
if not isinstance(data, str):
data = json.dumps(data)
return data.encode("utf8")
def _encode_path(path: str) -> str:
# double encoding as per AWS v4
return quote(quote(path))
# Returns an urlencoded query string.
# Note: the signing algo below **requires** query params to be sorted.
def _encode_qs(kwargs: dict) -> str:
args = sorted([(k, v) for k, v in kwargs.items()])
return urlencode(args)
# Retrieve the hostname from the given url.
def _get_host(url: str) -> str:
return urlsplit(url).netloc.split(":")[0]
def _print_request(req: Request, level=0):
if level <= 0:
return
if level > 0:
print(f"{req.method} {req.full_url}")
if level > 1:
for k, v in req.headers.items():
print(f"{k}: {v}")
if req.data:
print(json.dumps(json.loads(req.data.decode("utf8")), indent=2))
def _cache_file() -> str:
return path.join(path.expanduser('~'), '.rai', 'tokens.json')
# Read oauth cache
def _read_cache() -> dict:
filename = _cache_file()
if not path.exists(filename):
return {}
try:
with open(filename, 'r') as cache:
return json.loads(cache.read())
except Exception as e:
logger.error(f"can't read token cache {filename}: {e}")
return {}
# Read access token from cache
def _read_token_cache(creds: ClientCredentials) -> AccessToken:
try:
cache = _read_cache()
return AccessToken(**cache[creds.client_id])
except Exception:
return None
# write access token to cache
def _write_token_cache(creds: ClientCredentials):
try:
cache_dir = path.dirname(_cache_file())
makedirs(cache_dir, exist_ok=True)
cache = _read_cache()
cache[creds.client_id] = creds.access_token
with open(_cache_file(), 'w') as f:
f.write(json.dumps(cache, default=vars))
except Exception as e:
logger.warning(f'Failed to write to token cache {_cache_file()}: {e}')
# Returns the current access token if valid, otherwise requests new token.
def _get_access_token(ctx: Context, url: str) -> AccessToken:
creds = ctx.credentials
assert isinstance(creds, ClientCredentials)
if creds.access_token is None or creds.access_token.is_expired():
creds.access_token = _read_token_cache(creds)
if creds.access_token is None or creds.access_token.is_expired():
creds.access_token = _request_access_token(ctx, url)
_write_token_cache(creds)
return creds.access_token.access_token
def _log_request_response(req, rsp):
content_type = req.headers["Content-Type"] if "Content-Type" in req.headers else ""
agent = req.headers["User-Agent"] if "User-Agent" in req.headers else ""
request_id = rsp.headers["X-Request-ID"] if "X-Request-ID" in rsp.headers else ""
logger.debug(f"{rsp._method} HTTP/{rsp.version} {content_type} {rsp.url} {rsp.status} {agent} {request_id}")
def _request_access_token(ctx: Context, url: str) -> AccessToken:
creds = ctx.credentials
assert isinstance(creds, ClientCredentials)
# ensure the audience contains the protocol scheme
audience = ctx.audience or f"https://{_get_host(url)}"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Host": _get_host(creds.client_credentials_url),
"User-Agent": _default_user_agent(),
}
body = {
CLIENT_ID_KEY: creds.client_id,
CLIENT_SECRET_KEY: creds.client_secret,
AUDIENCE_KEY: audience,
GRANT_TYPE_KEY: CLIENT_CREDENTIALS_KEY,
}
data = _encode(body)
req = Request(
method="POST",
url=creds.client_credentials_url,
headers=headers,
data=data,
)
_print_request(req)
with _urlopen_with_retry(req, ctx.retries) as rsp:
_log_request_response(req, rsp)
result = json.loads(rsp.read())
token = result.get(ACCESS_KEY_TOKEN_KEY, None)
if token is not None:
expires_in = result.get(EXPIRES_IN_KEY, None)
scope = result.get(SCOPE, None)
return AccessToken(token, scope, expires_in)
raise Exception("failed to get the access token")
# Authenticate the request by signing or adding access token.
def _authenticate(ctx: Context, req: Request) -> Request:
creds = ctx.credentials
if creds is None:
return req
if isinstance(creds, ClientCredentials):
access_token = _get_access_token(ctx, req.full_url)
req.headers["authorization"] = f"Bearer {access_token}"
return req
raise Exception("unknown credential type")
# Issues an HTTP request and retries if failed due to URLError.
def _urlopen_with_retry(req: Request, retries: int = 0):
if retries < 0:
raise ValueError("Retries must be a non-negative integer")
attempts = retries + 1
for attempt in range(attempts):
try:
return urlopen(req)
except (URLError, ConnectionError) as e:
logger.warning(f"URL/Connection error occured {req.full_url} (attempt {attempt + 1}/{attempts}). Error message: {str(e)}")
if attempt == attempts - 1:
logger.error(f"Failed to connect to {req.full_url} after {attempts} attempt{'s' if attempts > 1 else ''}")
raise e
# Issues an RAI REST API request, and returns response contents if successful.
def request(ctx: Context, method: str, url: str, headers={}, data=None, **kwargs):
headers = _default_headers(url, headers)
if kwargs:
url = f"{url}?{_encode_qs(kwargs)}"
data = _encode(data)
req = Request(method=method, url=url, headers=headers, data=data)
req = _authenticate(ctx, req)
_print_request(req)
rsp = _urlopen_with_retry(req, ctx.retries)
_log_request_response(req, rsp)
return rsp
def delete(ctx: Context, url: str, data, headers={}, **kwargs):
return request(ctx, "DELETE", url, headers=headers, data=data, **kwargs)
def get(ctx: Context, url: str, headers={}, **kwargs):
return request(ctx, "GET", url, headers=headers, **kwargs)
def put(ctx: Context, url: str, data, headers={}, **kwargs):
return request(ctx, "PUT", url, headers=headers, data=data, **kwargs)
def post(ctx: Context, url: str, data, headers={}, **kwargs):
return request(ctx, "POST", url, headers=headers, data=data, **kwargs)
def patch(ctx: Context, url: str, data, headers={}, **kwargs):
return request(ctx, "PATCH", url, headers=headers, data=data, **kwargs)