Skip to content

Commit

Permalink
Source Google Ads: handle refresh error (#26209)
Browse files Browse the repository at this point in the history
* Source Google Ads: handle refresh error

* Source Google Ads: update docs

* Automated Change

* Source Google Ads: add unit tests

* Automated Change

* Empty-Commit

---------

Co-authored-by: artem1205 <artem1205@users.noreply.github.com>
  • Loading branch information
artem1205 and artem1205 committed May 18, 2023
1 parent fa98b67 commit 00c8e03
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ COPY main.py ./

ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]

LABEL io.airbyte.version=0.2.18
LABEL io.airbyte.version=0.2.19
LABEL io.airbyte.name=airbyte/source-google-ads
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data:
connectorSubtype: api
connectorType: source
definitionId: 253487c0-2246-43ba-a21f-5116b20a2c50
dockerImageTag: 0.2.18
dockerImageTag: 0.2.19
dockerRepository: airbyte/source-google-ads
githubIssueLabel: source-google-ads
icon: google-adwords.svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

import backoff
import pendulum
from airbyte_cdk.models import FailureType
from airbyte_cdk.utils import AirbyteTracedException
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.v13.services.types.google_ads_service import GoogleAdsRow, SearchGoogleAdsResponse
from google.api_core.exceptions import ServerError, TooManyRequests
from google.auth import exceptions
from proto.marshal.collections import Repeated, RepeatedComposite

REPORT_MAPPING = {
Expand Down Expand Up @@ -44,9 +47,17 @@ def __init__(self, credentials: MutableMapping[str, Any]):
# `google-ads` library version `14.0.0` and higher requires an additional required parameter `use_proto_plus`.
# More details can be found here: https://developers.google.com/google-ads/api/docs/client-libs/python/protobuf-messages
credentials["use_proto_plus"] = True
self.client = GoogleAdsClient.load_from_dict(credentials, version=API_VERSION)
self.client = self.get_google_ads_client(credentials)
self.ga_service = self.client.get_service("GoogleAdsService")

@staticmethod
def get_google_ads_client(credentials) -> GoogleAdsClient:
try:
return GoogleAdsClient.load_from_dict(credentials, version=API_VERSION)
except exceptions.RefreshError as e:
message = f"Config Error, Please Check permissions: {str(e)}"
raise AirbyteTracedException(message=message, failure_type=FailureType.config_error) from e

@backoff.on_exception(
backoff.expo,
(ServerError, TooManyRequests),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from datetime import date

import pendulum
import pytest
from airbyte_cdk.utils import AirbyteTracedException
from freezegun import freeze_time
from google.auth import exceptions
from pendulum.tz.timezone import Timezone
from source_google_ads.google_ads import GoogleAds
from source_google_ads.models import Customer
Expand Down Expand Up @@ -57,6 +60,13 @@ def test_google_ads_init(mocker):
assert google_client_mocker.load_from_dict.call_args[0][0] == EXPECTED_CRED


def test_google_ads_wrong_permissions(mocker):
mocker.patch("source_google_ads.google_ads.GoogleAdsClient.load_from_dict", side_effect=exceptions.RefreshError("invalid_grant"))
with pytest.raises(AirbyteTracedException) as e:
GoogleAds(**SAMPLE_CONFIG)
assert e.value.message == "Config Error, Please Check permissions: invalid_grant"


def test_send_request(mocker, customers):
mocker.patch("source_google_ads.google_ads.GoogleAdsClient.load_from_dict", return_value=MockGoogleAdsClient(SAMPLE_CONFIG))
mocker.patch("source_google_ads.google_ads.GoogleAdsClient.get_service", return_value=MockGoogleAdsService())
Expand Down
1 change: 1 addition & 0 deletions docs/integrations/sources/google-ads.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Due to a limitation in the Google Ads API which does not allow getting performan

| Version | Date | Pull Request | Subject |
|:---------|:-----------|:---------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------|
| `0.2.19` | 2023-05-15 | [26209](https://github.com/airbytehq/airbyte/pull/26209) | Handle Token Refresh errors as `config_error` |
| `0.2.18` | 2023-05-15 | [25947](https://github.com/airbytehq/airbyte/pull/25947) | Improve GAQL parser error message if multiple resources provided |
| `0.2.17` | 2023-05-11 | [25987](https://github.com/airbytehq/airbyte/pull/25987) | Categorized Config Errors Accurately |
| `0.2.16` | 2023-05-10 | [25965](https://github.com/airbytehq/airbyte/pull/25965) | Fix Airbyte date-time data-types |
Expand Down

0 comments on commit 00c8e03

Please sign in to comment.