@@ -21,9 +21,6 @@ def parse_location(location) -> Optional[str]:
2121
2222
2323class 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