diff --git a/.github/workflows/publish-command.yml b/.github/workflows/publish-command.yml index 931b45c102af..ca01488dfebb 100644 --- a/.github/workflows/publish-command.yml +++ b/.github/workflows/publish-command.yml @@ -187,6 +187,8 @@ jobs: SOURCE_BAMBOO_HR_CREDS: ${{ secrets.SOURCE_BAMBOO_HR_CREDS }} SOURCE_LINKEDIN_ADS_TEST_CREDS: ${{ secrets.SOURCE_LINKEDIN_ADS_TEST_CREDS }} SOURCE_BIGCOMMERCE_CREDS: ${{ secrets.SOURCE_BIGCOMMERCE_CREDS }} + SOURCE_TIKTOK_MARKETING_TEST_CREDS: ${{ secrets.SOURCE_TIKTOK_MARKETING_TEST_CREDS }} + SOURCE_TIKTOK_MARKETING_PROD_TEST_CREDS: ${{ secrets.SOURCE_TIKTOK_MARKETING_PROD_TEST_CREDS }} DESTINATION_DATABRICKS_CREDS: ${{ secrets.DESTINATION_DATABRICKS_CREDS }} MONGODB_TEST_CREDS: ${{ secrets.MONGODB_TEST_CREDS }} - run: | diff --git a/.github/workflows/test-command.yml b/.github/workflows/test-command.yml index 657eccd72ef6..d5818495bf1a 100644 --- a/.github/workflows/test-command.yml +++ b/.github/workflows/test-command.yml @@ -182,6 +182,8 @@ jobs: SOURCE_BAMBOO_HR_CREDS: ${{ secrets.SOURCE_BAMBOO_HR_CREDS }} SOURCE_LINKEDIN_ADS_TEST_CREDS: ${{ secrets.SOURCE_LINKEDIN_ADS_TEST_CREDS }} SOURCE_BIGCOMMERCE_CREDS: ${{ secrets.SOURCE_BIGCOMMERCE_CREDS }} + SOURCE_TIKTOK_MARKETING_TEST_CREDS: ${{ secrets.SOURCE_TIKTOK_MARKETING_TEST_CREDS }} + SOURCE_TIKTOK_MARKETING_PROD_TEST_CREDS: ${{ secrets.SOURCE_TIKTOK_MARKETING_PROD_TEST_CREDS }} DESTINATION_DATABRICKS_CREDS: ${{ secrets.DESTINATION_DATABRICKS_CREDS }} MONGODB_TEST_CREDS: ${{ secrets.MONGODB_TEST_CREDS }} - run: | diff --git a/airbyte-config/init/src/main/resources/config/STANDARD_SOURCE_DEFINITION/4bfac00d-ce15-44ff-95b9-9e3c3e8fbd35.json b/airbyte-config/init/src/main/resources/config/STANDARD_SOURCE_DEFINITION/4bfac00d-ce15-44ff-95b9-9e3c3e8fbd35.json new file mode 100644 index 000000000000..af48c7ad5ef6 --- /dev/null +++ b/airbyte-config/init/src/main/resources/config/STANDARD_SOURCE_DEFINITION/4bfac00d-ce15-44ff-95b9-9e3c3e8fbd35.json @@ -0,0 +1,7 @@ +{ + "sourceDefinitionId": "4bfac00d-ce15-44ff-95b9-9e3c3e8fbd35", + "name": "TikTok Marketing", + "dockerRepository": "airbyte/source-tiktok-marketing", + "dockerImageTag": "0.1.0", + "documentationUrl": "https://docs.airbyte.io/integrations/sources/tiktok-marketing" +} diff --git a/airbyte-config/init/src/main/resources/seed/source_definitions.yaml b/airbyte-config/init/src/main/resources/seed/source_definitions.yaml index cac1c3ed7e46..55249614e078 100644 --- a/airbyte-config/init/src/main/resources/seed/source_definitions.yaml +++ b/airbyte-config/init/src/main/resources/seed/source_definitions.yaml @@ -311,6 +311,12 @@ dockerImageTag: 0.2.3 documentationUrl: https://docs.airbyte.io/integrations/sources/tempo sourceType: api +- sourceDefinitionId: 4bfac00d-ce15-44ff-95b9-9e3c3e8fbd35 + name: TikTok Marketing + dockerRepository: airbyte/source-tiktok-marketing + dockerImageTag: 0.1.0 + documentationUrl: https://docs.airbyte.io/integrations/sources/tiktok-marketing + sourceType: api - sourceDefinitionId: 8da67652-004c-11ec-9a03-0242ac130003 name: Trello dockerRepository: airbyte/source-trello diff --git a/airbyte-integrations/builds.md b/airbyte-integrations/builds.md index fb11a5ba0a6e..f3d934d438ac 100644 --- a/airbyte-integrations/builds.md +++ b/airbyte-integrations/builds.md @@ -76,6 +76,7 @@ | Square | [![source-square](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-square%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-square) | | Stripe | [![source-stripe](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-stripe%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-stripe) | | Tempo | [![source-tempo](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-tempo%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-tempo) | +| TikTok Marketing | [![source-tiktok-marketing](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-tiktok-marketing%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-tiktok-marketing) | | Trello | [![source-trello](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-trello%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-trello) | | Twilio | [![source-twilio](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-twilio%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-twilio) | | Typeform | [![source-typeform](https://img.shields.io/endpoint?url=https%3A%2F%2Fdnsgjos7lj2fu.cloudfront.net%2Ftests%2Fsummary%2Fsource-typeform%2Fbadge.json)](https://dnsgjos7lj2fu.cloudfront.net/tests/summary/source-typeform) | diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/.dockerignore b/airbyte-integrations/connectors/source-tiktok-marketing/.dockerignore new file mode 100644 index 000000000000..beb044a97325 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/.dockerignore @@ -0,0 +1,7 @@ +* +!Dockerfile +!Dockerfile.test +!main.py +!source_tiktok_marketing +!setup.py +!secrets diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/Dockerfile b/airbyte-integrations/connectors/source-tiktok-marketing/Dockerfile new file mode 100644 index 000000000000..7914f31d8397 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/Dockerfile @@ -0,0 +1,36 @@ +FROM python:3.7.11-alpine3.14 as base + +# build and load all requirements +FROM base as builder +WORKDIR /airbyte/integration_code + +# upgrade pip to the latest version +RUN apk --no-cache upgrade \ + && pip install --upgrade pip \ + && apk --no-cache add tzdata build-base + +COPY setup.py ./ +# install necessary packages to a temporary folder +RUN pip install --prefix=/install . + +# build a clean environment +FROM base +WORKDIR /airbyte/integration_code + +# copy all loaded and built libraries to a pure basic image +COPY --from=builder /install /usr/local +# add default timezone settings +COPY --from=builder /usr/share/zoneinfo/Etc/UTC /etc/localtime +RUN echo "Etc/UTC" > /etc/timezone +# Bash is installed for more convenient debugging. +RUN apk --no-cache add bash + +# copy payload code only +COPY main.py ./ +COPY source_tiktok_marketing ./source_tiktok_marketing + +ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py" +ENTRYPOINT ["python", "/airbyte/integration_code/main.py"] + +LABEL io.airbyte.version=0.1.0 +LABEL io.airbyte.name=airbyte/source-tiktok-marketing diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/README.md b/airbyte-integrations/connectors/source-tiktok-marketing/README.md new file mode 100644 index 000000000000..c92c4c9f1f91 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/README.md @@ -0,0 +1,129 @@ +# Tiktok Marketing Source + +This is the repository for the Tiktok Marketing source connector, written in Python. +For information about how to use this connector within Airbyte, see [the documentation](https://docs.airbyte.io/integrations/sources/tiktok-marketing). + +## Local development + +### Prerequisites +**To iterate on this connector, make sure to complete this prerequisites section.** + +#### Minimum Python version required `= 3.7.0` + +#### Build & Activate Virtual Environment and install dependencies +From this connector directory, create a virtual environment: +``` +python -m venv .venv +``` + +This will generate a virtualenv for this module in `.venv/`. Make sure this venv is active in your +development environment of choice. To activate it from the terminal, run: +``` +source .venv/bin/activate +pip install -r requirements.txt +``` +If you are in an IDE, follow your IDE's instructions to activate the virtualenv. + +Note that while we are installing dependencies from `requirements.txt`, you should only edit `setup.py` for your dependencies. `requirements.txt` is +used for editable installs (`pip install -e`) to pull in Python dependencies from the monorepo and will call `setup.py`. +If this is mumbo jumbo to you, don't worry about it, just put your deps in `setup.py` but install using `pip install -r requirements.txt` and everything +should work as you expect. + +#### Building via Gradle +From the Airbyte repository root, run: +``` +./gradlew :airbyte-integrations:connectors:source-tiktok-marketing:build +``` + +#### Create credentials +**If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/sources/tiktok-marketing) +to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `source_tiktok_marketing/spec.json` file. +Note that the `secrets` directory is gitignored by default, so there is no danger of accidentally checking in sensitive information. +See `integration_tests/sample_config.json` for a sample config file. + +**If you are an Airbyte core member**, copy the credentials in Lastpass under the secret name `source tiktok-marketing test creds` +and place them into `secrets/config.json`. + +### Locally running the connector +``` +python main.py spec +python main.py check --config secrets/config.json +python main.py discover --config secrets/config.json +python main.py read --config secrets/config.json --catalog integration_tests/configured_catalog.json +``` + +### Locally running the connector docker image + +#### Build +First, make sure you build the latest Docker image: +``` +docker build . -t airbyte/source-tiktok-marketing:dev +``` + +You can also build the connector image via Gradle: +``` +./gradlew :airbyte-integrations:connectors:source-tiktok-marketing:airbyteDocker +``` +When building via Gradle, the docker image name and tag, respectively, are the values of the `io.airbyte.name` and `io.airbyte.version` `LABEL`s in +the Dockerfile. + +#### Run +Then run any of the connector commands as follows: +``` +docker run --rm airbyte/source-tiktok-marketing:dev spec +docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-tiktok-marketing:dev check --config /secrets/config.json +docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-tiktok-marketing:dev discover --config /secrets/config.json +docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/integration_tests:/integration_tests airbyte/source-tiktok-marketing:dev read --config /secrets/config.json --catalog /integration_tests/configured_catalog.json +``` +## Testing + Make sure to familiarize yourself with [pytest test discovery](https://docs.pytest.org/en/latest/goodpractices.html#test-discovery) to know how your test files and methods should be named. +First install test dependencies into your virtual environment: +``` +pip install .[tests] +``` +### Unit Tests +To run unit tests locally, from the connector directory run: +``` +python -m pytest unit_tests +``` + +### Integration Tests +There are two types of integration tests: Acceptance Tests (Airbyte's test suite for all source connectors) and custom integration tests (which are specific to this connector). +#### Custom Integration tests +Place custom tests inside `integration_tests/` folder, then, from the connector root, run +``` +python -m pytest integration_tests +``` +#### Acceptance Tests +Customize `acceptance-test-config.yml` file to configure tests. See [Source Acceptance Tests](https://docs.airbyte.io/connector-development/testing-connectors/source-acceptance-tests-reference) for more information. +If your connector requires to create or destroy resources for use during acceptance tests create fixtures for it and place them inside integration_tests/acceptance.py. +To run your integration tests with acceptance tests, from the connector root, run +``` +python -m pytest integration_tests -p integration_tests.acceptance +``` +To run your integration tests with docker + +### Using gradle to run tests +All commands should be run from airbyte project root. +To run unit tests: +``` +./gradlew :airbyte-integrations:connectors:source-tiktok-marketing:unitTest +``` +To run acceptance and custom integration tests: +``` +./gradlew :airbyte-integrations:connectors:source-tiktok-marketing:integrationTest +``` + +## Dependency Management +All of your dependencies should go in `setup.py`, NOT `requirements.txt`. The requirements file is only used to connect internal Airbyte dependencies in the monorepo for local development. +We split dependencies between two groups, dependencies that are: +* required for your connector to work need to go to `MAIN_REQUIREMENTS` list. +* required for the testing need to go to `TEST_REQUIREMENTS` list + +### Publishing a new version of the connector +You've checked out the repo, implemented a million dollar feature, and you're ready to share your changes with the world. Now what? +1. Make sure your changes are passing unit and integration tests. +1. Bump the connector version in `Dockerfile` -- just increment the value of the `LABEL io.airbyte.version` appropriately (we use [SemVer](https://semver.org/)). +1. Create a Pull Request. +1. Pat yourself on the back for being an awesome contributor. +1. Someone from Airbyte will take a look at your PR and iterate with you to merge it into master. diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/acceptance-test-config.yml b/airbyte-integrations/connectors/source-tiktok-marketing/acceptance-test-config.yml new file mode 100644 index 000000000000..ae62790c8f57 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/acceptance-test-config.yml @@ -0,0 +1,36 @@ +# See [Source Acceptance Tests](https://docs.airbyte.io/connector-development/testing-connectors/source-acceptance-tests-reference) +# for more information about how to configure these tests + +connector_image: airbyte/source-tiktok-marketing:dev +tests: + spec: + - spec_path: "integration_tests/spec.json" + connection: + - config_path: "secrets/prod_config.json" + status: "succeed" + - config_path: "secrets/config.json" + status: "succeed" + - config_path: "integration_tests/invalid_config.json" + status: "failed" + discovery: + - config_path: "secrets/config.json" + - config_path: "secrets/prod_config.json" + basic_read: + - config_path: "secrets/config.json" + configured_catalog_path: "integration_tests/configured_catalog.json" + empty_streams: ["ads"] + - config_path: "secrets/prod_config.json" + configured_catalog_path: "integration_tests/configured_catalog.json" + empty_streams: [] + incremental: + - config_path: "secrets/config.json" + configured_catalog_path: "integration_tests/configured_catalog.json" + future_state_path: "integration_tests/abnormal_state.json" + - config_path: "secrets/prod_config.json" + configured_catalog_path: "integration_tests/configured_catalog.json" + future_state_path: "integration_tests/abnormal_state.json" + full_refresh: + - config_path: "secrets/config.json" + configured_catalog_path: "integration_tests/configured_catalog.json" + - config_path: "secrets/prod_config.json" + configured_catalog_path: "integration_tests/configured_catalog.json" diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/acceptance-test-docker.sh b/airbyte-integrations/connectors/source-tiktok-marketing/acceptance-test-docker.sh new file mode 100644 index 000000000000..c51577d10690 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/acceptance-test-docker.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env sh + +# Build latest connector image +docker build . -t $(cat acceptance-test-config.yml | grep "connector_image" | head -n 1 | cut -d: -f2-) + +# Pull latest acctest image +docker pull airbyte/source-acceptance-test:latest + +# Run +docker run --rm -it \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v /tmp:/tmp \ + -v $(pwd):/test_input \ + airbyte/source-acceptance-test \ + --acceptance-test-config /test_input + diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/bootstrap.md b/airbyte-integrations/connectors/source-tiktok-marketing/bootstrap.md new file mode 100644 index 000000000000..5dccedf09b5e --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/bootstrap.md @@ -0,0 +1,15 @@ +The Business Marketing API is [a REST based API](https://business-api.tiktok.com/marketing_api/docs?rid=88iodtuzdt7&id=1701890905779201). Connector is implemented with [Airbyte CDK](https://docs.airbyte.io/connector-development/cdk-python). +This service also provides a [sandbox](https://business-api.tiktok.com/marketing_api/docs?rid=88iodtuzdt7&id=1701890920013825) environment for testing with some limitations. + +## Core Advertiser stream +The basic entity is 'advertiser'. All other streams use this required parameter for data loading. This works slightly differently between sandbox and production environments. For production, every developer application can have multiple advertisers. [This endpoint](https://business-api.tiktok.com/marketing_api/docs?id=1708503202263042) gets a list of advertiser accounts that authorized an app, providing us functionality to obtain the associated advertisers. However, this endpoint is inaccessible for sandbox because a sandbox can have only one advertiser object and its ID is known in advance. + +## Other streams +* [Campaigns](https://business-api.tiktok.com/marketing_api/docs?id=1708582970809346) \(Incremental\) +* [Ad Groups](https://business-api.tiktok.com/marketing_api/docs?id=1708503489590273)\(Incremental\) +* [Ads](https://business-api.tiktok.com/marketing_api/docs?id=1708572923161602)\(Incremental\) + +Dependent streams have required parameter advertiser_id. +As cursor field this connector uses "modify_time" values. But endpoints don't provide any mechanism for correct data filtering and sorting thus for incremental sync this connector tries to load all data and to validate a cursor field value on own side. + + diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/build.gradle b/airbyte-integrations/connectors/source-tiktok-marketing/build.gradle new file mode 100644 index 000000000000..9df4b3767ace --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/build.gradle @@ -0,0 +1,14 @@ +plugins { + id 'airbyte-python' + id 'airbyte-docker' + id 'airbyte-source-acceptance-test' +} + +airbytePython { + moduleDirectory 'source_tiktok_marketing_singer' +} + +dependencies { + implementation files(project(':airbyte-integrations:bases:source-acceptance-test').airbyteDocker.outputs) + implementation files(project(':airbyte-integrations:bases:base-python').airbyteDocker.outputs) +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/__init__.py b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/abnormal_state.json b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/abnormal_state.json new file mode 100644 index 000000000000..d5682cee4f4d --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/abnormal_state.json @@ -0,0 +1,11 @@ +{ + "ads": { + "modify_time": "2030-08-30 09:16:10" + }, + "ad_groups": { + "modify_time": "2030-08-30 09:16:10" + }, + "campaigns": { + "modify_time": "2030-08-30 09:16:10" + } +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/acceptance.py b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/acceptance.py new file mode 100644 index 000000000000..108075487440 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/acceptance.py @@ -0,0 +1,14 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + + +import pytest + +pytest_plugins = ("source_acceptance_test.plugin",) + + +@pytest.fixture(scope="session", autouse=True) +def connector_setup(): + """ This fixture is a placeholder for external resources that acceptance test might require.""" + yield diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/configured_catalog.json b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/configured_catalog.json new file mode 100644 index 000000000000..e481c1c0c1fa --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/configured_catalog.json @@ -0,0 +1,50 @@ +{ + "streams": [ + { + "stream": { + "name": "ads", + "json_schema": {}, + "supported_sync_modes": ["full_refresh", "incremental"], + "source_defined_cursor": true, + "default_cursor_field": ["modify_time"], + "source_defined_primary_key": [["ad_id"]] + }, + "sync_mode": "incremental", + "destination_sync_mode": "append" + }, + { + "stream": { + "name": "advertisers", + "json_schema": {}, + "supported_sync_modes": ["full_refresh"], + "source_defined_primary_key": [["id"]] + }, + "sync_mode": "full_refresh", + "destination_sync_mode": "append" + }, + { + "stream": { + "name": "ad_groups", + "json_schema": {}, + "supported_sync_modes": ["full_refresh", "incremental"], + "source_defined_cursor": true, + "default_cursor_field": ["modify_time"], + "source_defined_primary_key": [["adgroup_id"]] + }, + "sync_mode": "incremental", + "destination_sync_mode": "append" + }, + { + "stream": { + "name": "campaigns", + "json_schema": {}, + "supported_sync_modes": ["full_refresh", "incremental"], + "source_defined_cursor": true, + "default_cursor_field": ["modify_time"], + "source_defined_primary_key": [["campaign_id"]] + }, + "sync_mode": "incremental", + "destination_sync_mode": "append" + } + ] +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/invalid_config.json b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/invalid_config.json new file mode 100644 index 000000000000..6611bf5ca860 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/invalid_config.json @@ -0,0 +1,7 @@ +{ + "access_token": "", + "start_date": "2021-01-01", + "environment": { + "advertiser_id": "11111111" + } +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/spec.json b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/spec.json new file mode 100644 index 000000000000..bba2d6cf23d9 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/integration_tests/spec.json @@ -0,0 +1,73 @@ +{ + "documentationUrl": "https://docs.airbyte.io/integrations/sources/tiktok-marketing", + "changelogUrl": "https://docs.airbyte.io/integrations/sources/tiktok-marketing", + "connectionSpecification": { + "title": "TikTok Marketing Source Spec", + "type": "object", + "properties": { + "environment": { + "title": "Environment", + "default": "Production", + "oneOf": [ + { + "title": "Production", + "type": "object", + "properties": { + "environment": { + "title": "Environment", + "const": "prod", + "type": "string" + }, + "app_id": { + "title": "App Id", + "description": "The App id applied by the developer.", + "type": "string" + }, + "secret": { + "title": "Secret", + "description": "The private key of the developer's application.", + "airbyte_secret": true, + "type": "string" + } + }, + "required": ["app_id", "secret"] + }, + { + "title": "Sandbox", + "type": "object", + "properties": { + "environment": { + "title": "Environment", + "const": "sandbox", + "type": "string" + }, + "advertiser_id": { + "title": "Advertiser Id", + "description": "The Advertiser ID which generated for the developer's Sandbox application.", + "type": "string" + } + }, + "required": ["advertiser_id"] + } + ], + "type": "object" + }, + "access_token": { + "title": "Access Token", + "description": "Long-term Authorized Access Token.", + "airbyte_secret": true, + "type": "string" + }, + "start_date": { + "title": "Start Date", + "description": "Start Date in format: YYYY-MM-DD.", + "default": "01-09-2016", + "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$", + "type": "string" + } + }, + "required": ["access_token"] + }, + "supportsIncremental": true, + "supported_destination_sync_modes": ["overwrite", "append", "append_dedup"] +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/main.py b/airbyte-integrations/connectors/source-tiktok-marketing/main.py new file mode 100644 index 000000000000..8829e8b49a65 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/main.py @@ -0,0 +1,13 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + + +import sys + +from airbyte_cdk.entrypoint import launch +from source_tiktok_marketing import SourceTiktokMarketing + +if __name__ == "__main__": + source = SourceTiktokMarketing() + launch(source, sys.argv[1:]) diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/requirements.txt b/airbyte-integrations/connectors/source-tiktok-marketing/requirements.txt new file mode 100644 index 000000000000..7be17a56d745 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/requirements.txt @@ -0,0 +1,3 @@ +# This file is autogenerated -- only edit if you know what you are doing. Use setup.py for declaring dependencies. +-e ../../bases/source-acceptance-test +-e . diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/setup.py b/airbyte-integrations/connectors/source-tiktok-marketing/setup.py new file mode 100644 index 000000000000..d49086e026b9 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/setup.py @@ -0,0 +1,26 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + + +from setuptools import find_packages, setup + +MAIN_REQUIREMENTS = [ + "airbyte-cdk~=0.1.24", +] + +TEST_REQUIREMENTS = ["pytest~=6.1", "source-acceptance-test", "requests-mock==1.9.3", "timeout-decorator==0.5.0"] + + +setup( + name="source_tiktok_marketing", + description="Source implementation for Tiktok Marketing.", + author="Airbyte", + author_email="contact@airbyte.io", + packages=find_packages(), + install_requires=MAIN_REQUIREMENTS, + package_data={"": ["*.json"]}, + extras_require={ + "tests": TEST_REQUIREMENTS, + }, +) diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/__init__.py b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/__init__.py new file mode 100644 index 000000000000..474fce2f19f3 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/__init__.py @@ -0,0 +1,26 @@ +# MIT License +# +# Copyright (c) 2020 Airbyte +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +from .source import SourceTiktokMarketing + +__all__ = ["SourceTiktokMarketing"] diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/ad_groups.json b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/ad_groups.json new file mode 100644 index 000000000000..9fe56aff268a --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/ad_groups.json @@ -0,0 +1,376 @@ +{ + "type": ["null", "object"], + "properties": { + "adgroup_id": { + "type": "integer" + }, + "campaign_id": { + "type": "integer" + }, + "advertiser_id": { + "type": "integer" + }, + "adgroup_name": { + "type": "string" + }, + "placement_type": { + "type": "string", + "enum": ["PLACEMENT_TYPE_AUTOMATIC", "PLACEMENT_TYPE_NORMAL"] + }, + "placement": { + "type": ["null", "array"], + "items": { + "type": "string" + } + }, + "enable_inventory_filter": { + "type": ["null", "boolean"] + }, + "is_comment_disable": { + "type": "integer" + }, + "app_id": { + "type": ["integer", "null"] + }, + "external_type": { + "type": "string", + "enum": [ + "APP_ANDROID", + "APP_IOS", + "WEBSITE", + "LEAD_GENERATION", + "WEBSITE_OR_DISPLAY" + ] + }, + "app_download_url": { + "type": ["string", "null"] + }, + "package": { + "type": ["string", "null"] + }, + "pixel_id": { + "type": ["integer", "null"] + }, + "external_action": { + "type": ["string", "null"] + }, + "deep_external_action": { + "type": ["string", "null"] + }, + "creative_material_mode": { + "type": "string" + }, + "modify_time": { + "type": "string" + }, + "create_time": { + "type": "string" + }, + "audience": { + "type": "array", + "items": { + "type": "integer" + } + }, + "excluded_audience": { + "type": "array", + "items": { + "type": "integer" + } + }, + "audience_type": { + "type": ["string", "null"] + }, + "location": { + "type": "array", + "items": { + "type": "integer" + } + }, + "is_hfss": { + "type": "boolean" + }, + "interest_category_v2": { + "type": "array", + "items": { + "type": "integer" + } + }, + "interest_keywords": { + "type": "array", + "items": { + "type": "integer" + } + }, + "age": { + "type": ["array", "null"], + "items": { + "type": "string" + } + }, + "gender": { + "type": ["string", "null"] + }, + "languages": { + "type": "array", + "items": { + "type": "string" + } + }, + "operation_system": { + "type": "array", + "items": { + "type": "string" + } + }, + "connection_type": { + "type": "array", + "items": { + "type": "string" + } + }, + "device_price": { + "type": ["array", "null"], + "items": { + "type": "number" + } + }, + "android_osv": { + "type": ["string", "null"] + }, + "ios_target_device": { + "type": ["string", "null"] + }, + "device_models": { + "type": ["array", "null"], + "items": { + "type": "integer" + } + }, + "ios_osv": { + "type": ["string", "null"] + }, + "budget_mode": { + "type": "string" + }, + "budget": { + "type": "number" + }, + "schedule_type": { + "type": "string" + }, + "schedule_start_time": { + "type": "string", + "format": "date-time" + }, + "schedule_end_time": { + "type": "string", + "format": "date-time" + }, + "dayparting": { + "type": ["string", "null"] + }, + "optimize_goal": { + "type": "string" + }, + "cpv_video_duration": { + "type": ["string", "null"] + }, + "conversion_window": { + "type": ["string", "null"] + }, + "pacing": { + "type": ["string", "null"] + }, + "billing_event": { + "type": ["string", "null"] + }, + "skip_learning_phase": { + "type": "integer" + }, + "bid_type": { + "type": ["string", "null"] + }, + "bid": { + "type": "number" + }, + "conversion_bid": { + "type": "number" + }, + "deep_bid_type": { + "type": ["string", "null"] + }, + "deep_cpabid": { + "type": "number" + }, + "status": { + "type": "string" + }, + "opt_status": { + "type": "string" + }, + "frequency": { + "type": ["integer", "null"] + }, + "frequency_schedule": { + "type": ["integer", "null"] + }, + "statistic_type": { + "type": ["string", "null"] + }, + "carriers_v2": { + "type": ["array", "null"], + "items": { + "type": "integer" + } + }, + "carriers": { + "type": ["array", "null"], + "items": { + "type": "string" + } + }, + "video_download": { + "type": "string" + }, + "pangle_block_app_list_id": { + "type": ["array", "null"], + "items": { + "type": "string" + } + }, + "action_categories": { + "type": ["array", "null"], + "items": { + "type": "string" + } + }, + "action_days": { + "type": ["null", "integer"] + }, + "video_actions": { + "type": ["array", "null"], + "items": { + "type": "string" + } + }, + "rf_buy_type": { + "type": ["string", "null"] + }, + "buy_impression": { + "type": ["number", "null"] + }, + "buy_reach": { + "type": ["number", "null"] + }, + "rf_predict_cpr": { + "type": ["number", "null"] + }, + "rf_predict_frequency": { + "type": ["number", "null"] + }, + "pangle_audience_package_include": { + "type": ["array", "null"], + "items": { + "type": "number" + } + }, + "pangle_audience_package_exclude": { + "type": ["array", "null"], + "items": { + "type": "number" + } + }, + "is_new_structure": { + "type": "boolean" + }, + "catalog_id": { + "type": ["null", "integer"] + }, + "product_set_id": { + "type": ["null", "integer"] + }, + "catalog_authorized_bc": { + "type": ["null", "integer"] + }, + "audience_rule": { + "type": ["object", "null"] + }, + "include_custom_actions": { + "type": ["array", "null"], + "items": { + "type": "object" + } + }, + "exclude_custom_actions": { + "type": ["array", "null"], + "items": { + "type": "object" + } + }, + "dpa_retargeting_type": { + "type": ["string", "null"] + }, + "split_test_adgroup_ids": { + "type": ["array", "null"], + "items": { + "type": "number" + } + }, + "brand_safety": { + "type": ["string", "null"] + }, + "brand_safety_partner": { + "type": ["string", "null"] + }, + "promotion_website_type": { + "type": ["string", "null"] + }, + "ios_quota_type": { + "type": ["string", "null"] + }, + "roas_bid": { + "type": ["number", "null"] + }, + "action_v2": { + "type": ["null", "array"], + "items": { + "type": ["object", "null"], + "properties": { + "action_categories": { + "type": ["array", "null"], + "items": { + "type": "integer" + } + }, + "action_period": { + "type": ["number", "null"] + }, + "action_scene": { + "type": ["string", "null"] + }, + "user_actions ": { + "type": ["array", "null"], + "items": { + "type": "string" + } + } + } + } + }, + "targeting_expansion": { + "type": ["object", "null"], + "properties": { + "enable_expansion": { + "type": "boolean" + }, + "expansion_types": { + "type": ["array", "null"], + "items": { + "type": "string" + } + } + } + } + } +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/ads.json b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/ads.json new file mode 100644 index 000000000000..f4a6f6567f78 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/ads.json @@ -0,0 +1,168 @@ +{ + "type": ["null", "object"], + "properties": { + "advertiser_id": { + "type": "integer" + }, + "campaign_id": { + "type": "integer" + }, + "campaign_name": { + "type": "string" + }, + "adgroup_id": { + "type": "integer" + }, + "adgroup_name": { + "type": "string" + }, + "ad_id": { + "type": "integer" + }, + "ad_name": { + "type": "string" + }, + "call_to_action": { + "type": ["null", "string"] + }, + "call_to_action_id": { + "type": ["null", "string"] + }, + "disclaimer_type": { + "type": ["null", "string"] + }, + "disclaimer_text": { + "type": ["null", "object"], + "properties": { + "text": { + "type": ["null", "string"] + } + } + }, + "disclaimer_clickable_texts": { + "type": ["null", "object"], + "properties": { + "text": { + "type": ["null", "string"] + }, + "url": { + "type": ["null", "string"] + } + } + }, + "card_id": { + "type": ["null", "integer"] + }, + "status": { + "type": "string" + }, + "opt_status": { + "type": ["null", "string"] + }, + "is_aco": { + "type": ["null", "boolean"] + }, + "image_ids": { + "type": ["null", "array"], + "items": { + "type": "string" + } + }, + "image_mode": { + "type": ["null", "string"] + }, + "ad_format": { + "type": ["null", "string"] + }, + "ad_text": { + "type": ["null", "string"] + }, + "ad_texts": { + "type": ["null", "array"], + "items": { + "type": "string" + } + }, + "video_id": { + "type": ["null", "string"] + }, + "tiktok_item_id": { + "type": ["null", "string"] + }, + "premium_badge_id": { + "type": ["null", "string"] + }, + "app_name": { + "type": ["null", "string"] + }, + "landing_page_url": { + "type": ["null", "string"] + }, + "landing_page_urls": { + "type": ["null", "array"], + "items": { + "type": "string" + } + }, + "display_name": { + "type": ["null", "string"] + }, + "profile_image": { + "type": ["null", "string"] + }, + "impression_tracking_url": { + "type": ["null", "string"] + }, + "click_tracking_url": { + "type": ["null", "string"] + }, + "open_url": { + "type": ["null", "string"] + }, + "open_url_type": { + "type": ["null", "string"] + }, + "fallback_type": { + "type": ["null", "string"] + }, + "playable_url": { + "type": ["null", "string"] + }, + "vast_moat": { + "type": ["null", "boolean"] + }, + "page_id": { + "type": ["number", "null"] + }, + "is_creative_authorized": { + "type": ["null", "boolean"] + }, + "is_new_structure": { + "type": ["null", "boolean"] + }, + "create_time": { + "type": "string" + }, + "modify_time": { + "type": "string" + }, + "dpa_fallback_type": { + "type": ["null", "string"] + }, + "dpa_open_url_type": { + "type": ["null", "string"] + }, + "dpa_video_tpl_id": { + "type": ["null", "string"] + }, + "promotional_music_disabled": { + "type": ["null", "boolean"] + }, + "item_duet_status": { + "type": ["null", "string"] + }, + "item_stitch_status": { + "type": ["null", "string"] + } + } +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/advertisers.json b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/advertisers.json new file mode 100644 index 000000000000..c5e89186b9ca --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/advertisers.json @@ -0,0 +1,68 @@ +{ + "type": ["null", "object"], + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "address": { + "type": ["null", "string"] + }, + "company": { + "type": ["null", "string"] + }, + "contacter": { + "type": ["null", "string"] + }, + "country": { + "type": ["null", "string"] + }, + "currency": { + "type": ["null", "string"] + }, + "description": { + "type": ["null", "string"] + }, + "email": { + "type": ["null", "string"] + }, + "industry": { + "type": ["null", "string"] + }, + "language": { + "type": ["null", "string"] + }, + "license_no": { + "type": ["null", "string"] + }, + "license_url": { + "type": ["null", "string"] + }, + "phonenumber": { + "type": ["null", "string"] + }, + "promotion_area": { + "type": ["null", "string"] + }, + "reason": { + "type": ["null", "string"] + }, + "role": { + "type": ["null", "string"] + }, + "status": { + "type": ["null", "string"] + }, + "timezone": { + "type": ["null", "string"] + }, + "balance": { + "type": "number" + }, + "create_time": { + "type": "integer" + } + } +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/campaigns.json b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/campaigns.json new file mode 100644 index 000000000000..9b3ddabe4f06 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/schemas/campaigns.json @@ -0,0 +1,56 @@ +{ + "type": ["null", "object"], + "properties": { + "campaign_id": { + "type": "integer" + }, + "campaign_name": { + "type": "string" + }, + "campaign_type": { + "type": "string" + }, + "advertiser_id": { + "type": "integer" + }, + "budget": { + "type": "number" + }, + "budget_mode": { + "type": "string" + }, + "status": { + "type": "string" + }, + "opt_status": { + "type": ["null", "string"] + }, + "objective": { + "type": ["null", "string"] + }, + "objective_type": { + "type": ["null", "string"] + }, + "budget_optimize_switch": { + "type": ["null", "number"] + }, + "bid_type": { + "type": ["null", "number"] + }, + "optimize_goal": { + "type": ["null", "string"] + }, + "split_test_variable": { + "type": ["null", "string"] + }, + "is_new_structure": { + "type": "boolean" + }, + "create_time": { + "type": "string" + }, + "modify_time": { + "type": "string" + } + } +} diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/source.py b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/source.py new file mode 100644 index 000000000000..1384e9561c69 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/source.py @@ -0,0 +1,67 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + +from typing import Any, List, Mapping, Tuple + +from airbyte_cdk.logger import AirbyteLogger +from airbyte_cdk.models import ConnectorSpecification, SyncMode +from airbyte_cdk.models.airbyte_protocol import DestinationSyncMode +from airbyte_cdk.sources import AbstractSource +from airbyte_cdk.sources.streams import Stream +from airbyte_cdk.sources.streams.http.auth import TokenAuthenticator + +from .spec import SourceTiktokMarketingSpec +from .streams import AdGroups, Ads, Advertisers, Campaigns + +DOCUMENTATION_URL = "https://docs.airbyte.io/integrations/sources/tiktok-marketing" + + +class TiktokTokenAuthenticator(TokenAuthenticator): + """ + Docs: https://business-api.tiktok.com/marketing_api/docs?rid=sta6fe2yww&id=1701890922708994 + """ + + def __init__(self, token: str, **kwargs): + super().__init__(token, **kwargs) + self.token = token + + def get_auth_header(self) -> Mapping[str, Any]: + return {"Access-Token": self.token} + + +class SourceTiktokMarketing(AbstractSource): + def spec(self, *args, **kwargs) -> ConnectorSpecification: + """Returns the spec for this integration.""" + return ConnectorSpecification( + documentationUrl=DOCUMENTATION_URL, + changelogUrl=DOCUMENTATION_URL, + supportsIncremental=True, + supported_destination_sync_modes=[DestinationSyncMode.overwrite, DestinationSyncMode.append, DestinationSyncMode.append_dedup], + connectionSpecification=SourceTiktokMarketingSpec.schema(), + ) + + @staticmethod + def _prepare_stream_args(config: Mapping[str, Any]) -> Mapping[str, Any]: + """Converts an input configure to stream arguments""" + return { + "authenticator": TiktokTokenAuthenticator(config["access_token"]), + "start_time": config.get("start_time") or "2021-01-01", + "advertiser_id": int(config["environment"].get("advertiser_id", 0)), + "app_id": int(config["environment"].get("app_id", 0)), + "secret": config["environment"].get("secret"), + } + + def check_connection(self, logger: AirbyteLogger, config: Mapping[str, Any]) -> Tuple[bool, any]: + """ + Tests if the input configuration can be used to successfully connect to the integration + """ + try: + next(Advertisers(**self._prepare_stream_args(config)).read_records(SyncMode.full_refresh)) + except Exception as err: + return False, err + return True, None + + def streams(self, config: Mapping[str, Any]) -> List[Stream]: + args = self._prepare_stream_args(config) + return [Ads(**args), Advertisers(**args), AdGroups(**args), Campaigns(**args)] diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/spec.py b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/spec.py new file mode 100644 index 000000000000..59391363e3eb --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/spec.py @@ -0,0 +1,78 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + + +import json +import re +from copy import deepcopy +from typing import Union + +from jsonschema import RefResolver +from pydantic import BaseModel, Field + +# TikTok Initial release date is September 2016 +DEFAULT_START_DATE = "01-09-2016" + + +class SandboxEnvSpec(BaseModel): + class Config: + title = "Sandbox" + + environment: str = Field("sandbox", const=True) + + # it is string because UI has the bug https://github.com/airbytehq/airbyte/issues/6875 + advertiser_id: str = Field( + description="The Advertiser ID which generated for the developer's Sandbox application.", + ) + + +class ProductionEnvSpec(BaseModel): + class Config: + title = "Production" + + environment: str = Field("prod", const=True) + + # it is float because UI has the bug https://github.com/airbytehq/airbyte/issues/6875 + app_id: str = Field( + description="The App id applied by the developer.", + ) + secret: str = Field(description="The private key of the developer's application.", airbyte_secret=True) + + +class SourceTiktokMarketingSpec(BaseModel): + class Config: + title = "TikTok Marketing Source Spec" + + environment: Union[ProductionEnvSpec, SandboxEnvSpec] = Field(default=ProductionEnvSpec.Config.title) + + access_token: str = Field(description="Long-term Authorized Access Token.", airbyte_secret=True) + + start_date: str = Field( + description="Start Date in format: YYYY-MM-DD.", default=DEFAULT_START_DATE, pattern="^[0-9]{4}-[0-9]{2}-[0-9]{2}$" + ) + + @staticmethod + def change_format_to_oneOf(schema: dict, field_name: str) -> dict: + schema["properties"][field_name]["oneOf"] = deepcopy(schema["properties"][field_name]["anyOf"]) + schema["properties"][field_name]["type"] = "object" + del schema["properties"][field_name]["anyOf"] + return schema + + @staticmethod + def resolve_refs(schema: dict) -> dict: + json_schema_ref_resolver = RefResolver.from_schema(schema) + str_schema = json.dumps(schema) + for ref_block in re.findall(r'{"\$ref": "#\/definitions\/.+?(?="})"}', str_schema): + ref = json.loads(ref_block)["$ref"] + str_schema = str_schema.replace(ref_block, json.dumps(json_schema_ref_resolver.resolve(ref)[1])) + pyschema = json.loads(str_schema) + del pyschema["definitions"] + return pyschema + + @classmethod + def schema(cls) -> dict: + """ we're overriding the schema classmethod to enable some post-processing """ + schema = super().schema() + schema = cls.change_format_to_oneOf(schema, "environment") + return cls.resolve_refs(schema) diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/streams.py b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/streams.py new file mode 100644 index 000000000000..26b1fb98f969 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/source_tiktok_marketing/streams.py @@ -0,0 +1,313 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + +# Hierarchy of classes +# TiktokStream +# ├── ListAdvertiserIdsStream +# └── FullRefreshTiktokStream +# ├── Advertisers +# └── IncrementalTiktokStream +# ├── AdGroups +# ├── Ads +# └── Campaigns + +import json +from abc import ABC +from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, TypeVar, Union + +import pendulum +import pydantic +import requests +from airbyte_cdk.models import SyncMode +from airbyte_cdk.sources.streams.http import HttpStream +from airbyte_cdk.sources.streams.http.auth import NoAuth + +from .spec import DEFAULT_START_DATE + +T = TypeVar("T") + + +class JsonUpdatedState(pydantic.BaseModel): + current_stream_state: str + stream: T + + def __repr__(self): + """Overrides print view""" + return str(self.dict()) + + def dict(self, **kwargs): + """Overrides default logic. + A new updated stage has to be sent if all advertisers are used only + """ + if not self.stream.is_finished: + return self.current_stream_state + max_updated_at = self.stream.max_cursor_date or "" + return max(max_updated_at, self.current_stream_state) + + +class TiktokException(Exception): + """default exception of custom Tiktok logic""" + + +class TiktokStream(HttpStream, ABC): + # endpoints can have different list names + response_list_field = "list" + + # max value of page + page_size = 1000 + + def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]: + """All responses have the similar structure: + { + "message": "", + "code": , # 0 if error else error unique code + "request_id": "" + "data": { + "page_info": { + "total_number": , + "page": , + "page_size": , + "total_page": + }, + "list": [ + + ] + } + } + """ + data = response.json() + if data["code"]: + raise TiktokException(data["message"]) + data = data["data"] + if self.response_list_field in data: + data = data[self.response_list_field] + for record in data: + yield record + + @property + def url_base(self) -> str: + """ + Docs: https://business-api.tiktok.com/marketing_api/docs?id=1701890920013825 + """ + if self.is_sandbox: + return "https://sandbox-ads.tiktok.com/open_api/v1.2/" + return "https://business-api.tiktok.com/open_api/v1.2/" + + def next_page_token(self, *args, **kwargs) -> Optional[Mapping[str, Any]]: + # this data without listing + return None + + def should_retry(self, response: requests.Response) -> bool: + """ + Once the rate limit is met, the server returns "code": 40100 + Docs: https://business-api.tiktok.com/marketing_api/docs?id=1701890997610497 + """ + data = response.json() + if data["code"] == 40100: + return True + return super().should_retry(response) + + def backoff_time(self, response: requests.Response) -> Optional[float]: + """ + The system uses a second call limit for each developer app. The set limit varies according to the app's call limit level. + """ + # Basic: 10/sec + # Advanced: 20/sec + # Premium: 30/sec + # All apps are set to basic call limit level by default. + # Returns maximum possible delay + return 0.6 + + +class ListAdvertiserIdsStream(TiktokStream): + """Loading of all possible advertisers""" + + primary_key = "advertiser_id" + + def __init__(self, advertiser_id: int, app_id: int, secret: str, access_token: str): + super().__init__(authenticator=NoAuth()) + self._advertiser_ids = [] + # for Sandbox env + self._advertiser_id = advertiser_id + if not self._advertiser_id: + # for Production env + self._secret = secret + self._app_id = app_id + self._access_token = access_token + else: + self._advertiser_ids.append(self._advertiser_id) + + @property + def is_sandbox(self) -> bool: + """ + the config parameter advertiser_id is required for Sandbox + """ + # only sandbox has a not empty self._advertiser_id value + return self._advertiser_id > 0 + + def request_params( + self, stream_state: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None, **kwargs + ) -> MutableMapping[str, Any]: + + return { + "access_token": self._access_token, + "secret": self._secret, + "app_id": self._app_id, + } + + def path(self, *args, **kwargs) -> str: + return "oauth2/advertiser/get/" + + @property + def advertiser_ids(self): + if not self._advertiser_ids: + for advertiser in self.read_records(SyncMode.full_refresh): + self._advertiser_ids.append(advertiser["advertiser_id"]) + return self._advertiser_ids + + +class FullRefreshTiktokStream(TiktokStream, ABC): + primary_key = "id" + fields: List[str] = None + + def __init__(self, advertiser_id: int, app_id: int, secret: str, start_time: str, **kwargs): + super().__init__(**kwargs) + # convert a start date to TikTok format + # example: "2021-08-24" => "2021-08-24 00:00:00" + self._start_time = pendulum.parse(start_time or DEFAULT_START_DATE).strftime("%Y-%m-%d 00:00:00") + self._advertiser_storage = ListAdvertiserIdsStream( + advertiser_id=advertiser_id, app_id=app_id, secret=secret, access_token=self.authenticator.token + ) + self.max_cursor_date = None + self._advertiser_ids = self._advertiser_storage.advertiser_ids + + @property + def is_sandbox(self): + return self._advertiser_storage.is_sandbox + + @staticmethod + def convert_array_param(arr: List[Union[str, int]]) -> str: + return json.dumps(arr) + + @property + def is_finished(self): + return len(self._advertiser_ids) == 0 + + def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]: + """Loads all updated tickets after last stream state""" + while self._advertiser_ids: + advertiser_id = self._advertiser_ids.pop(0) + yield {"advertiser_id": advertiser_id} + + def request_params( + self, + stream_state: Mapping[str, Any] = None, + next_page_token: Mapping[str, Any] = None, + stream_slice: Mapping[str, Any] = None, + **kwargs + ) -> MutableMapping[str, Any]: + params = {"page_size": self.page_size} + if self.fields: + params["fields"] = self.convert_array_param(self.fields) + if stream_slice: + params.update(stream_slice) + return params + + +class IncrementalTiktokStream(FullRefreshTiktokStream, ABC): + cursor_field = "modify_time" + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.max_cursor_date = None + + def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]: + """All responses have the following pagination data: + { + "data": { + "page_info": { + "total_number": < total_item_count >, + "page": < current_page_number >, + "page_size": < page_size >, + "total_page": < total_page_count > + }, + ... + } + } + """ + + page_info = response.json()["data"]["page_info"] + if page_info["page"] < page_info["total_page"]: + return {"page": page_info["page"] + 1} + return None + + def request_params(self, next_page_token: Mapping[str, Any] = None, **kwargs) -> MutableMapping[str, Any]: + params = super().request_params(next_page_token=next_page_token, **kwargs) + if next_page_token: + params.update(next_page_token) + return params + + def parse_response(self, response: requests.Response, stream_state: Mapping[str, Any], **kwargs) -> Iterable[Mapping]: + """Additional data filtering""" + state = stream_state.get(self.cursor_field) or self._start_time + for record in super().parse_response(response, **kwargs): + updated = record[self.cursor_field] + if updated <= state: + continue + elif not self.max_cursor_date or self.max_cursor_date < updated: + self.max_cursor_date = updated + yield record + + def get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]) -> Mapping[str, Any]: + # needs to save a last state if all advertisers are used before only + current_stream_state_value = (current_stream_state or {}).get(self.cursor_field, "") + + # a object JsonUpdatedState is related with a currect stream and should return a new updated state if needed + if not isinstance(current_stream_state_value, JsonUpdatedState): + current_stream_state_value = JsonUpdatedState(stream=self, current_stream_state=current_stream_state_value) + + return {self.cursor_field: current_stream_state_value} + + +class Advertisers(FullRefreshTiktokStream): + """Docs: https://ads.tiktok.com/marketing_api/docs?id=1708503202263042""" + + def request_params(self, **kwargs) -> MutableMapping[str, Any]: + params = super().request_params(**kwargs) + params["advertiser_ids"] = self.convert_array_param(self._advertiser_ids) + return params + + def path(self, *args, **kwargs) -> str: + return "advertiser/info/" + + def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]: + """this stream must work with the default slice logic""" + yield None + + +class Campaigns(IncrementalTiktokStream): + """Docs: https://ads.tiktok.com/marketing_api/docs?id=1708582970809346""" + + primary_key = "campaign_id" + + def path(self, *args, **kwargs) -> str: + return "campaign/get/" + + +class AdGroups(IncrementalTiktokStream): + """Docs: https://ads.tiktok.com/marketing_api/docs?id=1708503489590273""" + + primary_key = "adgroup_id" + + def path(self, *args, **kwargs) -> str: + return "adgroup/get/" + + +class Ads(IncrementalTiktokStream): + """Docs: https://ads.tiktok.com/marketing_api/docs?id=1708572923161602""" + + primary_key = "ad_id" + + def path(self, *args, **kwargs) -> str: + return "ad/get/" diff --git a/airbyte-integrations/connectors/source-tiktok-marketing/unit_tests/unit_test.py b/airbyte-integrations/connectors/source-tiktok-marketing/unit_tests/unit_test.py new file mode 100644 index 000000000000..87ff4b597140 --- /dev/null +++ b/airbyte-integrations/connectors/source-tiktok-marketing/unit_tests/unit_test.py @@ -0,0 +1,131 @@ +# +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. +# + +import json +import random +from typing import Any, Dict, Iterable, List, Mapping, Tuple + +import pendulum +import pytest +import requests_mock +import timeout_decorator +from airbyte_cdk.sources.streams.http.exceptions import UserDefinedBackoffException +from source_tiktok_marketing import SourceTiktokMarketing +from source_tiktok_marketing.streams import Ads, Advertisers, JsonUpdatedState + +SANDBOX_CONFIG_FILE = "secrets/config.json" +PROD_CONFIG_FILE = "secrets/prod_config.json" + + +@pytest.fixture(scope="module") +def prepared_sandbox_args(): + """Generates streams settings from a file for sandbox""" + with open(SANDBOX_CONFIG_FILE, "r") as f: + return SourceTiktokMarketing._prepare_stream_args(json.loads(f.read())) + + +@pytest.fixture(scope="module") +def prepared_prod_args(): + """Generates streams settings from a file for production""" + with open(PROD_CONFIG_FILE, "r") as f: + return SourceTiktokMarketing._prepare_stream_args(json.loads(f.read())) + + +@timeout_decorator.timeout(20) +def test_backoff(prepared_sandbox_args): + """TiktokMarketing sends the header 'Retry-After' about needed delay. + All streams have to handle it""" + stream = Advertisers(**prepared_sandbox_args) + with requests_mock.Mocker() as m: + url = stream.url_base + stream.path() + m.get(url, text=json.dumps({"code": 40100})) + with pytest.raises(UserDefinedBackoffException): + list(stream.read_records(sync_mode=None)) + + +def generate_pages(items: List[Mapping[str, Any]], page_size: int, last_empty: bool = False) -> Iterable[Tuple[int, Dict]]: + pages = [] + for i in range(0, len(items), page_size): + pages.append(items[i : i + page_size]) + if last_empty: + pages.append([]) + total_number = len(items) + for page_number, page_items in enumerate(pages, start=1): + yield ( + page_number, + { + "message": "OK", + "code": 0, + "request_id": "unique_request_id", + "data": { + "page_info": { + "total_number": total_number, + "page": page_number, + "page_size": page_size, + "total_page": len(page_items), + }, + "list": page_items, + }, + }, + ) + + +def random_integer(max_value: int = 1634125471, min_value: int = 1) -> int: + return random.randint(min_value, max_value) + + +def unixtime2str(unix_time: int) -> str: + "Converts unix time to string" + return pendulum.from_timestamp(unix_time).strftime("%Y-%m-%d %H:%M:%S") + + +def test_random_items(prepared_prod_args): + stream = Ads(**prepared_prod_args) + advertiser_count = 100 + test_advertiser_ids = set([random_integer() for _ in range(advertiser_count)]) + advertiser_count = len(test_advertiser_ids) + page_size = 100 + with requests_mock.Mocker() as m: + # mock for advertisers' list + advertisers = [{"advertiser_id": i, "advertiser_name": str(i)} for i in test_advertiser_ids] + for _, page_response in generate_pages(items=advertisers, page_size=advertiser_count): + m.register_uri("GET", "/open_api/v1.2/oauth2/advertiser/get/", json=page_response) + stream = Ads(**prepared_prod_args) + stream.page_size = page_size + assert not set(test_advertiser_ids).symmetric_difference(stream._advertiser_ids), "stream found not all advertiser IDs" + + current_state = None + max_updated_value = None + for stream_slice in stream.stream_slices(): + advertiser_id = stream_slice["advertiser_id"] + test_ad_ids = [random_integer() for _ in range(random_integer(max_value=999))] + ad_items = [] + for ad_id in test_ad_ids: + create_time = random_integer(min_value=1507901660) + ad_items.append( + { + "create_time": unixtime2str(create_time), + "modify_time": unixtime2str(create_time + 60), + "advertiser_id": advertiser_id, + "ad_id": ad_id, + } + ) + if not max_updated_value or max_updated_value < ad_items[-1][stream.cursor_field]: + max_updated_value = ad_items[-1][stream.cursor_field] + # mock for ads + for page, page_response in generate_pages(items=ad_items, page_size=page_size, last_empty=True): + uri = f"/open_api/v1.2/ad/get/?page_size={page_size}&advertiser_id={advertiser_id}" + if page != 1: + uri += f"&page={page}" + m.register_uri("GET", uri, complete_qs=True, json=page_response) + + for record in stream.read_records(sync_mode=None, stream_slice=stream_slice): + current_state = stream.get_updated_state(current_state, record) + assert isinstance(current_state[stream.cursor_field], JsonUpdatedState), "state should be an JsonUpdatedState object" + if advertisers[-1]["advertiser_id"] != advertiser_id: + assert ( + current_state[stream.cursor_field].dict() == "" + ), "max updated cursor value should be returned for last slice only" + assert len(stream._advertiser_ids) == 0, "all advertisers should be popped" + assert current_state[stream.cursor_field].dict() == max_updated_value diff --git a/docs/integrations/README.md b/docs/integrations/README.md index 75c5c08fff7a..77a0b2690e6e 100644 --- a/docs/integrations/README.md +++ b/docs/integrations/README.md @@ -102,6 +102,7 @@ Airbyte uses a grading system for connectors to help users understand what to ex | [Sugar CRM](sources/sugar-crm.md) | Beta | | [SurveyMonkey](sources/surveymonkey.md) | Beta | | [Tempo](sources/tempo.md) | Beta | +| [TikTok Marketing](./sources/tiktok-marketing.md)| Alpha | | [Trello](sources/trello.md) | Beta | | [Twilio](sources/twilio.md) | Beta | | [US Census](sources/us-census.md) | Alpha | diff --git a/docs/integrations/sources/tiktok-marketing.md b/docs/integrations/sources/tiktok-marketing.md new file mode 100644 index 000000000000..2455bc7f8fe2 --- /dev/null +++ b/docs/integrations/sources/tiktok-marketing.md @@ -0,0 +1,54 @@ +# TikTok Marketing + +## Overview + +The [TikTok For Business Marketing API](https://ads.tiktok.com/marketing_api/homepage?rid=uvtbok1h19) allows you to directly interact with the TikTok Ads Manager platform for automated ad management and analysis. + +The TikTok Marketing source supports both Full Refresh and Incremental syncs. You can choose if this connector will copy only the new or updated data, or all rows in the tables and columns you set up for replication, every time a sync is run. + +This Source Connector is based on a [Airbyte CDK](https://docs.airbyte.io/connector-development/cdk-python). + +### Output schema + +Several output streams are available from this source: + +* [Advertisers](https://business-api.tiktok.com/marketing_api/docs?id=1708503202263042) \(Full Refresh\) +* [Campaigns](https://business-api.tiktok.com/marketing_api/docs?id=1708582970809346) \(Incremental\) +* [Ad Groups](https://business-api.tiktok.com/marketing_api/docs?id=1708503489590273)\(Incremental\) +* [Ads](https://business-api.tiktok.com/marketing_api/docs?id=1708572923161602)\(Incremental\) + +If there are more endpoints you'd like Airbyte to support, please [create an issue.](https://github.com/airbytehq/airbyte/issues/new/choose) + +### Features + +| Feature | Supported? | +| :--- | :--- | +| Full Refresh Sync | Yes | +| Incremental - Append Sync | Yes | +| SSL connection | Yes | +| Namespaces | No | + +### Performance considerations + +The connector is restricted by [requests limitation](https://ads.tiktok.com/marketing_api/docs?rid=fgvgaumno25&id=1701890997610497). This connector should not run into TikTok Marketing API limitations under normal usage. Please [create an issue](https://github.com/airbytehq/airbyte/issues) if you see any rate limit issues that are not automatically retried successfully. + +## Getting started + +### Requirements + +* Access Token - This token will not expire. +* Production Environment + * App ID + * Secret +* SandBox Environment + * Advertiser ID - It is generated for sandbox in one copy + +### Setup guide + +Please read [How to get your AppID, Secret and Access Token](https://ads.tiktok.com/marketing_api/docs?rid=fgvgaumno25&id=1701890909484033) or [How to create a SandBox Environment](https://ads.tiktok.com/marketing_api/docs?rid=fgvgaumno25&id=1701890920013825) + +## Changelog + +| Version | Date | Pull Request | Subject | +| :------ | :-------- | :----- | :------ | +| 0.1.0 | 2021-09-18 | [5887](https://github.com/airbytehq/airbyte/pull/5887) | Release TikTok Marketing CDK Connector | diff --git a/tools/bin/ci_credentials.sh b/tools/bin/ci_credentials.sh index 1f35898bfcfc..f60a6c8ead97 100755 --- a/tools/bin/ci_credentials.sh +++ b/tools/bin/ci_credentials.sh @@ -135,6 +135,8 @@ write_standard_creds source-stripe "$SOURCE_STRIPE_CREDS" write_standard_creds source-stripe "$STRIPE_INTEGRATION_CONNECTED_ACCOUNT_TEST_CREDS" "connected_account_config.json" write_standard_creds source-surveymonkey "$SURVEYMONKEY_TEST_CREDS" write_standard_creds source-tempo "$TEMPO_INTEGRATION_TEST_CREDS" +write_standard_creds source-tiktok-marketing "$SOURCE_TIKTOK_MARKETING_TEST_CREDS" +write_standard_creds source-tiktok-marketing "$SOURCE_TIKTOK_MARKETING_PROD_TEST_CREDS" "prod_config.json" write_standard_creds source-trello "$TRELLO_TEST_CREDS" write_standard_creds source-twilio "$TWILIO_TEST_CREDS" write_standard_creds source-typeform "$SOURCE_TYPEFORM_CREDS"