ingestr 0.8.4__py3-none-any.whl → 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ingestr might be problematic. Click here for more details.
- ingestr/src/factory.py +6 -0
- ingestr/src/filesystem/__init__.py +98 -0
- ingestr/src/filesystem/helpers.py +100 -0
- ingestr/src/filesystem/readers.py +131 -0
- ingestr/src/shopify/__init__.py +3 -1
- ingestr/src/sources.py +149 -2
- ingestr/src/version.py +1 -1
- ingestr/src/zendesk/__init__.py +460 -0
- ingestr/src/zendesk/helpers/__init__.py +25 -0
- ingestr/src/zendesk/helpers/api_helpers.py +105 -0
- ingestr/src/zendesk/helpers/credentials.py +54 -0
- ingestr/src/zendesk/helpers/talk_api.py +118 -0
- ingestr/src/zendesk/settings.py +57 -0
- {ingestr-0.8.4.dist-info → ingestr-0.9.0.dist-info}/METADATA +12 -1
- {ingestr-0.8.4.dist-info → ingestr-0.9.0.dist-info}/RECORD +18 -9
- {ingestr-0.8.4.dist-info → ingestr-0.9.0.dist-info}/WHEEL +0 -0
- {ingestr-0.8.4.dist-info → ingestr-0.9.0.dist-info}/entry_points.txt +0 -0
- {ingestr-0.8.4.dist-info → ingestr-0.9.0.dist-info}/licenses/LICENSE.md +0 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines all the sources and resources needed for ZendeskSupport, ZendeskChat and ZendeskTalk
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from itertools import chain
|
|
6
|
+
from typing import Iterable, Iterator, Optional
|
|
7
|
+
|
|
8
|
+
import dlt
|
|
9
|
+
from dlt.common import pendulum
|
|
10
|
+
from dlt.common.time import ensure_pendulum_datetime
|
|
11
|
+
from dlt.common.typing import TAnyDateTime, TDataItem, TDataItems
|
|
12
|
+
from dlt.sources import DltResource
|
|
13
|
+
|
|
14
|
+
from .helpers.api_helpers import process_ticket, process_ticket_field
|
|
15
|
+
from .helpers.credentials import TZendeskCredentials, ZendeskCredentialsOAuth
|
|
16
|
+
from .helpers.talk_api import PaginationType, ZendeskAPIClient
|
|
17
|
+
from .settings import (
|
|
18
|
+
CUSTOM_FIELDS_STATE_KEY,
|
|
19
|
+
DEFAULT_START_DATE,
|
|
20
|
+
INCREMENTAL_TALK_ENDPOINTS,
|
|
21
|
+
SUPPORT_ENDPOINTS,
|
|
22
|
+
SUPPORT_EXTRA_ENDPOINTS,
|
|
23
|
+
TALK_ENDPOINTS,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dlt.source(max_table_nesting=0)
|
|
28
|
+
def zendesk_talk(
|
|
29
|
+
credentials: TZendeskCredentials = dlt.secrets.value,
|
|
30
|
+
start_date: Optional[TAnyDateTime] = DEFAULT_START_DATE,
|
|
31
|
+
end_date: Optional[TAnyDateTime] = None,
|
|
32
|
+
) -> Iterable[DltResource]:
|
|
33
|
+
"""
|
|
34
|
+
Retrieves data from Zendesk Talk for phone calls and voicemails.
|
|
35
|
+
|
|
36
|
+
`start_date` argument can be used on its own or together with `end_date`. When both are provided
|
|
37
|
+
data is limited to items updated in that time range.
|
|
38
|
+
The range is "half-open", meaning elements equal and higher than `start_date` and elements lower than `end_date` are included.
|
|
39
|
+
All resources opt-in to use Airflow scheduler if run as Airflow task
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
credentials: The credentials for authentication. Defaults to the value in the `dlt.secrets` object.
|
|
43
|
+
start_date: The start time of the range for which to load. Defaults to January 1st 2000.
|
|
44
|
+
end_date: The end time of the range for which to load data.
|
|
45
|
+
If end time is not provided, the incremental loading will be enabled and after initial run, only new data will be retrieved
|
|
46
|
+
Yields:
|
|
47
|
+
DltResource: Data resources from Zendesk Talk.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
# use the credentials to authenticate with the ZendeskClient
|
|
51
|
+
zendesk_client = ZendeskAPIClient(credentials)
|
|
52
|
+
start_date_obj = ensure_pendulum_datetime(start_date)
|
|
53
|
+
end_date_obj = ensure_pendulum_datetime(end_date) if end_date else None
|
|
54
|
+
|
|
55
|
+
# regular endpoints
|
|
56
|
+
for key, talk_endpoint, item_name, cursor_paginated in TALK_ENDPOINTS:
|
|
57
|
+
yield dlt.resource(
|
|
58
|
+
talk_resource(
|
|
59
|
+
zendesk_client,
|
|
60
|
+
key,
|
|
61
|
+
item_name or talk_endpoint,
|
|
62
|
+
PaginationType.CURSOR if cursor_paginated else PaginationType.OFFSET,
|
|
63
|
+
),
|
|
64
|
+
name=key,
|
|
65
|
+
write_disposition="replace",
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# adding incremental endpoints
|
|
69
|
+
for key, talk_incremental_endpoint in INCREMENTAL_TALK_ENDPOINTS.items():
|
|
70
|
+
yield dlt.resource(
|
|
71
|
+
talk_incremental_resource,
|
|
72
|
+
name=f"{key}_incremental",
|
|
73
|
+
primary_key="id",
|
|
74
|
+
write_disposition="merge",
|
|
75
|
+
)(
|
|
76
|
+
zendesk_client=zendesk_client,
|
|
77
|
+
talk_endpoint_name=key,
|
|
78
|
+
talk_endpoint=talk_incremental_endpoint,
|
|
79
|
+
updated_at=dlt.sources.incremental[str](
|
|
80
|
+
"updated_at",
|
|
81
|
+
initial_value=start_date_obj.isoformat(),
|
|
82
|
+
end_value=end_date_obj.isoformat() if end_date_obj else None,
|
|
83
|
+
allow_external_schedulers=True,
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def talk_resource(
|
|
89
|
+
zendesk_client: ZendeskAPIClient,
|
|
90
|
+
talk_endpoint_name: str,
|
|
91
|
+
talk_endpoint: str,
|
|
92
|
+
pagination_type: PaginationType,
|
|
93
|
+
) -> Iterator[TDataItem]:
|
|
94
|
+
"""
|
|
95
|
+
Loads data from a Zendesk Talk endpoint.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
zendesk_client: An instance of ZendeskAPIClient for making API calls to Zendesk Talk.
|
|
99
|
+
talk_endpoint_name: The name of the talk_endpoint.
|
|
100
|
+
talk_endpoint: The actual URL ending of the endpoint.
|
|
101
|
+
pagination: Type of pagination type used by endpoint
|
|
102
|
+
|
|
103
|
+
Yields:
|
|
104
|
+
TDataItem: Dictionary containing the data from the endpoint.
|
|
105
|
+
"""
|
|
106
|
+
# send query and process it
|
|
107
|
+
yield from zendesk_client.get_pages(
|
|
108
|
+
talk_endpoint, talk_endpoint_name, pagination_type
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def talk_incremental_resource(
|
|
113
|
+
zendesk_client: ZendeskAPIClient,
|
|
114
|
+
talk_endpoint_name: str,
|
|
115
|
+
talk_endpoint: str,
|
|
116
|
+
updated_at: dlt.sources.incremental[str],
|
|
117
|
+
) -> Iterator[TDataItem]:
|
|
118
|
+
"""
|
|
119
|
+
Loads data from a Zendesk Talk endpoint with incremental loading.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
zendesk_client: An instance of ZendeskAPIClient for making API calls to Zendesk Talk.
|
|
123
|
+
talk_endpoint_name: The name of the talk_endpoint.
|
|
124
|
+
talk_endpoint: The actual URL ending of the endpoint.
|
|
125
|
+
updated_at: Source for the last updated timestamp.
|
|
126
|
+
|
|
127
|
+
Yields:
|
|
128
|
+
TDataItem: Dictionary containing the data from the endpoint.
|
|
129
|
+
"""
|
|
130
|
+
# send the request and process it
|
|
131
|
+
for page in zendesk_client.get_pages(
|
|
132
|
+
talk_endpoint,
|
|
133
|
+
talk_endpoint_name,
|
|
134
|
+
PaginationType.START_TIME,
|
|
135
|
+
params={
|
|
136
|
+
"start_time": ensure_pendulum_datetime(updated_at.last_value).int_timestamp
|
|
137
|
+
},
|
|
138
|
+
):
|
|
139
|
+
yield page
|
|
140
|
+
if updated_at.end_out_of_range:
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@dlt.source(max_table_nesting=0)
|
|
145
|
+
def zendesk_chat(
|
|
146
|
+
credentials: ZendeskCredentialsOAuth,
|
|
147
|
+
start_date: Optional[TAnyDateTime] = DEFAULT_START_DATE,
|
|
148
|
+
end_date: Optional[TAnyDateTime] = None,
|
|
149
|
+
) -> Iterable[DltResource]:
|
|
150
|
+
"""
|
|
151
|
+
Retrieves data from Zendesk Chat for chat interactions.
|
|
152
|
+
|
|
153
|
+
`start_date` argument can be used on its own or together with `end_date`. When both are provided
|
|
154
|
+
data is limited to items updated in that time range.
|
|
155
|
+
The range is "half-open", meaning elements equal and higher than `start_date` and elements lower than `end_date` are included.
|
|
156
|
+
All resources opt-in to use Airflow scheduler if run as Airflow task
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
credentials: The credentials for authentication. Defaults to the value in the `dlt.secrets` object.
|
|
160
|
+
start_date: The start time of the range for which to load. Defaults to January 1st 2000.
|
|
161
|
+
end_date: The end time of the range for which to load data.
|
|
162
|
+
If end time is not provided, the incremental loading will be enabled and after initial run, only new data will be retrieved
|
|
163
|
+
|
|
164
|
+
Yields:
|
|
165
|
+
DltResource: Data resources from Zendesk Chat.
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
# Authenticate
|
|
169
|
+
zendesk_client = ZendeskAPIClient(credentials, url_prefix="https://www.zopim.com")
|
|
170
|
+
start_date_obj = ensure_pendulum_datetime(start_date)
|
|
171
|
+
end_date_obj = ensure_pendulum_datetime(end_date) if end_date else None
|
|
172
|
+
|
|
173
|
+
yield dlt.resource(chats_table_resource, name="chats", write_disposition="merge")(
|
|
174
|
+
zendesk_client,
|
|
175
|
+
dlt.sources.incremental[str](
|
|
176
|
+
"update_timestamp|updated_timestamp",
|
|
177
|
+
initial_value=start_date_obj.isoformat(),
|
|
178
|
+
end_value=end_date_obj.isoformat() if end_date_obj else None,
|
|
179
|
+
allow_external_schedulers=True,
|
|
180
|
+
),
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def chats_table_resource(
|
|
185
|
+
zendesk_client: ZendeskAPIClient,
|
|
186
|
+
update_timestamp: dlt.sources.incremental[str],
|
|
187
|
+
) -> Iterator[TDataItems]:
|
|
188
|
+
"""
|
|
189
|
+
Resource for Chats
|
|
190
|
+
|
|
191
|
+
Args:
|
|
192
|
+
zendesk_client: The Zendesk API client instance, used to make calls to Zendesk API.
|
|
193
|
+
update_timestamp: Incremental source specifying the timestamp for incremental loading.
|
|
194
|
+
|
|
195
|
+
Yields:
|
|
196
|
+
dict: A dictionary representing each row of data.
|
|
197
|
+
"""
|
|
198
|
+
chat_pages = zendesk_client.get_pages(
|
|
199
|
+
"/api/v2/incremental/chats",
|
|
200
|
+
"chats",
|
|
201
|
+
PaginationType.START_TIME,
|
|
202
|
+
params={
|
|
203
|
+
"start_time": ensure_pendulum_datetime(
|
|
204
|
+
update_timestamp.last_value
|
|
205
|
+
).int_timestamp,
|
|
206
|
+
"fields": "chats(*)",
|
|
207
|
+
},
|
|
208
|
+
)
|
|
209
|
+
for page in chat_pages:
|
|
210
|
+
yield page
|
|
211
|
+
|
|
212
|
+
if update_timestamp.end_out_of_range:
|
|
213
|
+
return
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
@dlt.source(max_table_nesting=0)
|
|
217
|
+
def zendesk_support(
|
|
218
|
+
credentials: TZendeskCredentials,
|
|
219
|
+
load_all: bool = True,
|
|
220
|
+
pivot_ticket_fields: bool = True,
|
|
221
|
+
start_date: Optional[TAnyDateTime] = DEFAULT_START_DATE,
|
|
222
|
+
end_date: Optional[TAnyDateTime] = None,
|
|
223
|
+
) -> Iterable[DltResource]:
|
|
224
|
+
"""
|
|
225
|
+
Retrieves data from Zendesk Support for tickets, users, brands, organizations, and groups.
|
|
226
|
+
|
|
227
|
+
`start_date` argument can be used on its own or together with `end_date`. When both are provided
|
|
228
|
+
data is limited to items updated in that time range.
|
|
229
|
+
The range is "half-open", meaning elements equal and higher than `start_date` and elements lower than `end_date` are included.
|
|
230
|
+
All resources opt-in to use Airflow scheduler if run as Airflow task
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
credentials: The credentials for authentication. Defaults to the value in the `dlt.secrets` object.
|
|
234
|
+
load_all: Whether to load extra resources for the API. Defaults to True.
|
|
235
|
+
pivot_ticket_fields: Whether to pivot the custom fields in tickets. Defaults to True.
|
|
236
|
+
start_date: The start time of the range for which to load. Defaults to January 1st 2000.
|
|
237
|
+
end_date: The end time of the range for which to load data.
|
|
238
|
+
If end time is not provided, the incremental loading will be enabled and after initial run, only new data will be retrieved
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
Sequence[DltResource]: Multiple dlt resources.
|
|
242
|
+
"""
|
|
243
|
+
|
|
244
|
+
start_date_obj = ensure_pendulum_datetime(start_date)
|
|
245
|
+
end_date_obj = ensure_pendulum_datetime(end_date) if end_date else None
|
|
246
|
+
|
|
247
|
+
start_date_ts = start_date_obj.int_timestamp
|
|
248
|
+
start_date_iso_str = start_date_obj.isoformat()
|
|
249
|
+
end_date_ts: Optional[int] = None
|
|
250
|
+
end_date_iso_str: Optional[str] = None
|
|
251
|
+
if end_date_obj:
|
|
252
|
+
end_date_ts = end_date_obj.int_timestamp
|
|
253
|
+
end_date_iso_str = end_date_obj.isoformat()
|
|
254
|
+
|
|
255
|
+
@dlt.resource(primary_key="id", write_disposition="append")
|
|
256
|
+
def ticket_events(
|
|
257
|
+
zendesk_client: ZendeskAPIClient,
|
|
258
|
+
timestamp: dlt.sources.incremental[int] = dlt.sources.incremental(
|
|
259
|
+
"timestamp",
|
|
260
|
+
initial_value=start_date_ts,
|
|
261
|
+
end_value=end_date_ts,
|
|
262
|
+
allow_external_schedulers=True,
|
|
263
|
+
),
|
|
264
|
+
) -> Iterator[TDataItem]:
|
|
265
|
+
# URL For ticket events
|
|
266
|
+
# 'https://d3v-dlthub.zendesk.com/api/v2/incremental/ticket_events.json?start_time=946684800'
|
|
267
|
+
event_pages = zendesk_client.get_pages(
|
|
268
|
+
"/api/v2/incremental/ticket_events.json",
|
|
269
|
+
"ticket_events",
|
|
270
|
+
PaginationType.STREAM,
|
|
271
|
+
params={"start_time": timestamp.last_value},
|
|
272
|
+
)
|
|
273
|
+
for page in event_pages:
|
|
274
|
+
yield page
|
|
275
|
+
if timestamp.end_out_of_range:
|
|
276
|
+
return
|
|
277
|
+
|
|
278
|
+
@dlt.resource(
|
|
279
|
+
name="tickets",
|
|
280
|
+
primary_key="id",
|
|
281
|
+
write_disposition="merge",
|
|
282
|
+
columns={
|
|
283
|
+
"tags": {"data_type": "complex"},
|
|
284
|
+
"custom_fields": {"data_type": "complex"},
|
|
285
|
+
},
|
|
286
|
+
)
|
|
287
|
+
def ticket_table(
|
|
288
|
+
zendesk_client: ZendeskAPIClient,
|
|
289
|
+
pivot_fields: bool = True,
|
|
290
|
+
updated_at: dlt.sources.incremental[
|
|
291
|
+
pendulum.DateTime
|
|
292
|
+
] = dlt.sources.incremental(
|
|
293
|
+
"updated_at",
|
|
294
|
+
initial_value=start_date_obj,
|
|
295
|
+
end_value=end_date_obj,
|
|
296
|
+
allow_external_schedulers=True,
|
|
297
|
+
),
|
|
298
|
+
) -> Iterator[TDataItem]:
|
|
299
|
+
"""
|
|
300
|
+
Resource for tickets table. Uses DLT state to handle column renaming of custom fields to prevent changing the names of said columns.
|
|
301
|
+
This resource uses pagination, loading and side loading to make API calls more efficient.
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
zendesk_client: The Zendesk API client instance, used to make calls to Zendesk API.
|
|
305
|
+
pivot_fields: Indicates whether to pivot the custom fields in tickets. Defaults to True.
|
|
306
|
+
per_page: The number of Ticket objects to load per page. Defaults to 1000.
|
|
307
|
+
updated_at: Incremental source for the 'updated_at' column.
|
|
308
|
+
Defaults to dlt.sources.incremental("updated_at", initial_value=start_date).
|
|
309
|
+
|
|
310
|
+
Yields:
|
|
311
|
+
TDataItem: Dictionary containing the ticket data.
|
|
312
|
+
"""
|
|
313
|
+
# grab the custom fields from dlt state if any
|
|
314
|
+
if pivot_fields:
|
|
315
|
+
load_ticket_fields_state(zendesk_client)
|
|
316
|
+
fields_dict = dlt.current.source_state().setdefault(CUSTOM_FIELDS_STATE_KEY, {})
|
|
317
|
+
# include_objects = ["users", "groups", "organisation", "brands"]
|
|
318
|
+
ticket_pages = zendesk_client.get_pages(
|
|
319
|
+
"/api/v2/incremental/tickets",
|
|
320
|
+
"tickets",
|
|
321
|
+
PaginationType.STREAM,
|
|
322
|
+
params={"start_time": updated_at.last_value.int_timestamp},
|
|
323
|
+
)
|
|
324
|
+
for page in ticket_pages:
|
|
325
|
+
yield [
|
|
326
|
+
process_ticket(ticket, fields_dict, pivot_custom_fields=pivot_fields)
|
|
327
|
+
for ticket in page
|
|
328
|
+
]
|
|
329
|
+
# stop loading when using end_value and end is reached
|
|
330
|
+
if updated_at.end_out_of_range:
|
|
331
|
+
return
|
|
332
|
+
|
|
333
|
+
@dlt.resource(
|
|
334
|
+
name="ticket_metric_events", primary_key="id", write_disposition="append"
|
|
335
|
+
)
|
|
336
|
+
def ticket_metric_table(
|
|
337
|
+
zendesk_client: ZendeskAPIClient,
|
|
338
|
+
time: dlt.sources.incremental[str] = dlt.sources.incremental(
|
|
339
|
+
"time",
|
|
340
|
+
initial_value=start_date_iso_str,
|
|
341
|
+
end_value=end_date_iso_str,
|
|
342
|
+
allow_external_schedulers=True,
|
|
343
|
+
),
|
|
344
|
+
) -> Iterator[TDataItem]:
|
|
345
|
+
"""
|
|
346
|
+
Resource for ticket metric events table. Returns all the ticket metric events from the starting date,
|
|
347
|
+
with the default starting date being January 1st of the current year.
|
|
348
|
+
|
|
349
|
+
Args:
|
|
350
|
+
zendesk_client: The Zendesk API client instance, used to make calls to Zendesk API.
|
|
351
|
+
time: Incremental source for the 'time' column,
|
|
352
|
+
indicating the starting date for retrieving ticket metric events.
|
|
353
|
+
Defaults to dlt.sources.incremental("time", initial_value=start_date_iso_str).
|
|
354
|
+
|
|
355
|
+
Yields:
|
|
356
|
+
TDataItem: Dictionary containing the ticket metric event data.
|
|
357
|
+
"""
|
|
358
|
+
# "https://example.zendesk.com/api/v2/incremental/ticket_metric_events?start_time=1332034771"
|
|
359
|
+
metric_event_pages = zendesk_client.get_pages(
|
|
360
|
+
"/api/v2/incremental/ticket_metric_events",
|
|
361
|
+
"ticket_metric_events",
|
|
362
|
+
PaginationType.CURSOR,
|
|
363
|
+
params={
|
|
364
|
+
"start_time": ensure_pendulum_datetime(time.last_value).int_timestamp,
|
|
365
|
+
},
|
|
366
|
+
)
|
|
367
|
+
for page in metric_event_pages:
|
|
368
|
+
yield page
|
|
369
|
+
|
|
370
|
+
if time.end_out_of_range:
|
|
371
|
+
return
|
|
372
|
+
|
|
373
|
+
def ticket_fields_table(zendesk_client: ZendeskAPIClient) -> Iterator[TDataItem]:
|
|
374
|
+
"""
|
|
375
|
+
Loads ticket fields data from Zendesk API.
|
|
376
|
+
|
|
377
|
+
Args:
|
|
378
|
+
zendesk_client: The Zendesk API client instance, used to make calls to Zendesk API.
|
|
379
|
+
|
|
380
|
+
Yields:
|
|
381
|
+
TDataItem: Dictionary containing the ticket fields data.
|
|
382
|
+
"""
|
|
383
|
+
# get dlt state
|
|
384
|
+
ticket_custom_fields = dlt.current.source_state().setdefault(
|
|
385
|
+
CUSTOM_FIELDS_STATE_KEY, {}
|
|
386
|
+
)
|
|
387
|
+
# get all custom fields and update state if needed, otherwise just load dicts into tables
|
|
388
|
+
all_fields = list(
|
|
389
|
+
chain.from_iterable(
|
|
390
|
+
zendesk_client.get_pages(
|
|
391
|
+
"/api/v2/ticket_fields.json", "ticket_fields", PaginationType.OFFSET
|
|
392
|
+
)
|
|
393
|
+
)
|
|
394
|
+
)
|
|
395
|
+
# all_fields = zendesk_client.ticket_fields()
|
|
396
|
+
for field in all_fields:
|
|
397
|
+
yield process_ticket_field(field, ticket_custom_fields)
|
|
398
|
+
|
|
399
|
+
def load_ticket_fields_state(
|
|
400
|
+
zendesk_client: ZendeskAPIClient,
|
|
401
|
+
) -> None:
|
|
402
|
+
for _ in ticket_fields_table(zendesk_client):
|
|
403
|
+
pass
|
|
404
|
+
|
|
405
|
+
ticket_fields_resource = dlt.resource(
|
|
406
|
+
name="ticket_fields", write_disposition="replace"
|
|
407
|
+
)(ticket_fields_table)
|
|
408
|
+
|
|
409
|
+
# Authenticate
|
|
410
|
+
zendesk_client = ZendeskAPIClient(credentials)
|
|
411
|
+
|
|
412
|
+
# loading base tables
|
|
413
|
+
resource_list = [
|
|
414
|
+
ticket_fields_resource(zendesk_client=zendesk_client),
|
|
415
|
+
ticket_events(zendesk_client=zendesk_client),
|
|
416
|
+
ticket_table(zendesk_client=zendesk_client, pivot_fields=pivot_ticket_fields),
|
|
417
|
+
ticket_metric_table(zendesk_client=zendesk_client),
|
|
418
|
+
]
|
|
419
|
+
|
|
420
|
+
# other tables to be loaded
|
|
421
|
+
resources_to_be_loaded = list(SUPPORT_ENDPOINTS) # make a copy
|
|
422
|
+
if load_all:
|
|
423
|
+
resources_to_be_loaded.extend(SUPPORT_EXTRA_ENDPOINTS)
|
|
424
|
+
for resource, endpoint_url, data_key, cursor_paginated in resources_to_be_loaded:
|
|
425
|
+
resource_list.append(
|
|
426
|
+
dlt.resource(
|
|
427
|
+
basic_resource(
|
|
428
|
+
zendesk_client, endpoint_url, data_key or resource, cursor_paginated
|
|
429
|
+
),
|
|
430
|
+
name=resource,
|
|
431
|
+
write_disposition="replace",
|
|
432
|
+
)
|
|
433
|
+
)
|
|
434
|
+
return resource_list
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def basic_resource(
|
|
438
|
+
zendesk_client: ZendeskAPIClient,
|
|
439
|
+
endpoint_url: str,
|
|
440
|
+
data_key: str,
|
|
441
|
+
cursor_paginated: bool,
|
|
442
|
+
) -> Iterator[TDataItem]:
|
|
443
|
+
"""
|
|
444
|
+
Basic loader for most endpoints offered by Zenpy. Supports pagination. Expects to be called as a DLT Resource.
|
|
445
|
+
|
|
446
|
+
Args:
|
|
447
|
+
zendesk_client: The Zendesk API client instance, used to make calls to Zendesk API.
|
|
448
|
+
resource: The Zenpy endpoint to retrieve data from, usually directly linked to a Zendesk API endpoint.
|
|
449
|
+
cursor_paginated: Tells to use CURSOR pagination or OFFSET/no pagination
|
|
450
|
+
|
|
451
|
+
Yields:
|
|
452
|
+
TDataItem: Dictionary containing the resource data.
|
|
453
|
+
"""
|
|
454
|
+
|
|
455
|
+
pages = zendesk_client.get_pages(
|
|
456
|
+
endpoint_url,
|
|
457
|
+
data_key,
|
|
458
|
+
PaginationType.CURSOR if cursor_paginated else PaginationType.OFFSET,
|
|
459
|
+
)
|
|
460
|
+
yield from pages
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Zendesk source helpers"""
|
|
2
|
+
|
|
3
|
+
from typing import List, Tuple
|
|
4
|
+
|
|
5
|
+
from dlt.common import pendulum
|
|
6
|
+
from dlt.common.time import timedelta
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def make_date_ranges(
|
|
10
|
+
start: pendulum.DateTime, end: pendulum.DateTime, step: timedelta
|
|
11
|
+
) -> List[Tuple[pendulum.DateTime, pendulum.DateTime]]:
|
|
12
|
+
"""Make tuples of (start, end) date ranges between the given `start` and `end` dates.
|
|
13
|
+
The last range in the resulting list will be capped to the value of `end` argument so it may be smaller than `step`
|
|
14
|
+
|
|
15
|
+
Example usage, create 1 week ranges between January 1st 2023 and today:
|
|
16
|
+
>>> make_date_ranges(pendulum.DateTime(2023, 1, 1).as_tz('UTC'), pendulum.today(), timedelta(weeks=1))
|
|
17
|
+
"""
|
|
18
|
+
ranges = []
|
|
19
|
+
while True:
|
|
20
|
+
end_time = min(start + step, end)
|
|
21
|
+
ranges.append((start, end_time))
|
|
22
|
+
if end_time == end:
|
|
23
|
+
break
|
|
24
|
+
start = end_time
|
|
25
|
+
return ranges
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from typing import Dict, Optional, TypedDict
|
|
2
|
+
|
|
3
|
+
from dlt.common import logger, pendulum
|
|
4
|
+
from dlt.common.time import ensure_pendulum_datetime
|
|
5
|
+
from dlt.common.typing import DictStrAny, DictStrStr, TDataItem
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TCustomFieldInfo(TypedDict):
|
|
9
|
+
title: str
|
|
10
|
+
options: DictStrStr
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _parse_date_or_none(value: Optional[str]) -> Optional[pendulum.DateTime]:
|
|
14
|
+
if not value:
|
|
15
|
+
return None
|
|
16
|
+
return ensure_pendulum_datetime(value)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def process_ticket(
|
|
20
|
+
ticket: DictStrAny,
|
|
21
|
+
custom_fields: Dict[str, TCustomFieldInfo],
|
|
22
|
+
pivot_custom_fields: bool = True,
|
|
23
|
+
) -> DictStrAny:
|
|
24
|
+
"""
|
|
25
|
+
Helper function that processes a ticket object and returns a dictionary of ticket data.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
ticket: The ticket dict object returned by a Zendesk API call.
|
|
29
|
+
custom_fields: A dictionary containing all the custom fields available for tickets.
|
|
30
|
+
pivot_custom_fields: A boolean indicating whether to pivot all custom fields or not.
|
|
31
|
+
Defaults to True.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
DictStrAny: A dictionary containing cleaned data about a ticket.
|
|
35
|
+
"""
|
|
36
|
+
# pivot custom field if indicated as such
|
|
37
|
+
# get custom fields
|
|
38
|
+
pivoted_fields = set()
|
|
39
|
+
for custom_field in ticket["custom_fields"]:
|
|
40
|
+
if pivot_custom_fields:
|
|
41
|
+
cus_field_id = str(custom_field["id"])
|
|
42
|
+
field = custom_fields.get(cus_field_id, None)
|
|
43
|
+
if field is None:
|
|
44
|
+
logger.warning(
|
|
45
|
+
"Custom field with ID %s does not exist in fields state. It may have been created after the pipeline run started.",
|
|
46
|
+
cus_field_id,
|
|
47
|
+
)
|
|
48
|
+
custom_field["ticket_id"] = ticket["id"]
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
pivoted_fields.add(cus_field_id)
|
|
52
|
+
field_name = field["title"]
|
|
53
|
+
current_value = custom_field["value"]
|
|
54
|
+
options = field["options"]
|
|
55
|
+
# Map dropdown values to labels
|
|
56
|
+
if not current_value or not options:
|
|
57
|
+
ticket[field_name] = current_value
|
|
58
|
+
elif isinstance(
|
|
59
|
+
current_value, list
|
|
60
|
+
): # Multiple choice field has a list of values
|
|
61
|
+
ticket[field_name] = [options.get(key, key) for key in current_value]
|
|
62
|
+
else:
|
|
63
|
+
ticket[field_name] = options.get(current_value)
|
|
64
|
+
else:
|
|
65
|
+
custom_field["ticket_id"] = ticket["id"]
|
|
66
|
+
# delete fields that are not needed for pivoting
|
|
67
|
+
if pivot_custom_fields:
|
|
68
|
+
ticket["custom_fields"] = [
|
|
69
|
+
f for f in ticket["custom_fields"] if str(f["id"]) not in pivoted_fields
|
|
70
|
+
]
|
|
71
|
+
if not ticket["custom_fields"]:
|
|
72
|
+
del ticket["custom_fields"]
|
|
73
|
+
del ticket["fields"]
|
|
74
|
+
# modify dates to return datetime objects instead
|
|
75
|
+
ticket["updated_at"] = _parse_date_or_none(ticket["updated_at"])
|
|
76
|
+
ticket["created_at"] = _parse_date_or_none(ticket["created_at"])
|
|
77
|
+
ticket["due_at"] = _parse_date_or_none(ticket["due_at"])
|
|
78
|
+
return ticket
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def process_ticket_field(
|
|
82
|
+
field: DictStrAny, custom_fields_state: Dict[str, TCustomFieldInfo]
|
|
83
|
+
) -> TDataItem:
|
|
84
|
+
"""Update custom field mapping in dlt state for the given field."""
|
|
85
|
+
# grab id and update state dict
|
|
86
|
+
# if the id is new, add a new key to indicate that this is the initial value for title
|
|
87
|
+
# New dropdown options are added to existing field but existing options are not changed
|
|
88
|
+
return_dict = field.copy()
|
|
89
|
+
field_id = str(field["id"])
|
|
90
|
+
|
|
91
|
+
options = field.get("custom_field_options", [])
|
|
92
|
+
new_options = {o["value"]: o["name"] for o in options}
|
|
93
|
+
existing_field = custom_fields_state.get(field_id)
|
|
94
|
+
if existing_field:
|
|
95
|
+
existing_options = existing_field["options"]
|
|
96
|
+
if return_options := return_dict.get("custom_field_options"):
|
|
97
|
+
for item in return_options:
|
|
98
|
+
item["name"] = existing_options.get(item["value"], item["name"])
|
|
99
|
+
for key, value in new_options.items():
|
|
100
|
+
if key not in existing_options:
|
|
101
|
+
existing_options[key] = value
|
|
102
|
+
else:
|
|
103
|
+
custom_fields_state[field_id] = dict(title=field["title"], options=new_options)
|
|
104
|
+
return_dict["initial_title"] = field["title"]
|
|
105
|
+
return return_dict
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module handles how credentials are read in dlt sources
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import ClassVar, List, Union
|
|
6
|
+
|
|
7
|
+
import dlt
|
|
8
|
+
from dlt.common.configuration import configspec
|
|
9
|
+
from dlt.common.configuration.specs import CredentialsConfiguration
|
|
10
|
+
from dlt.common.typing import TSecretValue
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@configspec
|
|
14
|
+
class ZendeskCredentialsBase(CredentialsConfiguration):
|
|
15
|
+
"""
|
|
16
|
+
The Base version of all the ZendeskCredential classes.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
subdomain: str = dlt.config.value
|
|
20
|
+
__config_gen_annotations__: ClassVar[List[str]] = []
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@configspec
|
|
24
|
+
class ZendeskCredentialsEmailPass(ZendeskCredentialsBase):
|
|
25
|
+
"""
|
|
26
|
+
This class is used to store credentials for Email + Password Authentication
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
email: str = dlt.config.value
|
|
30
|
+
password: TSecretValue = dlt.secrets.value
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@configspec
|
|
34
|
+
class ZendeskCredentialsOAuth(ZendeskCredentialsBase):
|
|
35
|
+
"""
|
|
36
|
+
This class is used to store credentials for OAuth Token Authentication
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
oauth_token: TSecretValue = dlt.secrets.value
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@configspec
|
|
43
|
+
class ZendeskCredentialsToken(ZendeskCredentialsBase):
|
|
44
|
+
"""
|
|
45
|
+
This class is used to store credentials for Token Authentication
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
email: str = dlt.config.value
|
|
49
|
+
token: TSecretValue = dlt.secrets.value
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
TZendeskCredentials = Union[
|
|
53
|
+
ZendeskCredentialsEmailPass, ZendeskCredentialsToken, ZendeskCredentialsOAuth
|
|
54
|
+
]
|