|
| 1 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, List, Mapping, MutableMapping, Optional |
| 5 | + |
| 6 | +import requests |
| 7 | +from airbyte_cdk.sources.declarative.extractors.record_extractor import RecordExtractor |
| 8 | +from airbyte_cdk.sources.declarative.incremental import DatetimeBasedCursor |
| 9 | +from airbyte_cdk.sources.declarative.migrations.state_migration import StateMigration |
| 10 | +from airbyte_cdk.sources.declarative.requesters.request_option import RequestOptionType |
| 11 | +from airbyte_cdk.sources.declarative.types import StreamSlice, StreamState |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class ZendeskSupportAuditLogsIncrementalSync(DatetimeBasedCursor): |
| 16 | + """ |
| 17 | + This class is created for the Audit Logs stream. List with time range is used for record filtering. |
| 18 | + """ |
| 19 | + |
| 20 | + def get_request_params( |
| 21 | + self, |
| 22 | + *, |
| 23 | + stream_state: Optional[StreamState] = None, |
| 24 | + stream_slice: Optional[StreamSlice] = None, |
| 25 | + next_page_token: Optional[Mapping[str, Any]] = None, |
| 26 | + ) -> Mapping[str, Any]: |
| 27 | + option_type = RequestOptionType.request_parameter |
| 28 | + options: MutableMapping[str, Any] = {} |
| 29 | + if not stream_slice: |
| 30 | + return options |
| 31 | + |
| 32 | + # set list with time range |
| 33 | + if self.start_time_option and self.start_time_option.inject_into == option_type: |
| 34 | + start_time = stream_slice.get(self._partition_field_start.eval(self.config)) |
| 35 | + options[self.start_time_option.field_name.eval(config=self.config)] = [start_time] # type: ignore # field_name is always casted to an interpolated string |
| 36 | + if self.end_time_option and self.end_time_option.inject_into == option_type: |
| 37 | + options[self.end_time_option.field_name.eval(config=self.config)].append(stream_slice.get(self._partition_field_end.eval(self.config))) # type: ignore # field_name is always casted to an interpolated string |
| 38 | + return options |
| 39 | + |
| 40 | + |
| 41 | +class ZendeskSupportExtractorEvents(RecordExtractor): |
| 42 | + def extract_records(self, response: requests.Response) -> List[Mapping[str, Any]]: |
| 43 | + try: |
| 44 | + records = response.json().get("ticket_events") or [] |
| 45 | + except requests.exceptions.JSONDecodeError: |
| 46 | + records = [] |
| 47 | + |
| 48 | + events = [] |
| 49 | + for record in records: |
| 50 | + for event in record.get("child_events", []): |
| 51 | + if event.get("event_type") == "Comment": |
| 52 | + for prop in ["via_reference_id", "ticket_id", "timestamp"]: |
| 53 | + event[prop] = record.get(prop) |
| 54 | + |
| 55 | + # https://github.com/airbytehq/oncall/issues/1001 |
| 56 | + if not isinstance(event.get("via"), dict): |
| 57 | + event["via"] = None |
| 58 | + events.append(event) |
| 59 | + return events |
| 60 | + |
| 61 | + |
| 62 | +class ZendeskSupportAttributeDefinitionsExtractor(RecordExtractor): |
| 63 | + def extract_records(self, response: requests.Response) -> List[Mapping[str, Any]]: |
| 64 | + try: |
| 65 | + records = [] |
| 66 | + for definition in response.json()["definitions"]["conditions_all"]: |
| 67 | + definition["condition"] = "all" |
| 68 | + records.append(definition) |
| 69 | + for definition in response.json()["definitions"]["conditions_any"]: |
| 70 | + definition["condition"] = "any" |
| 71 | + records.append(definition) |
| 72 | + except requests.exceptions.JSONDecodeError: |
| 73 | + records = [] |
| 74 | + return records |
0 commit comments