Skip to content

Commit aef0fc7

Browse files
authored
Source Snapchat Marketing: update authenticator package (#38574)
1 parent 3ec9b4e commit aef0fc7

File tree

6 files changed

+393
-132
lines changed

6 files changed

+393
-132
lines changed

airbyte-integrations/connectors/source-snapchat-marketing/metadata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data:
88
connectorSubtype: api
99
connectorType: source
1010
definitionId: 200330b2-ea62-4d11-ac6d-cfe3e3f8ab2b
11-
dockerImageTag: 0.6.1
11+
dockerImageTag: 0.6.2
1212
dockerRepository: airbyte/source-snapchat-marketing
1313
githubIssueLabel: source-snapchat-marketing
1414
icon: snapchat.svg

airbyte-integrations/connectors/source-snapchat-marketing/poetry.lock

Lines changed: 382 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

airbyte-integrations/connectors/source-snapchat-marketing/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = [ "poetry-core>=1.0.0",]
33
build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
6-
version = "0.6.1"
6+
version = "0.6.2"
77
name = "source-snapchat-marketing"
88
description = "Source implementation for Snapchat Marketing."
99
authors = [ "Airbyte <[email protected]>",]
@@ -17,7 +17,7 @@ include = "source_snapchat_marketing"
1717

1818
[tool.poetry.dependencies]
1919
python = "^3.9,<3.12"
20-
airbyte-cdk = "0.80.0"
20+
airbyte-cdk = "0.90.0"
2121

2222
[tool.poetry.scripts]
2323
source-snapchat-marketing = "source_snapchat_marketing.run:run"

airbyte-integrations/connectors/source-snapchat-marketing/source_snapchat_marketing/source.py

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from airbyte_cdk.sources.streams import Stream
1717
from airbyte_cdk.sources.streams.core import IncrementalMixin, package_name_from_class
1818
from airbyte_cdk.sources.streams.http import HttpStream
19-
from airbyte_cdk.sources.streams.http.auth import Oauth2Authenticator
20-
from airbyte_cdk.sources.streams.http.exceptions import DefaultBackoffException
19+
from airbyte_cdk.sources.streams.http.requests_native_auth import Oauth2Authenticator
2120
from airbyte_cdk.sources.utils.schema_helpers import ResourceSchemaLoader
2221

2322
# https://marketingapi.snapchat.com/docs/#core-metrics
@@ -250,7 +249,7 @@ def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
250249
self.max_state = self.initial_state
251250

252251
parent_stream = self.parent(
253-
authenticator=self.authenticator,
252+
authenticator=self._session.auth,
254253
start_date=self.start_date,
255254
end_date=self.end_date,
256255
action_report_time=self.action_report_time,
@@ -379,7 +378,7 @@ def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
379378
"""Each stream slice represents each entity id from parent stream"""
380379

381380
parent_stream = self.parent(
382-
authenticator=self.authenticator,
381+
authenticator=self._session.auth,
383382
start_date=self.start_date,
384383
end_date=self.end_date,
385384
action_report_time=self.action_report_time,
@@ -757,44 +756,13 @@ class CampaignsStatsLifetime(Lifetime, Stats):
757756
parent = Campaigns
758757

759758

760-
class SnapchatOauth2Authenticator(Oauth2Authenticator):
761-
@backoff.on_exception(
762-
backoff.expo,
763-
DefaultBackoffException,
764-
on_backoff=lambda details: logger.info(
765-
f"Caught retryable error after {details['tries']} tries. Waiting {details['wait']} seconds then retrying..."
766-
),
767-
max_time=300,
768-
)
769-
def refresh_access_token(self) -> Tuple[str, int]:
770-
"""
771-
returns a tuple of (access_token, token_lifespan_in_seconds)
772-
"""
773-
try:
774-
response = requests.request(
775-
method="POST",
776-
url=self.token_refresh_endpoint,
777-
data=self.get_refresh_request_body(),
778-
headers=self.get_refresh_access_token_headers(),
779-
)
780-
response.raise_for_status()
781-
response_json = response.json()
782-
return response_json["access_token"], response_json["expires_in"]
783-
except requests.exceptions.RequestException as e:
784-
if e.response.status_code == 429 or e.response.status_code >= 500:
785-
raise DefaultBackoffException(request=e.response.request, response=e.response)
786-
raise
787-
except Exception as e:
788-
raise Exception(f"Error while refreshing access token: {e}") from e
789-
790-
791759
# Source
792760
class SourceSnapchatMarketing(AbstractSource):
793761
"""Source Snapchat Marketing helps to retrieve the different Ad data from Snapchat business account"""
794762

795763
def check_connection(self, logger, config) -> Tuple[bool, any]:
796764
try:
797-
auth = SnapchatOauth2Authenticator(
765+
auth = Oauth2Authenticator(
798766
token_refresh_endpoint="https://accounts.snapchat.com/login/oauth2/access_token",
799767
client_id=config["client_id"],
800768
client_secret=config["client_secret"],
@@ -820,7 +788,7 @@ def streams(self, config: Mapping[str, Any]) -> List[Stream]:
820788
# 2. when timezone is not specified, default account's timezone will be used automatically
821789
default_end_date = pendulum.now().subtract(days=DELAYED_DAYS).to_date_string()
822790
kwargs = {
823-
"authenticator": SnapchatOauth2Authenticator(
791+
"authenticator": Oauth2Authenticator(
824792
token_refresh_endpoint="https://accounts.snapchat.com/login/oauth2/access_token",
825793
client_id=config["client_id"],
826794
client_secret=config["client_secret"],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
Ads,
1515
AdsStatsDaily,
1616
AdsStatsLifetime,
17+
Oauth2Authenticator,
1718
Organizations,
18-
SnapchatOauth2Authenticator,
1919
SourceSnapchatMarketing,
2020
)
2121

@@ -381,7 +381,7 @@ def test_retry_get_access_token(requests_mock):
381381
"https://accounts.snapchat.com/login/oauth2/access_token",
382382
[{"status_code": 429}, {"status_code": 429}, {"status_code": 200, "json": {"access_token": "token", "expires_in": 3600}}],
383383
)
384-
auth = SnapchatOauth2Authenticator(
384+
auth = Oauth2Authenticator(
385385
token_refresh_endpoint="https://accounts.snapchat.com/login/oauth2/access_token",
386386
client_id="client_id",
387387
client_secret="client_secret",

docs/integrations/sources/snapchat-marketing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Snapchat Marketing API has limitations to 1000 items per page.
121121

122122
| Version | Date | Pull Request | Subject |
123123
| :------ | :--------- | :------------------------------------------------------- | :----------------------------------------------------------------------------- |
124+
| 0.6.2 | 2024-15-22 | [38574](https://github.com/airbytehq/airbyte/pull/38574) | Update authenticator package |
124125
| 0.6.1 | 2024-04-24 | [36662](https://github.com/airbytehq/airbyte/pull/36662) | Schema descriptions |
125126
| 0.6.0 | 2024-04-10 | [30586](https://github.com/airbytehq/airbyte/pull/30586) | Add `attribution_windows`,`action_report_time` as optional configurable params |
126127
| 0.5.0 | 2024-03-19 | [36267](https://github.com/airbytehq/airbyte/pull/36267) | Pin airbyte-cdk version to `^0` |

0 commit comments

Comments
 (0)