Skip to content

Commit 41fc5d0

Browse files
authored
fix: edge case handling in source amplitude
2 parents ac5305e + dd21b35 commit 41fc5d0

File tree

1 file changed

+25
-4
lines changed
  • airbyte-integrations/connectors/source-amplitude/source_amplitude

1 file changed

+25
-4
lines changed

airbyte-integrations/connectors/source-amplitude/source_amplitude/api.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,20 @@ class Events(IncrementalAmplitudeStream):
142142
state_checkpoint_interval = 1000
143143
time_interval = {"days": 3}
144144

145+
# To avoid raising http exceptions by default. (HttpStream class raises http errors by default if set to True)
146+
# Export API(Used to get events data) returns 404 when there is no data which is being propogated to UI as error in sync
147+
@property
148+
def raise_on_http_errors(self) -> bool:
149+
"""
150+
Override if needed. If set to False, allows opting-out of raising HTTP code exception.
151+
"""
152+
return False
153+
145154
def parse_response(self, response: requests.Response, stream_state: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping]:
155+
# Read records func calls this func which raises exception when 404 is sent as response
156+
if response.status_code == 404:
157+
self.logger.warn("No data available for the time range requested.")
158+
return []
146159
state_value = stream_state[self.cursor_field] if stream_state else self._start_date.strftime(self.compare_date_template)
147160
try:
148161
zip_file = zipfile.ZipFile(io.BytesIO(response.content))
@@ -231,8 +244,12 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
231244
if response_data:
232245
series = list(map(list, zip(*response_data["series"])))
233246
for i, date in enumerate(response_data["xValues"]):
234-
yield {"date": date, "statistics": dict(zip(response_data["seriesLabels"], series[i]))}
235-
247+
try:
248+
yield {"date": date, "statistics": dict(zip(response_data["seriesLabels"], series[i]))}
249+
except (IndexError,KeyError) as e:
250+
#To avoid propogating this error to UI
251+
self.logger.warn(e)
252+
236253
def path(self, **kwargs) -> str:
237254
return f"{self.api_version}/users"
238255

@@ -245,13 +262,17 @@ class AverageSessionLength(IncrementalAmplitudeStream):
245262

246263
def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
247264
response_data = response.json().get(self.data_field, [])
248-
if response_data:
265+
if response_data and response_data["series"]:
249266
# From the Amplitude documentation it follows that "series" is an array with one element which is itself
250267
# an array that contains the average session length for each day.
251268
# https://developers.amplitude.com/docs/dashboard-rest-api#returns-2
252269
series = response_data["series"][0]
253270
for i, date in enumerate(response_data["xValues"]):
254-
yield {"date": date, "length": series[i]}
271+
try:
272+
yield {"date": date, "length": series[i]}
273+
except IndexError as e:
274+
#To avoid propogating this error to UI
275+
self.logger.warn(e)
255276

256277
def path(self, **kwargs) -> str:
257278
return f"{self.api_version}/sessions/average"

0 commit comments

Comments
 (0)