Skip to content

Commit b917b34

Browse files
committed
Updated proxy test
1 parent 5238659 commit b917b34

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

e2e/test_proxy.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
import uuid
55
import requests
6-
6+
from utils.immunisation_api import ImmunisationApi
77
from lib.env import get_service_base_path, get_status_endpoint_api_key
88

99

@@ -40,8 +40,11 @@ def test_mtls(self):
4040
backend_health = f"https://{backend_url}/status"
4141

4242
with self.assertRaises(requests.exceptions.RequestException) as e:
43-
requests.get(backend_health, headers={"X-Request-ID": str(uuid.uuid4())})
44-
self.assertTrue("RemoteDisconnected" in str(e.exception))
43+
ImmunisationApi.make_request_with_backoff(
44+
http_method="GET",
45+
url=backend_health,
46+
headers={"X-Request-ID": str(uuid.uuid4())})
47+
self.assertTrue("RemoteDisconnected" in str(e.exception))
4548

4649
@staticmethod
4750
def get_backend_url() -> str:

e2e/utils/immunisation_api.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ def parse_location(location) -> Optional[str]:
2121

2222

2323
class ImmunisationApi:
24-
MAX_RETRIES = 5
25-
STANDARD_REQUEST_DELAY_SECONDS = 1
26-
2724
url: str
2825
headers: dict
2926
auth: BaseAuthentication
@@ -50,16 +47,19 @@ def __str__(self):
5047
# The e2e tests put pressure on both test environments from APIGEE and PDS
5148
# so the chances of having rate limiting errors are high especially during
5249
# the busy times of the day.
53-
def _make_request_with_backoff(
54-
self,
50+
@staticmethod
51+
def make_request_with_backoff(
5552
http_method: str,
5653
url: str,
5754
expected_status_code: int,
55+
headers: dict = None,
56+
max_retries: int = 5,
57+
delay_seconds: float = 1.0,
5858
**kwargs
5959
):
60-
for attempt in range(self.MAX_RETRIES):
60+
for attempt in range(max_retries):
6161
try:
62-
response = requests.request(http_method, url, **kwargs)
62+
response = requests.request(method=http_method, url=url, headers=headers, **kwargs)
6363

6464
if response.status_code != expected_status_code:
6565
if response.status_code >= 500:
@@ -72,11 +72,11 @@ def _make_request_with_backoff(
7272
return response
7373

7474
except Exception as e:
75-
if attempt == self.MAX_RETRIES - 1:
75+
if attempt == max_retries - 1:
7676
raise
7777

7878
wait = (2 ** attempt) + random.uniform(0, 0.5)
79-
total_wait_time = wait + self.STANDARD_REQUEST_DELAY_SECONDS
79+
total_wait_time = wait + delay_seconds
8080

8181
print(
8282
f"[{datetime.now():%Y-%m-%d %H:%M:%S}] "
@@ -105,15 +105,15 @@ def create_a_deleted_immunization_resource(self, resource: dict = None) -> dict:
105105
return imms
106106

107107
def get_immunization_by_id(self, event_id, expected_status_code: int = 200):
108-
return self._make_request_with_backoff(
108+
return self.make_request_with_backoff(
109109
"GET",
110110
f"{self.url}/Immunization/{event_id}",
111111
expected_status_code,
112112
headers=self._update_headers()
113113
)
114114

115115
def create_immunization(self, imms, expected_status_code: int = 201):
116-
response = self._make_request_with_backoff(
116+
response = self.make_request_with_backoff(
117117
"POST",
118118
f"{self.url}/Immunization",
119119
expected_status_code,
@@ -134,7 +134,7 @@ def create_immunization(self, imms, expected_status_code: int = 201):
134134
return response
135135

136136
def update_immunization(self, imms_id, imms, expected_status_code: int = 200):
137-
return self._make_request_with_backoff(
137+
return self.make_request_with_backoff(
138138
"PUT",
139139
f"{self.url}/Immunization/{imms_id}",
140140
expected_status_code,
@@ -143,15 +143,15 @@ def update_immunization(self, imms_id, imms, expected_status_code: int = 200):
143143
)
144144

145145
def delete_immunization(self, imms_id, expected_status_code: int = 204):
146-
return self._make_request_with_backoff(
146+
return self.make_request_with_backoff(
147147
"DELETE",
148148
f"{self.url}/Immunization/{imms_id}",
149149
expected_status_code,
150150
headers=self._update_headers()
151151
)
152152

153153
def search_immunizations(self, patient_identifier: str, immunization_target: str, expected_status_code: int = 200):
154-
return self._make_request_with_backoff(
154+
return self.make_request_with_backoff(
155155
"GET",
156156
f"{self.url}/Immunization?patient.identifier={patient_identifier_system}|{patient_identifier}"
157157
f"&-immunization.target={immunization_target}",
@@ -171,7 +171,7 @@ def search_immunizations_full(
171171
else:
172172
url = f"{self.url}/Immunization?{query_string}"
173173

174-
return self._make_request_with_backoff(
174+
return self.make_request_with_backoff(
175175
http_method,
176176
url,
177177
expected_status_code,

0 commit comments

Comments
 (0)