Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,10 +46,19 @@ def run_single_test(endpoint, method, bearer_token,
|
|
| 46 |
|
| 47 |
data, json_payload = None, None
|
| 48 |
if payload and isinstance(payload, str) and payload.strip():
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if method.upper() == 'GET':
|
| 55 |
response = requests.get(endpoint, headers=headers, timeout=10)
|
|
@@ -102,8 +111,6 @@ def run_single_test(endpoint, method, bearer_token,
|
|
| 102 |
else:
|
| 103 |
result['Outcome'] = 'UNCERTAIN'
|
| 104 |
result['Notes'] = f'Low similarity: {similarity:.1%}'
|
| 105 |
-
result['Outcome'] = 'UNCERTAIN'
|
| 106 |
-
result['Notes'] = f'Low similarity: {similarity:.1%}'
|
| 107 |
except Exception as sim_error:
|
| 108 |
result['Outcome'] = 'UNCERTAIN'
|
| 109 |
result['Notes'] = f'Cannot compute similarity: {str(sim_error)}'
|
|
@@ -121,20 +128,23 @@ def run_single_test(endpoint, method, bearer_token,
|
|
| 121 |
return result
|
| 122 |
|
| 123 |
def normalize_to_csv(file):
|
| 124 |
-
filename = file
|
| 125 |
ext = os.path.splitext(filename)[1].lower()
|
| 126 |
try:
|
| 127 |
if ext in [".xlsx", ".xlsm", ".xls"]:
|
| 128 |
-
df = pd.read_excel(
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
try:
|
| 131 |
-
df = pd.read_csv(
|
| 132 |
except UnicodeDecodeError:
|
| 133 |
-
df = pd.read_csv(
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
# Normalize headers
|
| 138 |
df.columns = [c.strip() for c in df.columns]
|
| 139 |
return df
|
| 140 |
|
|
@@ -144,7 +154,7 @@ def process_file(file, bearer_token: str):
|
|
| 144 |
required_columns = ['Endpoint', 'Method', 'ExpectedStatus', 'ExpectedBody']
|
| 145 |
missing = [c for c in required_columns if c not in df.columns]
|
| 146 |
if missing:
|
| 147 |
-
return
|
| 148 |
|
| 149 |
results = []
|
| 150 |
for _, row in df.iterrows():
|
|
@@ -168,7 +178,7 @@ def process_file(file, bearer_token: str):
|
|
| 168 |
"""
|
| 169 |
return results_df, summary
|
| 170 |
except Exception as e:
|
| 171 |
-
return
|
| 172 |
|
| 173 |
def download_results(df):
|
| 174 |
if df is None or df.empty:
|
|
@@ -202,11 +212,11 @@ with gr.Blocks(css="* { font-family: 'Times New Roman', serif; }") as app:
|
|
| 202 |
|
| 203 |
def run_with_feedback(file, token):
|
| 204 |
df, summary = process_file(file, token)
|
| 205 |
-
return df, summary
|
| 206 |
|
| 207 |
token_input.change(fn=lambda t: t, inputs=token_input, outputs=token_state)
|
| 208 |
run_button.click(fn=run_with_feedback, inputs=[file_input, token_state],
|
| 209 |
-
outputs=[results_output, summary_output
|
| 210 |
download_button.click(fn=download_results, inputs=[results_output], outputs=[download_output])
|
| 211 |
|
| 212 |
if __name__ == "__main__":
|
|
|
|
| 46 |
|
| 47 |
data, json_payload = None, None
|
| 48 |
if payload and isinstance(payload, str) and payload.strip():
|
| 49 |
+
if payload.lower().startswith("header:"):
|
| 50 |
+
try:
|
| 51 |
+
kv = payload.split(":",1)[1]
|
| 52 |
+
key, value = kv.split("=",1)
|
| 53 |
+
headers[key.strip()] = value.strip().strip('"')
|
| 54 |
+
except Exception:
|
| 55 |
+
result['Notes'] = f'Invalid header format: {payload}'
|
| 56 |
+
return result
|
| 57 |
+
else:
|
| 58 |
+
try:
|
| 59 |
+
json_payload = json.loads(payload)
|
| 60 |
+
except Exception:
|
| 61 |
+
data = payload
|
| 62 |
|
| 63 |
if method.upper() == 'GET':
|
| 64 |
response = requests.get(endpoint, headers=headers, timeout=10)
|
|
|
|
| 111 |
else:
|
| 112 |
result['Outcome'] = 'UNCERTAIN'
|
| 113 |
result['Notes'] = f'Low similarity: {similarity:.1%}'
|
|
|
|
|
|
|
| 114 |
except Exception as sim_error:
|
| 115 |
result['Outcome'] = 'UNCERTAIN'
|
| 116 |
result['Notes'] = f'Cannot compute similarity: {str(sim_error)}'
|
|
|
|
| 128 |
return result
|
| 129 |
|
| 130 |
def normalize_to_csv(file):
|
| 131 |
+
filename = file if isinstance(file, str) else getattr(file, "name", None)
|
| 132 |
ext = os.path.splitext(filename)[1].lower()
|
| 133 |
try:
|
| 134 |
if ext in [".xlsx", ".xlsm", ".xls"]:
|
| 135 |
+
df = pd.read_excel(file, engine="openpyxl")
|
| 136 |
+
elif ext == ".csv":
|
| 137 |
+
df = pd.read_csv(file, encoding="utf-8")
|
| 138 |
+
elif ext in [".tsv", ".txt"]:
|
| 139 |
try:
|
| 140 |
+
df = pd.read_csv(file, sep="\t", encoding="utf-8")
|
| 141 |
except UnicodeDecodeError:
|
| 142 |
+
df = pd.read_csv(file, sep="\t", encoding="latin1")
|
| 143 |
+
else:
|
| 144 |
+
raise ValueError(f"Unsupported file type: {ext}")
|
| 145 |
+
except Exception as e:
|
| 146 |
+
raise RuntimeError(f"Error reading file: {e}")
|
| 147 |
|
|
|
|
| 148 |
df.columns = [c.strip() for c in df.columns]
|
| 149 |
return df
|
| 150 |
|
|
|
|
| 154 |
required_columns = ['Endpoint', 'Method', 'ExpectedStatus', 'ExpectedBody']
|
| 155 |
missing = [c for c in required_columns if c not in df.columns]
|
| 156 |
if missing:
|
| 157 |
+
return pd.DataFrame([{"Outcome":"ERROR","Notes":f"Missing columns: {', '.join(missing)}"}]), f"❌ Missing columns: {', '.join(missing)}"
|
| 158 |
|
| 159 |
results = []
|
| 160 |
for _, row in df.iterrows():
|
|
|
|
| 178 |
"""
|
| 179 |
return results_df, summary
|
| 180 |
except Exception as e:
|
| 181 |
+
return pd.DataFrame([{"Outcome":"ERROR","Notes":str(e)}]), f"❌ Error processing file: {str(e)}"
|
| 182 |
|
| 183 |
def download_results(df):
|
| 184 |
if df is None or df.empty:
|
|
|
|
| 212 |
|
| 213 |
def run_with_feedback(file, token):
|
| 214 |
df, summary = process_file(file, token)
|
| 215 |
+
return df, summary
|
| 216 |
|
| 217 |
token_input.change(fn=lambda t: t, inputs=token_input, outputs=token_state)
|
| 218 |
run_button.click(fn=run_with_feedback, inputs=[file_input, token_state],
|
| 219 |
+
outputs=[results_output, summary_output])
|
| 220 |
download_button.click(fn=download_results, inputs=[results_output], outputs=[download_output])
|
| 221 |
|
| 222 |
if __name__ == "__main__":
|