Spaces:
Runtime error
Runtime error
Pedro Bento
commited on
Commit
·
ea2d63e
1
Parent(s):
0b89350
Fixed errors
Browse files- tdagent/tools/get_url_content.py +39 -21
tdagent/tools/get_url_content.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from collections.abc import Sequence
|
2 |
from typing import Literal, Optional, Dict, Any
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
import requests
|
@@ -9,37 +10,41 @@ from tdagent.constants import HttpContentType
|
|
9 |
# Define valid HTTP methods
|
10 |
HttpMethod = Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"]
|
11 |
|
12 |
-
|
13 |
def make_http_request(
|
14 |
url: str,
|
15 |
method: HttpMethod = "GET",
|
16 |
-
content_type:
|
17 |
-
body:
|
18 |
-
timeout:
|
19 |
-
custom_headers:
|
20 |
) -> tuple[str, str]:
|
21 |
"""Make an HTTP request to a URL with specified method and parameters.
|
22 |
|
23 |
Args:
|
24 |
url: The URL to make the request to.
|
25 |
method: HTTP method to use (GET, POST, PUT, DELETE, PATCH, HEAD).
|
26 |
-
content_type:
|
27 |
-
content types in the response body.
|
28 |
body: Request body for methods that support it (POST, PUT, PATCH).
|
29 |
timeout: Request timeout in seconds. Defaults to 30.
|
30 |
-
custom_headers:
|
31 |
|
32 |
Returns:
|
33 |
-
A pair of strings (content, error_message).
|
34 |
-
error making the request, the `content` will be empty and
|
35 |
-
`error_message` will contain the error cause. Otherwise,
|
36 |
-
`error_message` will be empty and the content will be filled
|
37 |
-
with data fetched from the URL.
|
38 |
"""
|
39 |
-
|
|
|
40 |
|
|
|
41 |
if content_type:
|
42 |
-
headers["Accept"] =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Prepare request parameters
|
45 |
request_params: Dict[str, Any] = {
|
@@ -70,7 +75,6 @@ def make_http_request(
|
|
70 |
|
71 |
return response.text, ""
|
72 |
|
73 |
-
|
74 |
# Create the Gradio interface
|
75 |
gr_make_http_request = gr.Interface(
|
76 |
fn=make_http_request,
|
@@ -81,9 +85,21 @@ gr_make_http_request = gr.Interface(
|
|
81 |
label="HTTP Method",
|
82 |
value="GET"
|
83 |
),
|
84 |
-
gr.Textbox(
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
gr.Textbox(
|
88 |
label="Custom Headers (JSON format)",
|
89 |
placeholder='{"Authorization": "Bearer token"}'
|
@@ -100,7 +116,9 @@ gr_make_http_request = gr.Interface(
|
|
100 |
),
|
101 |
examples=[
|
102 |
["https://google.com", "GET", "text/html", "", 30, ""],
|
103 |
-
["https://api.example.com/data", "POST", "application/json", '{"key": "value"}', 30,
|
104 |
-
'{"Authorization": "Bearer token"}'],
|
105 |
],
|
106 |
)
|
|
|
|
|
|
|
|
1 |
from collections.abc import Sequence
|
2 |
from typing import Literal, Optional, Dict, Any
|
3 |
+
import json
|
4 |
|
5 |
import gradio as gr
|
6 |
import requests
|
|
|
10 |
# Define valid HTTP methods
|
11 |
HttpMethod = Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"]
|
12 |
|
|
|
13 |
def make_http_request(
|
14 |
url: str,
|
15 |
method: HttpMethod = "GET",
|
16 |
+
content_type: str = "",
|
17 |
+
body: str = "",
|
18 |
+
timeout: float = 30,
|
19 |
+
custom_headers: str = ""
|
20 |
) -> tuple[str, str]:
|
21 |
"""Make an HTTP request to a URL with specified method and parameters.
|
22 |
|
23 |
Args:
|
24 |
url: The URL to make the request to.
|
25 |
method: HTTP method to use (GET, POST, PUT, DELETE, PATCH, HEAD).
|
26 |
+
content_type: Comma-separated string of content types.
|
|
|
27 |
body: Request body for methods that support it (POST, PUT, PATCH).
|
28 |
timeout: Request timeout in seconds. Defaults to 30.
|
29 |
+
custom_headers: JSON string of additional headers.
|
30 |
|
31 |
Returns:
|
32 |
+
A pair of strings (content, error_message).
|
|
|
|
|
|
|
|
|
33 |
"""
|
34 |
+
# Initialize headers dictionary
|
35 |
+
headers = {}
|
36 |
|
37 |
+
# Parse content type
|
38 |
if content_type:
|
39 |
+
headers["Accept"] = content_type
|
40 |
+
|
41 |
+
# Parse custom headers
|
42 |
+
if custom_headers:
|
43 |
+
try:
|
44 |
+
custom_headers_dict = json.loads(custom_headers)
|
45 |
+
headers.update(custom_headers_dict)
|
46 |
+
except json.JSONDecodeError:
|
47 |
+
return "", "Invalid JSON format in custom headers"
|
48 |
|
49 |
# Prepare request parameters
|
50 |
request_params: Dict[str, Any] = {
|
|
|
75 |
|
76 |
return response.text, ""
|
77 |
|
|
|
78 |
# Create the Gradio interface
|
79 |
gr_make_http_request = gr.Interface(
|
80 |
fn=make_http_request,
|
|
|
85 |
label="HTTP Method",
|
86 |
value="GET"
|
87 |
),
|
88 |
+
gr.Textbox(
|
89 |
+
label="Content Type",
|
90 |
+
placeholder="text/html,application/json"
|
91 |
+
),
|
92 |
+
gr.Textbox(
|
93 |
+
label="Request Body (for POST/PUT/PATCH)",
|
94 |
+
lines=3,
|
95 |
+
placeholder='{"key": "value"}'
|
96 |
+
),
|
97 |
+
gr.Number(
|
98 |
+
label="Timeout (seconds)",
|
99 |
+
value=30,
|
100 |
+
minimum=1,
|
101 |
+
maximum=300
|
102 |
+
),
|
103 |
gr.Textbox(
|
104 |
label="Custom Headers (JSON format)",
|
105 |
placeholder='{"Authorization": "Bearer token"}'
|
|
|
116 |
),
|
117 |
examples=[
|
118 |
["https://google.com", "GET", "text/html", "", 30, ""],
|
119 |
+
["https://api.example.com/data", "POST", "application/json", '{"key": "value"}', 30, '{"Authorization": "Bearer token"}'],
|
|
|
120 |
],
|
121 |
)
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
gr_make_http_request.launch()
|