-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbase.py
More file actions
101 lines (78 loc) · 2.88 KB
/
base.py
File metadata and controls
101 lines (78 loc) · 2.88 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
import json
import requests
from ....src.util.errors import raise_custom_error
class SQL():
def __init__(self, base_class) -> None:
self.super = base_class
# Performs the given SQL query
# https://docs.transpose.io/sql/parameters/
def query(self,
sql_query: str,
parameters: dict={}) -> dict:
# build headers
request_headers = {
'x-api-key': self.super.api_key,
'Accept': 'application/json',
}
# build body
body = {
'sql': sql_query,
'parameters': parameters
}
# if in verbose mode, log the endpoint
print("\n{}\n {}\n".format("https://api.transpose.io/sql", json.dumps(body, indent=4))) if self.super.verbose else None
request = requests.post(
"https://api.transpose.io/sql",
headers=request_headers,
json=body
)
# check for a successful response
if request.status_code == 200:
response = request.json()
return response
else:
raise_custom_error(request.status_code, request.json()['message'])
# Gets the schema from the Transpose API
def schema(self) -> dict:
# build headers
request_headers = {
'x-api-key': self.super.api_key,
'Accept': 'application/json',
}
# if in verbose mode, log the endpoint
request = requests.get(
"https://api.transpose.io/get-schema",
headers=request_headers,
)
# check for a successful response
if request.status_code == 200:
response = request.json()
return response
else:
raise_custom_error(request.status_code, request.json()['message'])
# Calls the AI query assistant
def generate_query(self, text: str, chain: str='ethereum') -> dict:
# build headers
request_headers = {
'x-api-key': self.super.api_key,
'Accept': 'application/json',
}
# build body
body = {
'text': text,
'chain': chain
}
# if in verbose mode, log the endpoint
print("\n{}\n {}\n".format("https://api.transpose.io/text-to-sql", json.dumps(body, indent=4))) if self.super.verbose else None
# make request
request = requests.post(
"https://api.transpose.io/text-to-sql",
headers=request_headers,
json=body,
)
# check for a successful response
if request.status_code == 200:
response = request.json()
return response
else:
raise_custom_error(request.status_code, request.json()['message'])