Skip to content

Commit 120f118

Browse files
End date configuration for tiktok (#12838)
* added end date configuration. * restore and added stream_test in unit test * updated spec.json * updated with master * spec.json updated * updated streams_test.py * updated spec.json with new end date * updated in unit test file * stream test updated * fix: formatting and spec * chore: updated the source-pipedrive version * fix: formatting and spec * fix: formatting and spec * fix: formatting and spec Co-authored-by: Harshith Mullapudi <[email protected]>
1 parent 9d99688 commit 120f118

File tree

9 files changed

+48
-17
lines changed

9 files changed

+48
-17
lines changed

airbyte-config/init/src/main/resources/seed/source_definitions.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@
914914
- name: TikTok Marketing
915915
sourceDefinitionId: 4bfac00d-ce15-44ff-95b9-9e3c3e8fbd35
916916
dockerRepository: airbyte/source-tiktok-marketing
917-
dockerImageTag: 0.1.10
917+
dockerImageTag: 0.1.11
918918
documentationUrl: https://docs.airbyte.io/integrations/sources/tiktok-marketing
919919
icon: tiktok.svg
920920
sourceType: api

airbyte-integrations/connectors/source-tiktok-marketing/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ COPY source_tiktok_marketing ./source_tiktok_marketing
3232
ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py"
3333
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]
3434

35-
LABEL io.airbyte.version=0.1.10
35+
LABEL io.airbyte.version=0.1.11
3636
LABEL io.airbyte.name=airbyte/source-tiktok-marketing

airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/spec.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,28 @@
8484
"enum": ["LIFETIME", "DAY", "HOUR"],
8585
"order": 2,
8686
"type": "string"
87+
},
88+
"end_date": {
89+
"title": "End Date",
90+
"description": "The date until which you'd like to replicate data for all incremental streams, in the format YYYY-MM-DD. All data generated between start_date and this date will be replicated. Not setting this option will result in always syncing the data till the current date.",
91+
"default": "2022-05-23",
92+
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
93+
"order": 3,
94+
"type": "string"
8795
}
8896
}
8997
},
9098
"supportsIncremental": true,
99+
"supportsNormalization": false,
100+
"supportsDBT": false,
91101
"supported_destination_sync_modes": ["overwrite", "append", "append_dedup"],
102+
"authSpecification": null,
92103
"advanced_auth": {
93104
"auth_flow_type": "oauth2.0",
94105
"predicate_key": ["credentials", "auth_type"],
95106
"predicate_value": "oauth2.0",
96107
"oauth_config_specification": {
108+
"oauth_user_input_from_connector_config_specification": null,
97109
"complete_oauth_output_specification": {
98110
"title": "CompleteOauthOutputSpecification",
99111
"type": "object",
@@ -110,14 +122,8 @@
110122
"title": "CompleteOauthServerInputSpecification",
111123
"type": "object",
112124
"properties": {
113-
"app_id": {
114-
"title": "App Id",
115-
"type": "string"
116-
},
117-
"secret": {
118-
"title": "Secret",
119-
"type": "string"
120-
}
125+
"app_id": { "title": "App Id", "type": "string" },
126+
"secret": { "title": "Secret", "type": "string" }
121127
},
122128
"required": ["app_id", "secret"]
123129
},

airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/source.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
SourceTiktokMarketingSpec,
1919
)
2020
from .streams import (
21+
DEFAULT_END_DATE,
2122
DEFAULT_START_DATE,
2223
AdGroupAudienceReports,
2324
AdGroups,
@@ -93,6 +94,7 @@ def _prepare_stream_args(config: Mapping[str, Any]) -> Mapping[str, Any]:
9394
return {
9495
"authenticator": TiktokTokenAuthenticator(access_token),
9596
"start_date": config.get("start_date") or DEFAULT_START_DATE,
97+
"end_date": config.get("end_date") or DEFAULT_END_DATE,
9698
"advertiser_id": advertiser_id,
9799
"app_id": app_id,
98100
"secret": secret,

airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/spec.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from jsonschema import RefResolver
1111
from pydantic import BaseModel, Field
1212

13-
from .streams import DEFAULT_START_DATE, ReportGranularity
13+
from .streams import DEFAULT_END_DATE, DEFAULT_START_DATE, ReportGranularity
1414

1515

1616
class OauthCredSpec(BaseModel):
@@ -62,6 +62,18 @@ class Config:
6262
order=2,
6363
)
6464

65+
end_date: str = Field(
66+
title="End Date",
67+
default=DEFAULT_END_DATE,
68+
pattern="^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
69+
description=(
70+
"The date until which you'd like to replicate data for all incremental streams, in the format YYYY-MM-DD. "
71+
"All data generated between start_date and this date will be replicated. "
72+
"Not setting this option will result in always syncing the data till the current date."
73+
),
74+
order=3,
75+
)
76+
6577
@classmethod
6678
def change_format_to_oneOf(cls, schema: dict) -> dict:
6779
new_schema = {}

airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/streams.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
# TikTok Initial release date is September 2016
2424
DEFAULT_START_DATE = "2016-09-01"
25+
DEFAULT_END_DATE = str(datetime.now().date())
2526
NOT_AUDIENCE_METRICS = [
2627
"reach",
2728
"cost_per_1000_reached",
@@ -226,12 +227,15 @@ def transform_function(original_value: Any, field_schema: Dict[str, Any]) -> Any
226227
return Decimal(original_value)
227228
return original_value
228229

229-
def __init__(self, start_date: str, **kwargs):
230+
def __init__(self, start_date: str, end_date: str, **kwargs):
230231
super().__init__(**kwargs)
231232
self.kwargs = kwargs
232233
# convert a start date to TikTok format
233234
# example: "2021-08-24" => "2021-08-24 00:00:00"
234235
self._start_time = pendulum.parse(start_date or DEFAULT_START_DATE).strftime("%Y-%m-%d 00:00:00")
236+
# convert end date to TikTok format
237+
# example: "2021-08-24" => "2021-08-24 00:00:00"
238+
self._end_time = pendulum.parse(end_date or DEFAULT_END_DATE).strftime("%Y-%m-%d 00:00:00")
235239
self.max_cursor_date = None
236240
self._advertiser_ids = []
237241

@@ -422,7 +426,9 @@ def cursor_field(self):
422426
return []
423427

424428
@staticmethod
425-
def _get_time_interval(start_date: Union[datetime, str], granularity: ReportGranularity) -> Iterable[Tuple[datetime, datetime]]:
429+
def _get_time_interval(
430+
start_date: Union[datetime, str], ending_date: Union[datetime, str], granularity: ReportGranularity
431+
) -> Iterable[Tuple[datetime, datetime]]:
426432
"""Due to time range restrictions based on the level of granularity of reports, we have to chunk API calls in order
427433
to get the desired time range.
428434
Docs: https://ads.tiktok.com/marketing_api/docs?id=1714590313280513
@@ -432,7 +438,7 @@ def _get_time_interval(start_date: Union[datetime, str], granularity: ReportGran
432438
"""
433439
if isinstance(start_date, str):
434440
start_date = pendulum.parse(start_date)
435-
end_date = pendulum.now()
441+
end_date = pendulum.parse(ending_date) if ending_date else pendulum.now()
436442

437443
# Snapchat API only allows certain amount of days of data based on the reporting granularity
438444
if granularity == ReportGranularity.DAY:
@@ -530,9 +536,10 @@ def _get_metrics(self):
530536

531537
def stream_slices(self, stream_state: Mapping[str, Any] = None, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
532538
stream_start = self.select_cursor_field_value(stream_state) or self._start_time
539+
stream_end = self._end_time
533540

534541
for slice_adv_id in super().stream_slices(**kwargs):
535-
for start_date, end_date in self._get_time_interval(stream_start, self.report_granularity):
542+
for start_date, end_date in self._get_time_interval(stream_start, stream_end, self.report_granularity):
536543
slice = {
537544
"advertiser_id": slice_adv_id["advertiser_id"],
538545
"start_date": start_date.strftime("%Y-%m-%d"),

airbyte-integrations/connectors/source-tiktok-marketing/unit_tests/streams_test.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"secret": "secret",
2727
"authenticator": None,
2828
"start_date": START_DATE,
29+
"end_date": END_DATE,
2930
"app_id": 1234,
3031
"advertiser_id": 0,
3132
}
@@ -34,6 +35,7 @@
3435
"secret": "secret",
3536
"authenticator": None,
3637
"start_date": START_DATE,
38+
"end_date": END_DATE,
3739
"app_id": 1234,
3840
"advertiser_id": 2000,
3941
}
@@ -62,13 +64,13 @@ def advertiser_ids_fixture():
6264
],
6365
)
6466
def test_get_time_interval(pendulum_now_mock, granularity, intervals_len):
65-
intervals = BasicReports._get_time_interval(start_date="2020-01-01", granularity=granularity)
67+
intervals = BasicReports._get_time_interval(start_date="2020-01-01", ending_date="2020-03-01", granularity=granularity)
6668
assert len(list(intervals)) == intervals_len
6769

6870

6971
@patch.object(pendulum, "now", return_value=pendulum.parse("2018-12-25"))
7072
def test_get_time_interval_past(pendulum_now_mock_past):
71-
intervals = BasicReports._get_time_interval(start_date="2020-01-01", granularity=ReportGranularity.DAY)
73+
intervals = BasicReports._get_time_interval(start_date="2020-01-01", ending_date="2020-01-01", granularity=ReportGranularity.DAY)
7274
assert len(list(intervals)) == 1
7375

7476

airbyte-integrations/connectors/source-tiktok-marketing/unit_tests/unit_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_random_items(prepared_prod_args):
112112
)
113113
if not max_updated_value or max_updated_value < ad_items[-1][stream.cursor_field]:
114114
max_updated_value = ad_items[-1][stream.cursor_field]
115+
115116
# mock for ads
116117
for page, page_response in generate_pages(items=ad_items, page_size=page_size, last_empty=True):
117118
uri = f"/open_api/v1.2/ad/get/?page_size={page_size}&advertiser_id={advertiser_id}"

docs/integrations/sources/tiktok-marketing.md

+1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ The connector is restricted by [requests limitation](https://ads.tiktok.com/mark
524524

525525
| Version | Date | Pull Request | Subject |
526526
|:--------|:-----------|:---------------------------------------------------------|:----------------------------------------------------------------------------------------------|
527+
| 0.1.11 | 2022-04-27 | [12838](https://github.com/airbytehq/airbyte/pull/12838) | Added end date configuration for tiktok |
527528
| 0.1.10 | 2022-05-07 | [12545](https://github.com/airbytehq/airbyte/pull/12545) | Removed odd production authenication method |
528529
| 0.1.9 | 2022-04-30 | [12500](https://github.com/airbytehq/airbyte/pull/12500) | Improve input configuration copy |
529530
| 0.1.8 | 2022-04-28 | [12435](https://github.com/airbytehq/airbyte/pull/12435) | updated spec descriptions |

0 commit comments

Comments
 (0)