Spaces:
Running
Running
Commit
·
108dc63
1
Parent(s):
e6a445e
Renamed everything for consistency
Browse files- apiCall.py → api_client.py +1 -1
- api_validator.py → api_monitor.py +16 -42
- main.py +4 -4
apiCall.py → api_client.py
RENAMED
@@ -38,7 +38,7 @@ def parse_key_value_string(key_value_string):
|
|
38 |
return result
|
39 |
|
40 |
|
41 |
-
def
|
42 |
method="GET",
|
43 |
base_url=None,
|
44 |
endpoint=None,
|
|
|
38 |
return result
|
39 |
|
40 |
|
41 |
+
def call_api(
|
42 |
method="GET",
|
43 |
base_url=None,
|
44 |
endpoint=None,
|
api_validator.py → api_monitor.py
RENAMED
@@ -1,9 +1,6 @@
|
|
1 |
-
import
|
2 |
import json
|
3 |
from datetime import datetime, timedelta
|
4 |
-
import json
|
5 |
-
import hashlib
|
6 |
-
import apiCall
|
7 |
import hashlib
|
8 |
import psycopg2
|
9 |
import os
|
@@ -13,7 +10,7 @@ from dotenv import load_dotenv
|
|
13 |
load_dotenv()
|
14 |
|
15 |
|
16 |
-
def
|
17 |
mcp_api_key,
|
18 |
name,
|
19 |
description,
|
@@ -31,11 +28,10 @@ def validate_api_call(
|
|
31 |
TOOL: Validate and store API configuration for monitoring.
|
32 |
|
33 |
PURPOSE: Test an API endpoint and store the configuration if successful. This is STEP 1
|
34 |
-
of the monitoring setup process. If validation fails, retry with corrected parameters.
|
35 |
-
If successful, use the returned config_id in setup_scheduler() function.
|
36 |
|
37 |
⚠️ CRITICAL: Even if success=True, you MUST manually check the 'sample_response' field
|
38 |
-
before proceeding to
|
39 |
error messages (like "401 Unauthorized", "Invalid API key", etc.) in the sample_response.
|
40 |
|
41 |
WORKFLOW:
|
@@ -43,7 +39,7 @@ def validate_api_call(
|
|
43 |
2. If success=False: Fix parameters and retry this function
|
44 |
3. If success=True: MANUALLY INSPECT the 'sample_response' field for errors
|
45 |
4. If sample_response contains error messages: Fix API parameters and retry validation
|
46 |
-
5. If sample_response looks valid: Use config_id in
|
47 |
|
48 |
Parameters:
|
49 |
- mcp_api_key: MCP API key serves as user identifier
|
@@ -89,20 +85,6 @@ def validate_api_call(
|
|
89 |
stop_after_hours: 48
|
90 |
time_to_start: "2024-06-15 09:00:00"
|
91 |
|
92 |
-
3. GitHub API monitoring:
|
93 |
-
mcp_api_key: "your_mcp_key_here"
|
94 |
-
name: "Repo Issues Monitor"
|
95 |
-
description: "Monitor new issues in repository"
|
96 |
-
method: "GET"
|
97 |
-
base_url: "https://api.github.com"
|
98 |
-
endpoint: "repos/microsoft/TypeScript/issues"
|
99 |
-
param_keys_values: "state: open\nper_page: 10"
|
100 |
-
header_keys_values: "Accept: application/vnd.github.v3+json\nUser-Agent: MyApp"
|
101 |
-
additional_params: "{}"
|
102 |
-
schedule_interval_minutes: 60
|
103 |
-
stop_after_hours: 168
|
104 |
-
time_to_start: ""
|
105 |
-
|
106 |
Returns:
|
107 |
- Dictionary with success status, config_id (needed for setup_scheduler), message, and sample_response
|
108 |
|
@@ -116,7 +98,7 @@ def validate_api_call(
|
|
116 |
"start_at": "2025-06-04T12:00:00Z"
|
117 |
}
|
118 |
|
119 |
-
NEXT STEP: If success=True, call
|
120 |
"""
|
121 |
try:
|
122 |
# Validate input parameters
|
@@ -189,10 +171,8 @@ def validate_api_call(
|
|
189 |
"config_id": None,
|
190 |
}
|
191 |
else:
|
192 |
-
parsed_start_time = datetime.now()
|
193 |
-
|
194 |
-
# Test the API call
|
195 |
-
result = apiCall.api_call(
|
196 |
method=method,
|
197 |
base_url=base_url,
|
198 |
endpoint=endpoint,
|
@@ -288,8 +268,8 @@ def validate_api_call(
|
|
288 |
method,
|
289 |
base_url,
|
290 |
endpoint,
|
291 |
-
json.dumps(
|
292 |
-
json.dumps(
|
293 |
additional_params,
|
294 |
False,
|
295 |
False,
|
@@ -314,7 +294,7 @@ def validate_api_call(
|
|
314 |
return {
|
315 |
"success": True,
|
316 |
"config_id": config_id,
|
317 |
-
"message": f"API call tested and stored successfully for '{name}'. Use this config_id in
|
318 |
"sample_response": (
|
319 |
json.loads(result)
|
320 |
if result.startswith("{") or result.startswith("[")
|
@@ -333,17 +313,15 @@ def validate_api_call(
|
|
333 |
}
|
334 |
|
335 |
|
336 |
-
def
|
337 |
"""
|
338 |
TOOL: Activate periodic monitoring for a validated API configuration.
|
339 |
|
340 |
PURPOSE: Start automated recurring API calls based on a previously validated configuration.
|
341 |
-
This is STEP 2 of the monitoring setup process.
|
342 |
-
|
343 |
-
PREREQUISITE: Must call validate_api_call() first and obtain a config_id from successful validation.
|
344 |
|
345 |
WORKFLOW:
|
346 |
-
1. First call
|
347 |
2. If validation successful, call this function with the config_id
|
348 |
3. Monitoring will run automatically according to the validated schedule
|
349 |
|
@@ -361,11 +339,7 @@ def setup_scheduler(config_id, mcp_api_key):
|
|
361 |
config_id: 987654321
|
362 |
mcp_api_key: "your_mcp_key_here"
|
363 |
|
364 |
-
|
365 |
-
config_id: 456789123
|
366 |
-
mcp_api_key: "your_mcp_key_here"
|
367 |
-
|
368 |
-
NOTE: The config_id must be obtained from a successful validate_api_call() response.
|
369 |
The mcp_api_key must match the one used during validation.
|
370 |
|
371 |
Returns:
|
@@ -394,7 +368,7 @@ def setup_scheduler(config_id, mcp_api_key):
|
|
394 |
## testing
|
395 |
if __name__ == "__main__":
|
396 |
# Example usage
|
397 |
-
response =
|
398 |
mcp_api_key="your_api_key",
|
399 |
name="Dog Facts API",
|
400 |
description="Monitor random dog facts from a free API",
|
|
|
1 |
+
import api_client
|
2 |
import json
|
3 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
4 |
import hashlib
|
5 |
import psycopg2
|
6 |
import os
|
|
|
10 |
load_dotenv()
|
11 |
|
12 |
|
13 |
+
def validate_api_configuration(
|
14 |
mcp_api_key,
|
15 |
name,
|
16 |
description,
|
|
|
28 |
TOOL: Validate and store API configuration for monitoring.
|
29 |
|
30 |
PURPOSE: Test an API endpoint and store the configuration if successful. This is STEP 1
|
31 |
+
of the monitoring setup process. If validation fails, retry with corrected parameters. If successful, use the returned config_id in activate_monitoring() function.
|
|
|
32 |
|
33 |
⚠️ CRITICAL: Even if success=True, you MUST manually check the 'sample_response' field
|
34 |
+
before proceeding to activate_monitoring(). The API call may return success=True but contain
|
35 |
error messages (like "401 Unauthorized", "Invalid API key", etc.) in the sample_response.
|
36 |
|
37 |
WORKFLOW:
|
|
|
39 |
2. If success=False: Fix parameters and retry this function
|
40 |
3. If success=True: MANUALLY INSPECT the 'sample_response' field for errors
|
41 |
4. If sample_response contains error messages: Fix API parameters and retry validation
|
42 |
+
5. If sample_response looks valid: Use config_id in activate_monitoring() to activate monitoring
|
43 |
|
44 |
Parameters:
|
45 |
- mcp_api_key: MCP API key serves as user identifier
|
|
|
85 |
stop_after_hours: 48
|
86 |
time_to_start: "2024-06-15 09:00:00"
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
Returns:
|
89 |
- Dictionary with success status, config_id (needed for setup_scheduler), message, and sample_response
|
90 |
|
|
|
98 |
"start_at": "2025-06-04T12:00:00Z"
|
99 |
}
|
100 |
|
101 |
+
NEXT STEP: If success=True, call activate_monitoring(config_id, mcp_api_key) to activate monitoring
|
102 |
"""
|
103 |
try:
|
104 |
# Validate input parameters
|
|
|
171 |
"config_id": None,
|
172 |
}
|
173 |
else:
|
174 |
+
parsed_start_time = datetime.now() # Test the API call
|
175 |
+
result = api_client.call_api(
|
|
|
|
|
176 |
method=method,
|
177 |
base_url=base_url,
|
178 |
endpoint=endpoint,
|
|
|
268 |
method,
|
269 |
base_url,
|
270 |
endpoint,
|
271 |
+
json.dumps(api_client.parse_key_value_string(param_keys_values)),
|
272 |
+
json.dumps(api_client.parse_key_value_string(header_keys_values)),
|
273 |
additional_params,
|
274 |
False,
|
275 |
False,
|
|
|
294 |
return {
|
295 |
"success": True,
|
296 |
"config_id": config_id,
|
297 |
+
"message": f"API call tested and stored successfully for '{name}'. Use this config_id in activate_monitoring() to activate monitoring.",
|
298 |
"sample_response": (
|
299 |
json.loads(result)
|
300 |
if result.startswith("{") or result.startswith("[")
|
|
|
313 |
}
|
314 |
|
315 |
|
316 |
+
def activate_monitoring(config_id, mcp_api_key):
|
317 |
"""
|
318 |
TOOL: Activate periodic monitoring for a validated API configuration.
|
319 |
|
320 |
PURPOSE: Start automated recurring API calls based on a previously validated configuration.
|
321 |
+
This is STEP 2 of the monitoring setup process. PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation.
|
|
|
|
|
322 |
|
323 |
WORKFLOW:
|
324 |
+
1. First call validate_api_configuration() to get config_id
|
325 |
2. If validation successful, call this function with the config_id
|
326 |
3. Monitoring will run automatically according to the validated schedule
|
327 |
|
|
|
339 |
config_id: 987654321
|
340 |
mcp_api_key: "your_mcp_key_here"
|
341 |
|
342 |
+
NOTE: The config_id must be obtained from a successful validate_api_configuration() response.
|
|
|
|
|
|
|
|
|
343 |
The mcp_api_key must match the one used during validation.
|
344 |
|
345 |
Returns:
|
|
|
368 |
## testing
|
369 |
if __name__ == "__main__":
|
370 |
# Example usage
|
371 |
+
response = validate_api_configuration(
|
372 |
mcp_api_key="your_api_key",
|
373 |
name="Dog Facts API",
|
374 |
description="Monitor random dog facts from a free API",
|
main.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from
|
4 |
import json
|
5 |
|
6 |
|
7 |
# API Validation Tab
|
8 |
validation_tab = gr.Interface(
|
9 |
-
fn=
|
10 |
inputs=[
|
11 |
gr.Textbox(
|
12 |
label="MCP API Key", placeholder="Enter your MCP API key", type="password"
|
@@ -101,7 +101,7 @@ validation_tab = gr.Interface(
|
|
101 |
|
102 |
# Scheduler Setup Tab
|
103 |
scheduler_tab = gr.Interface(
|
104 |
-
fn=
|
105 |
inputs=[
|
106 |
gr.Number(label="Config ID (from validation step)", value=None),
|
107 |
gr.Textbox(
|
|
|
1 |
import gradio as gr
|
2 |
+
from api_client import call_api
|
3 |
+
from api_monitor import validate_api_configuration, activate_monitoring
|
4 |
import json
|
5 |
|
6 |
|
7 |
# API Validation Tab
|
8 |
validation_tab = gr.Interface(
|
9 |
+
fn=validate_api_configuration,
|
10 |
inputs=[
|
11 |
gr.Textbox(
|
12 |
label="MCP API Key", placeholder="Enter your MCP API key", type="password"
|
|
|
101 |
|
102 |
# Scheduler Setup Tab
|
103 |
scheduler_tab = gr.Interface(
|
104 |
+
fn=activate_monitoring,
|
105 |
inputs=[
|
106 |
gr.Number(label="Config ID (from validation step)", value=None),
|
107 |
gr.Textbox(
|