This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
tool.py
257 lines (233 loc) · 9.38 KB
/
tool.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
from textwrap import dedent
from typing import Optional
from urllib.parse import urljoin
import schema
from schema import Schema, Literal
from attr import define, field
from griptape.core import BaseTool
from griptape.core.decorators import activity
from griptape.artifacts import BaseArtifact, TextArtifact, ErrorArtifact
@define
class RestApiClient(BaseTool):
base_url: str = field(kw_only=True)
path: Optional[str] = field(
default=None,
kw_only=True,
)
description: str = field(kw_only=True)
request_path_params_schema: Optional[str] = field(
default=None,
kw_only=True,
)
request_query_params_schema: Optional[str] = field(
default=None,
kw_only=True,
)
request_body_schema: Optional[str] = field(
default=None,
kw_only=True,
)
response_body_schema: Optional[str] = field(
default=None,
kw_only=True,
)
@property
def schema_template_args(self) -> dict:
return {
"base_url": self.base_url,
"path": self.path,
"full_url": self._build_url(self.base_url, path=self.path),
"description": self.description,
"request_body_schema": self.request_body_schema,
"request_query_params_schema": self.request_query_params_schema,
"request_path_params_schema": self.request_path_params_schema,
"response_body_schema": self.response_body_schema,
}
@activity(
config={
"description": dedent(
"""
This tool can be used to make a put request to the rest api url: {{full_url}}
This rest api has the following description: {{description}}
{% if request_body_schema %}The request body must follow this JSON schema: {{request_body_schema}}{% endif %}
{% if response_body_schema %}The response body must follow this JSON schema: {{response_body_schema}}{% endif %}
"""
),
"schema": Schema(
{
Literal("body", description="The request body."): dict,
}
),
}
)
def put(self, params: dict) -> BaseArtifact:
from requests import put, exceptions
values = params["values"]
base_url = self.base_url
path = self.path
body = values["body"]
url = self._build_url(base_url, path=path)
try:
response = put(url, data=body, timeout=30)
return TextArtifact(response.text)
except exceptions.RequestException as err:
return ErrorArtifact(str(err))
@activity(
config={
"description": dedent(
"""
This tool can be used to make a patch request to the rest api url: {{full_url}}
This rest api has the following description: {{description}}
{% if request_path_parameters %}The request path parameters must follow this JSON schema: {{request_path_params_schema}}{% endif %}
{% if request_body_schema %}The request body must follow this JSON schema: {{request_body_schema}}{% endif %}
{% if response_body_schema %}The response body must follow this JSON schema: {{response_body_schema}}{% endif %}
"""
),
"schema": Schema(
{
Literal(
"path_params", description="The request path parameters."
): list,
Literal("body", description="The request body."): dict,
}
),
}
)
def patch(self, params: dict) -> BaseArtifact:
from requests import patch, exceptions
values = params["values"]
base_url = self.base_url
path = self.path
body = values["body"]
path_params = values["path_params"]
url = self._build_url(base_url, path=path, path_params=path_params)
try:
response = patch(url, data=body, timeout=30)
return TextArtifact(response.text)
except exceptions.RequestException as err:
return ErrorArtifact(str(err))
@activity(
config={
"description": dedent(
"""
This tool can be used to make a post request to the rest api url: {{full_url}}
This rest api has the following description: {{description}}
{% if request_body_schema %}The request body must follow this JSON schema: {{request_body_schema}}{% endif %}
{% if response_body_schema %}The response body must follow this JSON schema: {{response_body_schema}}{% endif %}
"""
),
"schema": Schema(
{
Literal("body", description="The request body."): dict,
}
),
}
)
def post(self, params: dict) -> BaseArtifact:
from requests import post, exceptions
values = params["values"]
base_url = self.base_url
path = self.path
url = self._build_url(base_url, path=path)
body = values["body"]
try:
response = post(url, data=body, timeout=30)
return TextArtifact(response.text)
except exceptions.RequestException as err:
return ErrorArtifact(str(err))
@activity(
config={
"description": dedent(
"""
This tool can be used to make a get request to the rest api url: {{full_url}}
This rest api has the following description: {{description}}
{% if request_path_parameters %}The request path parameters must follow this JSON schema: {{request_path_params_schema}}{% endif %}
{% if request_query_parameters %}The request query parameters must follow this JSON schema: {{request_path_params_schema}}{% endif %}
{% if response_body_schema %}The response body must follow this JSON schema: {{response_body_schema}}{% endif %}
"""
),
"schema": schema.Optional(
Schema(
{
schema.Optional(
Literal(
"query_params",
description="The request query parameters.",
)
): dict,
schema.Optional(
Literal(
"path_params",
description="The request path parameters.",
)
): list,
}
)
),
}
)
def get(self, params: dict) -> BaseArtifact:
from requests import get, exceptions
values = params["values"]
base_url = self.base_url
path = self.path
query_params = {}
path_params = []
if values:
query_params = values.get("query_params", {})
path_params = values.get("path_params", [])
url = self._build_url(base_url, path=path, path_params=path_params)
try:
response = get(url, params=query_params, timeout=30)
return TextArtifact(response.text)
except exceptions.RequestException as err:
return ErrorArtifact(str(err))
@activity(
config={
"description": dedent(
"""
This tool can be used to make a delete request to the rest api url: {{full_url}}
This rest api has the following description: {{description}}
{% if request_path_parameters %}The request path parameters must follow this JSON schema: {{request_path_params_schema}}{% endif %}
{% if request_query_parameters %}The request query parameters must follow this JSON schema: {{request_path_params_schema}}{% endif %}
"""
),
"schema": Schema(
{
schema.Optional(
Literal(
"query_params",
description="The request query parameters.",
)
): dict,
schema.Optional(
Literal(
"path_params", description="The request path parameters."
)
): list,
}
),
}
)
def delete(self, params: dict = None) -> BaseArtifact:
from requests import delete, exceptions
values = params["values"]
base_url = self.base_url
path = self.path
query_params = values.get("query_params", {})
path_params = values.get("path_params", [])
url = self._build_url(base_url, path=path, path_params=path_params)
try:
response = delete(url, params=query_params, timeout=30)
return TextArtifact(response.text)
except exceptions.RequestException as err:
return ErrorArtifact(str(err))
def _build_url(self, base_url, path=None, path_params=None):
base = base_url.strip("/")
url = ""
if path:
url += path.strip("/")
if path_params:
url += f'/{str.join("/", map(str, path_params))}'
full_url = urljoin(base, url)
return full_url