3232 NWISSiteTransformer ,
3333 NWISWaterLevelTransformer ,
3434)
35- from backend .exceptions import USGSRateLimitError
35+ from backend .exceptions import USGSRateLimitError , PartialOrNoDataError
3636from backend .source import (
3737 BaseWaterLevelSource ,
3838 BaseSiteSource ,
@@ -91,6 +91,7 @@ def get_records(self):
9191 # if config.end_date:
9292 # params["endDt"] = config.end_dt.date().isoformat()
9393
94+ data : dict = {}
9495 tries : int = 0
9596
9697 while tries < MAX_RETRIES :
@@ -107,7 +108,7 @@ def get_records(self):
107108 )
108109
109110 if response .status_code == 200 :
110- data : dict = response .json ()
111+ data = response .json ()
111112 break
112113 elif response .status_code == 429 :
113114 raise USGSRateLimitError ()
@@ -125,6 +126,10 @@ def get_records(self):
125126 tries += 1
126127 time .sleep (tries )
127128
129+ if data == {}:
130+ self .warn ("Failed to retrieve site records after multiple attempts." )
131+ raise PartialOrNoDataError ("Failed to retrieve site records after multiple attempts." )
132+
128133 records : list = data .get ("features" , [])
129134
130135 return records
@@ -168,6 +173,8 @@ def get_records(self, site_record):
168173 list_of_sites
169174 ]
170175 }
176+
177+ data : dict = {}
171178 tries : int = 0
172179
173180 while tries < MAX_RETRIES :
@@ -184,7 +191,7 @@ def get_records(self, site_record):
184191 timeout = TIMEOUT ,
185192 )
186193 if response .status_code == 200 :
187- data : dict = response .json ()
194+ data = response .json ()
188195 break
189196 elif response .status_code == 429 :
190197 raise USGSRateLimitError ()
@@ -202,6 +209,10 @@ def get_records(self, site_record):
202209 tries += 1
203210 time .sleep (tries )
204211
212+ if data == {}:
213+ self .warn ("Failed to retrieve water level records after multiple attempts." )
214+ raise PartialOrNoDataError ("Failed to retrieve water level records after multiple attempts." )
215+
205216 features : list [dict ] = data .get ("features" , [])
206217
207218 standard_features : list [dict ] = [self ._standardize_record (feature ) for feature in features ]
@@ -225,6 +236,7 @@ def get_records(self, site_record):
225236 # USGS APIs use cursor pagination, so we can just follow the "next" links until there are no more
226237 while found_next_link:
227238 tries: int = 0
239+ data: dict = {}
228240 while tries < MAX_RETRIES:
229241 try:
230242 response = httpx.get(
@@ -233,7 +245,7 @@ def get_records(self, site_record):
233245 timeout=TIMEOUT,
234246 )
235247 if response.status_code == 200:
236- data: dict = response.json()
248+ data = response.json()
237249 break
238250 elif response.status_code == 429:
239251 raise USGSRateLimitError()
@@ -250,6 +262,10 @@ def get_records(self, site_record):
250262 tries += 1
251263 time.sleep(tries)
252264
265+ if data == {}:
266+ self.warn("Failed to retrieve paginated water level records after multiple attempts.")
267+ raise PartialOrNoDataError("Failed to retrieve paginated water level records after multiple attempts
268+
253269 features: list[dict] = data.get("features", [])
254270 standard_features: list[dict] = [self._standardize_record(feature) for feature in features]
255271 records.extend(standard_features)
0 commit comments