Spaces:
Running
Running
Commit
·
f119b25
1
Parent(s):
e3d7bd1
Implement universal API client with dynamic parameters. Currently may have robustness issues, but the main concept of plugging in an api and getting it to call works
Browse files- .gitignore +3 -1
- apiCall.py +50 -0
- main.py +125 -20
- requirements.txt +0 -0
.gitignore
CHANGED
@@ -191,4 +191,6 @@ cython_debug/
|
|
191 |
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
192 |
# refer to https://docs.cursor.com/context/ignore-files
|
193 |
.cursorignore
|
194 |
-
.cursorindexingignore
|
|
|
|
|
|
191 |
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
192 |
# refer to https://docs.cursor.com/context/ignore-files
|
193 |
.cursorignore
|
194 |
+
.cursorindexingignore
|
195 |
+
|
196 |
+
description.md
|
apiCall.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
|
5 |
+
class APIClient:
|
6 |
+
def __init__(self, base_url="https://v2.xivapi.com/api", auth_token=None):
|
7 |
+
self.base_url = base_url if base_url else "https://v2.xivapi.com/api"
|
8 |
+
self.auth_token = auth_token
|
9 |
+
|
10 |
+
def make_request(
|
11 |
+
self, endpoint=None, params=None, method="GET", data=None, json_data=None
|
12 |
+
):
|
13 |
+
"""
|
14 |
+
Make an API request with flexible parameters.
|
15 |
+
"""
|
16 |
+
url = f"{self.base_url}/{endpoint}" if endpoint else self.base_url
|
17 |
+
|
18 |
+
# Remove trailing slash if endpoint is empty
|
19 |
+
if endpoint == "":
|
20 |
+
url = self.base_url.rstrip("/")
|
21 |
+
|
22 |
+
headers = {}
|
23 |
+
if self.auth_token:
|
24 |
+
headers["Authorization"] = f"Bearer {self.auth_token}"
|
25 |
+
|
26 |
+
kwargs = {"headers": headers}
|
27 |
+
|
28 |
+
if params:
|
29 |
+
kwargs["params"] = params
|
30 |
+
if data:
|
31 |
+
kwargs["data"] = data
|
32 |
+
if json_data:
|
33 |
+
kwargs["json"] = json_data
|
34 |
+
|
35 |
+
try:
|
36 |
+
print(f"Making {method} request to {url}")
|
37 |
+
print(f"Parameters: {params}")
|
38 |
+
|
39 |
+
response = requests.request(method, url, **kwargs)
|
40 |
+
|
41 |
+
# Try to parse response as JSON
|
42 |
+
if response.status_code == 200:
|
43 |
+
try:
|
44 |
+
return response.json()
|
45 |
+
except json.JSONDecodeError:
|
46 |
+
return response.text
|
47 |
+
|
48 |
+
return f"Error {response.status_code}: {response.text}"
|
49 |
+
except requests.exceptions.RequestException as e:
|
50 |
+
return f"Request Error: {str(e)}"
|
main.py
CHANGED
@@ -1,29 +1,134 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
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 |
if __name__ == "__main__":
|
28 |
-
|
29 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import apiCall
|
3 |
+
import json
|
4 |
|
5 |
+
|
6 |
+
def api_call(
|
7 |
+
base_url=None,
|
8 |
+
auth_token=None,
|
9 |
+
endpoint=None,
|
10 |
+
param_keys_values=None,
|
11 |
+
additional_params=None,
|
12 |
+
method="GET",
|
13 |
+
):
|
14 |
"""
|
15 |
+
Make an API call to fetch data with dynamic parameters.
|
16 |
|
17 |
+
Parameters:
|
18 |
+
- base_url: The base URL of the API (e.g., "https://api.example.com")
|
19 |
+
- auth_token: Optional authentication token for APIs requiring authorization
|
20 |
+
- endpoint: The specific API endpoint to call (e.g., "search", "users/profile")
|
21 |
+
- param_keys_values: String containing parameter key-value pairs, one per line in format "key: value"
|
22 |
+
- additional_params: Optional JSON string for complex parameters
|
23 |
+
- method: HTTP method to use (GET, POST, PUT, DELETE)
|
24 |
|
25 |
+
Instructions:
|
26 |
+
- Format param_keys_values as a multi-line string with each parameter on a new line
|
27 |
+
- For numeric values, simply use numbers without quotes
|
28 |
+
- For boolean values, use "true" or "false" (lowercase)
|
29 |
+
- For string values, just provide the string without additional quotes
|
30 |
"""
|
31 |
+
# Build params dictionary from key-value pairs
|
32 |
+
params = {}
|
33 |
+
|
34 |
+
# Process param_keys_values
|
35 |
+
if param_keys_values:
|
36 |
+
lines = param_keys_values.strip().split("\n")
|
37 |
+
for line in lines:
|
38 |
+
if ":" in line:
|
39 |
+
key, value = line.split(":", 1)
|
40 |
+
key = key.strip()
|
41 |
+
value = value.strip()
|
42 |
+
|
43 |
+
if key: # Only add non-empty keys
|
44 |
+
# Try to parse numeric values
|
45 |
+
if value.isdigit():
|
46 |
+
params[key] = int(value)
|
47 |
+
elif value.lower() == "true":
|
48 |
+
params[key] = True
|
49 |
+
elif value.lower() == "false":
|
50 |
+
params[key] = False
|
51 |
+
else:
|
52 |
+
params[key] = value
|
53 |
+
|
54 |
+
# Handle additional parameters
|
55 |
+
if additional_params and additional_params.strip():
|
56 |
+
try:
|
57 |
+
# Parse additional JSON parameters
|
58 |
+
extra_params = json.loads(additional_params)
|
59 |
+
if isinstance(extra_params, dict):
|
60 |
+
params.update(extra_params)
|
61 |
+
else:
|
62 |
+
return "Error: Additional parameters must be a valid JSON object"
|
63 |
+
except json.JSONDecodeError as e:
|
64 |
+
return f"Error parsing additional parameters: {str(e)}"
|
65 |
+
|
66 |
+
try:
|
67 |
+
client = apiCall.APIClient(base_url, auth_token)
|
68 |
+
result = client.make_request(
|
69 |
+
endpoint=endpoint,
|
70 |
+
params=params,
|
71 |
+
method=method,
|
72 |
+
)
|
73 |
+
return result
|
74 |
+
except Exception as e:
|
75 |
+
return f"Error making API call: {str(e)}"
|
76 |
+
|
77 |
+
|
78 |
+
app = gr.Interface(
|
79 |
+
fn=api_call,
|
80 |
+
inputs=[
|
81 |
+
gr.Textbox(
|
82 |
+
label="Base URL",
|
83 |
+
placeholder="Enter base URL",
|
84 |
+
value="https://v2.xivapi.com/api",
|
85 |
+
),
|
86 |
+
gr.Textbox(label="Auth Token", placeholder="Enter auth token (optional)"),
|
87 |
+
gr.Textbox(label="Endpoint", placeholder="Enter endpoint", value="search"),
|
88 |
+
gr.Textbox(
|
89 |
+
label="Parameter Key-Value Pairs",
|
90 |
+
placeholder="Enter one parameter per line in format 'key: value'",
|
91 |
+
value='query: Name~"popoto"\nsheets: Item\nfields: Name,Description\nlanguage: en\nlimit: 1',
|
92 |
+
lines=5,
|
93 |
+
),
|
94 |
+
gr.Textbox(
|
95 |
+
label="Additional Parameters (JSON)",
|
96 |
+
placeholder="Enter any additional parameters as JSON",
|
97 |
+
value="{}",
|
98 |
+
lines=3,
|
99 |
+
),
|
100 |
+
gr.Radio(choices=["GET", "POST", "PUT", "DELETE"], label="Method", value="GET"),
|
101 |
+
],
|
102 |
+
outputs=gr.Textbox(label="API Response", lines=10),
|
103 |
+
title="Universal API Client",
|
104 |
+
description="Make API calls to any endpoint with custom parameters. \n\n"
|
105 |
+
+ "- **Base URL**: Enter the full API base URL (e.g., 'https://api.example.com') \n"
|
106 |
+
+ "- **Auth Token**: Provide if the API requires authentication \n"
|
107 |
+
+ "- **Endpoint**: The specific endpoint to call (without leading slash) \n"
|
108 |
+
+ "- **Parameter Key-Value Pairs**: Format as 'key: value' with each pair on a new line \n"
|
109 |
+
+ " Example: \n```\nquery: search term\nlimit: 10\nfilter: active\n``` \n"
|
110 |
+
+ "- **Additional Parameters**: Use valid JSON format for nested or complex parameters \n"
|
111 |
+
+ "- **Method**: Choose the appropriate HTTP method for your request",
|
112 |
+
examples=[
|
113 |
+
[
|
114 |
+
"https://v2.xivapi.com/api",
|
115 |
+
"",
|
116 |
+
"search",
|
117 |
+
'query: Name~"popoto"\nsheets: Item\nfields: Name,Description\nlanguage: en\nlimit: 1',
|
118 |
+
"{}",
|
119 |
+
"GET",
|
120 |
+
],
|
121 |
+
[
|
122 |
+
"https://api.github.com",
|
123 |
+
"",
|
124 |
+
"repos/microsoft/TypeScript/issues",
|
125 |
+
"state: open\nper_page: 5",
|
126 |
+
"{}",
|
127 |
+
"GET",
|
128 |
+
],
|
129 |
+
],
|
130 |
+
allow_flagging="never",
|
131 |
)
|
132 |
|
133 |
if __name__ == "__main__":
|
134 |
+
app.launch(mcp_server=True)
|
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|