Skip to content

Commit 4d37392

Browse files
fix(source-amazon-ads): fix download request for sponsored_products_report_stream (#43354)
1 parent 70b5501 commit 4d37392

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed

airbyte-integrations/connectors/source-amazon-ads/metadata.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ data:
1313
connectorSubtype: api
1414
connectorType: source
1515
definitionId: c6b0a29e-1da9-4512-9002-7bfd0cba2246
16-
dockerImageTag: 5.0.10
16+
dockerImageTag: 5.0.11
1717
dockerRepository: airbyte/source-amazon-ads
1818
documentationUrl: https://docs.airbyte.com/integrations/sources/amazon-ads
1919
githubIssueLabel: source-amazon-ads

airbyte-integrations/connectors/source-amazon-ads/pyproject.toml

+1-1
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 = "5.0.10"
6+
version = "5.0.11"
77
name = "source-amazon-ads"
88
description = "Source implementation for Amazon Ads."
99
authors = [ "Airbyte <[email protected]>",]

airbyte-integrations/connectors/source-amazon-ads/source_amazon_ads/streams/report_streams/products_report.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55

66
from http import HTTPStatus
7-
from typing import List
7+
from typing import Any, List, Mapping
88

9-
from .report_streams import ReportInfo, ReportStream
9+
import backoff
10+
import requests
11+
from airbyte_cdk.sources.streams.http.requests_native_auth import Oauth2Authenticator
12+
from source_amazon_ads.schemas import Profile
13+
14+
from .report_streams import ReportInfo, ReportStream, TooManyRequests
1015

1116
METRICS_MAP = {
1217
"campaigns": [
@@ -266,6 +271,12 @@ class SponsoredProductsReportStream(ReportStream):
266271
metrics_map = METRICS_MAP
267272
metrics_type_to_id_map = METRICS_TYPE_TO_ID_MAP
268273

274+
def __init__(self, config: Mapping[str, Any], profiles: List[Profile], authenticator: Oauth2Authenticator):
275+
super().__init__(config, profiles, authenticator)
276+
# using session without auth as API returns 400 bad request if Authorization header presents in request
277+
# X-Amz-Algorithm and X-Amz-Signature query params already present in the url, that is enough to make proper request
278+
self._report_download_session = requests.Session()
279+
269280
def report_init_endpoint(self, record_type: str) -> str:
270281
return f"/{self.API_VERSION}/reports"
271282

airbyte-integrations/connectors/source-amazon-ads/source_amazon_ads/streams/report_streams/report_streams.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def __init__(self, config: Mapping[str, Any], profiles: List[Profile], authentic
121121
self._state = {}
122122
self._session = requests.Session()
123123
self._session.auth = authenticator
124+
self._report_download_session = self._session
124125
self._model = self._generate_model()
125126
self._start_date: Optional[Date] = config.get("start_date")
126127
self._look_back_window: int = config["look_back_window"]
@@ -294,12 +295,13 @@ def _check_status(self, report_info: ReportInfo) -> Tuple[Status, str]:
294295
),
295296
max_tries=10,
296297
)
297-
def _send_http_request(self, url: str, profile_id: int, json: dict = None):
298+
def _send_http_request(self, url: str, profile_id: int, json: dict = None, is_download_report: bool = False):
298299
headers = self._get_auth_headers(profile_id)
299300
if json:
300301
response = self._session.post(url, headers=headers, json=json)
301302
else:
302-
response = self._session.get(url, headers=headers)
303+
session = self._report_download_session if is_download_report else self._session
304+
response = session.get(url, headers=headers)
303305
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
304306
raise TooManyRequests()
305307
return response
@@ -433,7 +435,8 @@ def _download_report(self, report_info: ReportInfo, url: str) -> List[dict]:
433435
"""
434436
Download and parse report result
435437
"""
436-
response = self._send_http_request(url, report_info.profile_id) if report_info else self._send_http_request(url, None)
438+
profile_id = report_info.profile_id if report_info else None
439+
response = self._send_http_request(url=url, profile_id=profile_id, is_download_report=True)
437440
response.raise_for_status()
438441
raw_string = decompress(response.content).decode("utf")
439442
return json.loads(raw_string)

airbyte-integrations/connectors/source-amazon-ads/unit_tests/test_report_streams.py

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pendulum
1313
import pytest
14+
import requests_mock
1415
import responses
1516
from airbyte_cdk.models import SyncMode
1617
from freezegun import freeze_time
@@ -876,3 +877,17 @@ def test_get_record_id_by_report_type(config, metric_object, record_type):
876877
profiles = make_profiles(profile_type="vendor")
877878
stream = SponsoredProductsReportStream(config, profiles, authenticator=mock.MagicMock())
878879
assert stream.get_record_id(metric_object, record_type), "recordId must be non-empty value"
880+
881+
882+
def test_sponsored_products_report_stream_send_http_request_on_download_not_use_headers(config):
883+
profiles = make_profiles(profile_type="vendor")
884+
stream = SponsoredProductsReportStream(config,profiles, authenticator=mock.MagicMock())
885+
download_url = "https://download/url"
886+
887+
with requests_mock.Mocker() as m:
888+
request_mock = m.get(download_url, status_code=200)
889+
stream._send_http_request(download_url, None, None, True)
890+
891+
assert request_mock.called is True
892+
assert "Authorization" not in request_mock.request_history[0].matcher.last_request._request.headers.keys()
893+

docs/integrations/sources/amazon-ads.md

+21-20
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,27 @@ Information about expected report generation waiting time can be found [here](ht
131131
<summary>Expand to review</summary>
132132

133133
| Version | Date | Pull Request | Subject |
134-
| :------ | :--------- | :------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------- |
135-
| 5.0.10 | 2024-08-10 | [42162](https://github.com/airbytehq/airbyte/pull/42162) | Update dependencies |
136-
| 5.0.9 | 2024-07-13 | [41876](https://github.com/airbytehq/airbyte/pull/41876) | Update dependencies |
137-
| 5.0.8 | 2024-07-10 | [41487](https://github.com/airbytehq/airbyte/pull/41487) | Update dependencies |
138-
| 5.0.7 | 2024-07-09 | [41143](https://github.com/airbytehq/airbyte/pull/41143) | Update dependencies |
139-
| 5.0.6 | 2024-07-06 | [40798](https://github.com/airbytehq/airbyte/pull/40798) | Update dependencies |
140-
| 5.0.5 | 2024-06-25 | [40403](https://github.com/airbytehq/airbyte/pull/40403) | Update dependencies |
141-
| 5.0.4 | 2024-06-21 | [39926](https://github.com/airbytehq/airbyte/pull/39926) | Update dependencies |
142-
| 5.0.3 | 2024-06-04 | [38962](https://github.com/airbytehq/airbyte/pull/38962) | [autopull] Upgrade base image to v1.2.1 |
143-
| 5.0.2 | 2024-05-29 | [38737](https://github.com/airbytehq/airbyte/pull/38737) | Update authenticator to `requests_native_auth` package |
144-
| 5.0.1 | 2024-04-29 | [37655](https://github.com/airbytehq/airbyte/pull/37655) | Update error messages and spec with info about `agency` profile type. |
145-
| 5.0.0 | 2024-03-22 | [36169](https://github.com/airbytehq/airbyte/pull/36169) | Update `SponsoredBrand` and `SponsoredProduct` streams due to API endpoint deprecation |
146-
| 4.1.0 | 2024-03-19 | [36267](https://github.com/airbytehq/airbyte/pull/36267) | Pin airbyte-cdk version to `^0` |
147-
| 4.0.4 | 2024-02-23 | [35481](https://github.com/airbytehq/airbyte/pull/35481) | Migrate source to `YamlDeclarativeSource` with custom `check_connection` |
148-
| 4.0.3 | 2024-02-12 | [35180](https://github.com/airbytehq/airbyte/pull/35180) | Manage dependencies with Poetry |
149-
| 4.0.2 | 2024-02-08 | [35013](https://github.com/airbytehq/airbyte/pull/35013) | Add missing field to `sponsored_display_budget_rules` stream |
150-
| 4.0.1 | 2023-12-28 | [33833](https://github.com/airbytehq/airbyte/pull/33833) | Updated oauth spec to put region, so we can choose oauth consent url based on it |
151-
| 4.0.0 | 2023-12-28 | [33817](https://github.com/airbytehq/airbyte/pull/33817) | Fix schema for streams: `SponsoredBrandsAdGroups` and `SponsoredBrandsKeywords` |
152-
| 3.4.2 | 2023-12-12 | [33361](https://github.com/airbytehq/airbyte/pull/33361) | Fix unexpected crash when handling error messages which don't have `requestId` field |
153-
| 3.4.1 | 2023-10-19 | [31599](https://github.com/airbytehq/airbyte/pull/31599) | Base image migration: remove Dockerfile and use the python-connector-base image |
134+
|:--------|:-----------|:---------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------|
135+
| 5.0.11 | 2024-08-12 | [43354](https://github.com/airbytehq/airbyte/pull/43354) | Fix download request for `sponsored_products_report_stream` |
136+
| 5.0.10 | 2024-08-10 | [42162](https://github.com/airbytehq/airbyte/pull/42162) | Update dependencies |
137+
| 5.0.9 | 2024-07-13 | [41876](https://github.com/airbytehq/airbyte/pull/41876) | Update dependencies |
138+
| 5.0.8 | 2024-07-10 | [41487](https://github.com/airbytehq/airbyte/pull/41487) | Update dependencies |
139+
| 5.0.7 | 2024-07-09 | [41143](https://github.com/airbytehq/airbyte/pull/41143) | Update dependencies |
140+
| 5.0.6 | 2024-07-06 | [40798](https://github.com/airbytehq/airbyte/pull/40798) | Update dependencies |
141+
| 5.0.5 | 2024-06-25 | [40403](https://github.com/airbytehq/airbyte/pull/40403) | Update dependencies |
142+
| 5.0.4 | 2024-06-21 | [39926](https://github.com/airbytehq/airbyte/pull/39926) | Update dependencies |
143+
| 5.0.3 | 2024-06-04 | [38962](https://github.com/airbytehq/airbyte/pull/38962) | [autopull] Upgrade base image to v1.2.1 |
144+
| 5.0.2 | 2024-05-29 | [38737](https://github.com/airbytehq/airbyte/pull/38737) | Update authenticator to `requests_native_auth` package |
145+
| 5.0.1 | 2024-04-29 | [37655](https://github.com/airbytehq/airbyte/pull/37655) | Update error messages and spec with info about `agency` profile type. |
146+
| 5.0.0 | 2024-03-22 | [36169](https://github.com/airbytehq/airbyte/pull/36169) | Update `SponsoredBrand` and `SponsoredProduct` streams due to API endpoint deprecation |
147+
| 4.1.0 | 2024-03-19 | [36267](https://github.com/airbytehq/airbyte/pull/36267) | Pin airbyte-cdk version to `^0` |
148+
| 4.0.4 | 2024-02-23 | [35481](https://github.com/airbytehq/airbyte/pull/35481) | Migrate source to `YamlDeclarativeSource` with custom `check_connection` |
149+
| 4.0.3 | 2024-02-12 | [35180](https://github.com/airbytehq/airbyte/pull/35180) | Manage dependencies with Poetry |
150+
| 4.0.2 | 2024-02-08 | [35013](https://github.com/airbytehq/airbyte/pull/35013) | Add missing field to `sponsored_display_budget_rules` stream |
151+
| 4.0.1 | 2023-12-28 | [33833](https://github.com/airbytehq/airbyte/pull/33833) | Updated oauth spec to put region, so we can choose oauth consent url based on it |
152+
| 4.0.0 | 2023-12-28 | [33817](https://github.com/airbytehq/airbyte/pull/33817) | Fix schema for streams: `SponsoredBrandsAdGroups` and `SponsoredBrandsKeywords` |
153+
| 3.4.2 | 2023-12-12 | [33361](https://github.com/airbytehq/airbyte/pull/33361) | Fix unexpected crash when handling error messages which don't have `requestId` field |
154+
| 3.4.1 | 2023-10-19 | [31599](https://github.com/airbytehq/airbyte/pull/31599) | Base image migration: remove Dockerfile and use the python-connector-base image |
154155
| 3.4.0 | 2023-06-09 | [25913](https://github.com/airbytehq/airbyte/pull/26203) | Add Stream `DisplayCreatives` |
155156
| 3.3.0 | 2023-09-22 | [30679](https://github.com/airbytehq/airbyte/pull/30679) | Fix unexpected column for `SponsoredProductCampaigns` and `SponsoredBrandsKeywords` |
156157
| 3.2.0 | 2023-09-18 | [30517](https://github.com/airbytehq/airbyte/pull/30517) | Add suggested streams; fix unexpected column issue |

0 commit comments

Comments
 (0)