Spaces:
Running
Running
Commit
·
d1583b8
1
Parent(s):
5bb5bbf
Refactor parse_key_value_string for improved key-value parsing and update api_call to utilize this function for parameters and headers
Browse files- apiCall.py +41 -38
- api_validator.py +3 -0
apiCall.py
CHANGED
@@ -2,6 +2,42 @@ import requests
|
|
2 |
import json
|
3 |
import psycopg2
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def api_call(
|
6 |
method="GET",
|
7 |
base_url=None,
|
@@ -54,42 +90,9 @@ def api_call(
|
|
54 |
x-api-key: your_api_key_here
|
55 |
content-type: application/json
|
56 |
additional_params: {"messages": [{"role": "user", "content": "Hello there!"}]}
|
57 |
-
"""
|
58 |
-
|
59 |
-
|
60 |
-
headers = {}
|
61 |
-
|
62 |
-
# Process param_keys_values
|
63 |
-
if param_keys_values:
|
64 |
-
lines = param_keys_values.strip().split("\n")
|
65 |
-
for line in lines:
|
66 |
-
if ":" in line:
|
67 |
-
key, value = line.split(":", 1)
|
68 |
-
key = key.strip()
|
69 |
-
value = value.strip()
|
70 |
-
|
71 |
-
if key: # Only add non-empty keys
|
72 |
-
# Try to parse numeric values
|
73 |
-
if value.isdigit():
|
74 |
-
params[key] = int(value)
|
75 |
-
elif value.lower() == "true":
|
76 |
-
params[key] = True
|
77 |
-
elif value.lower() == "false":
|
78 |
-
params[key] = False
|
79 |
-
else:
|
80 |
-
params[key] = value
|
81 |
-
|
82 |
-
# Process header_keys_values
|
83 |
-
if header_keys_values:
|
84 |
-
lines = header_keys_values.strip().split("\n")
|
85 |
-
for line in lines:
|
86 |
-
if ":" in line:
|
87 |
-
key, value = line.split(":", 1)
|
88 |
-
key = key.strip()
|
89 |
-
value = value.strip()
|
90 |
-
|
91 |
-
if key: # Only add non-empty keys
|
92 |
-
headers[key] = value
|
93 |
|
94 |
# Handle additional parameters
|
95 |
if additional_params and additional_params.strip():
|
@@ -110,9 +113,9 @@ def api_call(
|
|
110 |
params=params,
|
111 |
headers=headers,
|
112 |
method=method,
|
113 |
-
)
|
114 |
return result
|
115 |
-
|
116 |
except Exception as e:
|
117 |
return f"Error making API call: {str(e)}"
|
118 |
|
|
|
2 |
import json
|
3 |
import psycopg2
|
4 |
|
5 |
+
|
6 |
+
def parse_key_value_string(key_value_string):
|
7 |
+
"""Parse a key-value string into a dictionary.
|
8 |
+
|
9 |
+
Parameters:
|
10 |
+
- key_value_string: String with key-value pairs, one per line, separated by ':'
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
- Dictionary of parsed key-value pairs
|
14 |
+
"""
|
15 |
+
result = {}
|
16 |
+
|
17 |
+
if not key_value_string:
|
18 |
+
return result
|
19 |
+
|
20 |
+
lines = key_value_string.strip().split("\n")
|
21 |
+
for line in lines:
|
22 |
+
if ":" in line:
|
23 |
+
key, value = line.split(":", 1)
|
24 |
+
key = key.strip()
|
25 |
+
value = value.strip()
|
26 |
+
|
27 |
+
if key: # Only add non-empty keys
|
28 |
+
# Try to parse numeric values
|
29 |
+
if value.isdigit():
|
30 |
+
result[key] = int(value)
|
31 |
+
elif value.lower() == "true":
|
32 |
+
result[key] = True
|
33 |
+
elif value.lower() == "false":
|
34 |
+
result[key] = False
|
35 |
+
else:
|
36 |
+
result[key] = value
|
37 |
+
|
38 |
+
return result
|
39 |
+
|
40 |
+
|
41 |
def api_call(
|
42 |
method="GET",
|
43 |
base_url=None,
|
|
|
90 |
x-api-key: your_api_key_here
|
91 |
content-type: application/json
|
92 |
additional_params: {"messages": [{"role": "user", "content": "Hello there!"}]}
|
93 |
+
""" # Build params and headers dictionaries from key-value pairs
|
94 |
+
params = parse_key_value_string(param_keys_values)
|
95 |
+
headers = parse_key_value_string(header_keys_values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
# Handle additional parameters
|
98 |
if additional_params and additional_params.strip():
|
|
|
113 |
params=params,
|
114 |
headers=headers,
|
115 |
method=method,
|
116 |
+
)
|
117 |
return result
|
118 |
+
|
119 |
except Exception as e:
|
120 |
return f"Error making API call: {str(e)}"
|
121 |
|
api_validator.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import apiCall
|
2 |
import json
|
3 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
4 |
import hashlib
|
5 |
import psycopg2
|
6 |
|
|
|
1 |
import apiCall
|
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 |
|