-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrpc.py
184 lines (155 loc) · 5.76 KB
/
rpc.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
import json
from typing import List
import requests
from requests.adapters import HTTPAdapter, Retry
from .errors.api_error import ApiError
from .errors.server_error import ServerError
from .models.compass.cancel_query_run import (
CancelQueryRunRpcRequest,
CancelQueryRunRpcRequestParams,
CancelQueryRunRpcResponse,
)
from .models.compass.create_query_run import (
CreateQueryRunRpcParams,
CreateQueryRunRpcRequest,
CreateQueryRunRpcResponse,
)
from .models.compass.get_query_run import (
GetQueryRunRpcRequest,
GetQueryRunRpcRequestParams,
GetQueryRunRpcResponse,
)
from .models.compass.get_query_run_results import (
GetQueryRunResultsRpcParams,
GetQueryRunResultsRpcRequest,
GetQueryRunResultsRpcResponse,
)
from .models.compass.get_sql_statement import (
GetSqlStatementParams,
GetSqlStatementRequest,
GetSqlStatementResponse,
)
class RPC(object):
def __init__(
self,
base_url: str,
api_key: str,
max_retries: int = 10,
backoff_factor: float = 1,
status_forcelist: List[int] = [429, 500, 502, 503, 504],
method_allowlist: List[str] = [
"HEAD",
"GET",
"PUT",
"POST",
"DELETE",
"OPTIONS",
"TRACE",
],
):
self._base_url = base_url
self._api_key = api_key
# Session Settings
self._MAX_RETRIES = max_retries
self._BACKOFF_FACTOR = backoff_factor
self._STATUS_FORCE_LIST = status_forcelist
self._METHOD_ALLOWLIST = method_allowlist
def create_query(
self, params: CreateQueryRunRpcParams
) -> CreateQueryRunRpcResponse:
with self.session.post(
self.url,
data=json.dumps(CreateQueryRunRpcRequest(params=[params]).dict()),
headers=self._headers,
) as result:
data = self._handle_response(result, "createQueryRun")
create_query_resp = CreateQueryRunRpcResponse(**data)
return create_query_resp
def get_query_run(
self, params: GetQueryRunRpcRequestParams
) -> GetQueryRunRpcResponse:
with self.session.post(
self.url,
data=json.dumps(GetQueryRunRpcRequest(params=[params]).dict()),
headers=self._headers,
) as result:
data = self._handle_response(result, "getQueryRun")
get_query_run_resp = GetQueryRunRpcResponse(**data)
return get_query_run_resp
def get_sql_statement(
self, params: GetSqlStatementParams
) -> GetSqlStatementResponse:
with self.session.post(
self.url,
data=json.dumps(GetSqlStatementRequest(params=[params]).dict()),
headers=self._headers,
) as result:
data = self._handle_response(result, "getSqlStatement")
get_sql_statement_resp = GetSqlStatementResponse(**data)
return get_sql_statement_resp
def cancel_query_run(
self, params: CancelQueryRunRpcRequestParams
) -> CancelQueryRunRpcResponse:
with self.session.post(
self.url,
data=json.dumps(CancelQueryRunRpcRequest(params=[params]).dict()),
headers=self._headers,
) as result:
data = self._handle_response(result, "cancelQueryRun")
cancel_query_run_resp = CancelQueryRunRpcResponse(**data)
return cancel_query_run_resp
def get_query_result(
self, params: GetQueryRunResultsRpcParams
) -> GetQueryRunResultsRpcResponse:
with self.session.post(
self.url,
data=json.dumps(GetQueryRunResultsRpcRequest(params=[params]).dict()),
headers=self._headers,
) as result:
data = self._handle_response(result, "getQueryRunResults")
get_query_run_results_resp = GetQueryRunResultsRpcResponse(**data)
return get_query_run_results_resp
def _handle_response(self, result: requests.Response, method: str) -> dict:
if result.status_code is None:
raise ServerError(
status_code=0,
message=f"Unable to connect to server when calling `{method}`. Please try again later.",
)
if result.status_code >= 500:
raise ServerError(
status_code=result.status_code,
message=f"Unknown server error when calling `{method}`: {result.status_code} - {result.reason}. Please try again later.",
)
if result.status_code == 401 or result.status_code == 403:
raise ApiError("Unauthorized", result.status_code, "Invalid API Key.")
try:
data = result.json()
except json.decoder.JSONDecodeError:
raise ServerError(
status_code=result.status_code,
message=f"Unable to parse response for RPC response from `{method}`: {result.status_code} - {result.reason}. Please try again later.",
)
return data
@property
def _headers(self) -> dict:
return {
"Accept": "application/json",
"Content-Type": "application/json",
"x-api-key": self._api_key,
}
@property
def url(self) -> str:
return f"{self._base_url}/json-rpc"
@property
def session(self) -> requests.Session:
retry_strategy = Retry(
total=self._MAX_RETRIES,
backoff_factor=self._BACKOFF_FACTOR, # type: ignore
status_forcelist=self._STATUS_FORCE_LIST,
allowed_methods=self._METHOD_ALLOWLIST,
)
adapter = HTTPAdapter(max_retries=retry_strategy) # type: ignore
session = requests.Session()
session.mount("https://", adapter)
session.mount("http://", adapter)
return session