Skip to content

Commit 3142caf

Browse files
authored
Export semantic router sample app HTTP request data (#22)
* chore(samples): format and remove unused imports * refactor(samples): export http request data
1 parent 5a1035f commit 3142caf

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

samples/autogen/semantic-router/tests/test_api.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import json
5-
import os
6-
import sys
75
import time
86

97
import urllib3
108

9+
payloads = []
10+
response_data_array = []
11+
1112

1213
def wait_for_service(url, retries=5, delay=2):
1314
"""Wait for the service to be available by polling the health endpoint."""
@@ -47,9 +48,9 @@ def test_api_post_request():
4748
health_url = "http://localhost:8000/healthz"
4849

4950
# Wait for the service to be ready
50-
assert wait_for_service(
51-
health_url, retries=10
52-
), "Service did not become ready in time."
51+
assert wait_for_service(health_url, retries=10), (
52+
"Service did not become ready in time."
53+
)
5354

5455
# Sleep for a few seconds to ensure that the service is ready
5556
time.sleep(2)
@@ -66,6 +67,7 @@ def test_api_post_request():
6667
"context": "ctx",
6768
"intent": "asd",
6869
}
70+
payloads.append(payload)
6971

7072
# Make a POST request to the API
7173
response = http.request(
@@ -77,13 +79,14 @@ def test_api_post_request():
7779

7880
# Assert that the status code is 404 (OK)
7981
# as there is no agent to handle the request for the given intent
80-
assert (
81-
response.status == 404
82-
), f"Expected status code 404, but got {response.status}: {response.data}"
82+
assert response.status == 404, (
83+
f"Expected status code 404, but got {response.status}: {response.data}"
84+
)
8385

8486
# Now let's make a valid request for the intent "hr"
8587
payload["intent"] = "hr"
8688
payload["message"] = "My name is Python"
89+
payloads.append(payload)
8790

8891
# Make a POST request to the API
8992
response = http.request(
@@ -94,23 +97,23 @@ def test_api_post_request():
9497
)
9598

9699
# Assert that the status code is 200 (OK)
97-
assert (
98-
response.status == 200
99-
), f"Expected status code 200, but got {response.status}"
100+
assert response.status == 200, (
101+
f"Expected status code 200, but got {response.status}"
102+
)
100103

101104
# Decode the response body
102105
response_data = json.loads(response.data.decode("utf-8"))
106+
response_data_array.append(response_data)
103107

104108
# Assert that the response contains the expected keys
105-
assert (
106-
"agent_id" in response_data
107-
), "Response does not contain 'agent_id' key"
109+
assert "agent_id" in response_data, "Response does not contain 'agent_id' key"
108110

109111
# Save agent_id
110112
agent_id = response_data["agent_id"]
111113

112114
# Send another request with a different context
113115
payload["context"] = "ctx2"
116+
payloads.append(payload)
114117

115118
# Make a POST request to the API
116119
response = http.request(
@@ -121,22 +124,21 @@ def test_api_post_request():
121124
)
122125

123126
# Assert that the status code is 200 (OK)
124-
assert (
125-
response.status == 200
126-
), f"Expected status code 200, but got {response.status}"
127+
assert response.status == 200, (
128+
f"Expected status code 200, but got {response.status}"
129+
)
127130

128131
# Decode the response body
129132
response_data = json.loads(response.data.decode("utf-8"))
133+
response_data_array.append(response_data)
130134

131135
# Assert that the response contains the expected keys
132-
assert (
133-
"agent_id" in response_data
134-
), "Response does not contain 'agent_id' key"
136+
assert "agent_id" in response_data, "Response does not contain 'agent_id' key"
135137

136138
# Assert that the agent_id is different from the previous response
137-
assert (
138-
response_data["agent_id"] != agent_id
139-
), "Agent ID should be different for different contexts"
139+
assert response_data["agent_id"] != agent_id, (
140+
"Agent ID should be different for different contexts"
141+
)
140142

141143
# Optionally, release the connection back to the pool
142144
response.release_conn()

0 commit comments

Comments
 (0)