Skip to content

Commit

Permalink
Source HubSpot: retry when attempting to get scopes (#38024)
Browse files Browse the repository at this point in the history
Co-authored-by: cristina.mariscal <cristina.mariscal@cristina.mariscal--MacBook-Pro---DFJ27FJFXX>
  • Loading branch information
cmm-airbyte and cristina.mariscal authored May 30, 2024
1 parent 1d36fa9 commit d279f54
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data:
connectorSubtype: api
connectorType: source
definitionId: 36c891d9-4bd9-43ac-bad2-10e12756272c
dockerImageTag: 4.2.0
dockerImageTag: 4.2.1
dockerRepository: airbyte/source-hubspot
documentationUrl: https://docs.airbyte.com/integrations/sources/hubspot
githubIssueLabel: source-hubspot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = [ "poetry-core>=1.0.0",]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
version = "4.2.0"
version = "4.2.1"
name = "source-hubspot"
description = "Source implementation for HubSpot."
authors = [ "Airbyte <contact@airbyte.io>",]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#

import logging
import traceback
from http import HTTPStatus
from itertools import chain
from typing import Any, Generator, List, Mapping, Optional, Tuple
from typing import Any, Generator, List, Mapping, Optional, Tuple, Union

import requests
from airbyte_cdk.models import FailureType
from airbyte_cdk.sources import AbstractSource
from airbyte_cdk.sources.streams import Stream
from airbyte_cdk.sources.streams.http import HttpClient
from airbyte_cdk.sources.streams.http.error_handlers import ErrorResolution, HttpStatusErrorHandler, ResponseAction
from requests import HTTPError
from source_hubspot.errors import HubspotInvalidAuth
from source_hubspot.streams import (
Expand Down Expand Up @@ -97,7 +100,16 @@ def get_granted_scopes(self, authenticator):
try:
access_token = authenticator.get_access_token()
url = f"https://api.hubapi.com/oauth/v1/access-tokens/{access_token}"
response = requests.get(url=url)
error_resolution = ErrorResolution(
ResponseAction.RETRY, FailureType.transient_error, "Internal error attempting to get scopes."
)
error_mapping = {500: error_resolution, 502: error_resolution, 504: error_resolution}
http_client = HttpClient(
name="get hubspot granted scopes client",
logger=self.logger,
error_handler=HttpStatusErrorHandler(logger=self.logger, error_mapping=error_mapping),
)
request, response = http_client.send_request("get", url, request_kwargs={})
response.raise_for_status()
response_json = response.json()
granted_scopes = response_json["scopes"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,19 @@ def test_get_granted_scopes(requests_mock, mocker):

assert expected_scopes == actual_scopes

def test_get_granted_scopes_retry(requests_mock, mocker):
authenticator = mocker.Mock()
expected_token = "the-token"
authenticator.get_access_token.return_value = expected_token
mock_url = f"https://api.hubapi.com/oauth/v1/access-tokens/{expected_token}"
response = [
{"json": {}, "status_code": 500},
]

requests_mock.register_uri("GET", mock_url, response)
actual_scopes = SourceHubspot().get_granted_scopes(authenticator)
assert len(requests_mock.request_history) > 1


def test_streams_oauth_2_auth_no_suitable_scopes(requests_mock, mocker, config):
authenticator = mocker.Mock()
Expand Down
1 change: 1 addition & 0 deletions docs/integrations/sources/hubspot.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ The connector is restricted by normal HubSpot [rate limitations](https://legacyd

| Version | Date | Pull Request | Subject |
|:--------|:-----------| :------------------------------------------------------- |:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 4.2.1 | 2024-05-30 | [38024](https://github.com/airbytehq/airbyte/pull/38024) | etry when attempting to get scopes |
| 4.2.0 | 2024-05-24 | [38049](https://github.com/airbytehq/airbyte/pull/38049) | Add resumable full refresh support to `contacts_form_submissions` and `contacts_merged_audit` streams |
| 4.1.5 | 2024-05-17 | [38243](https://github.com/airbytehq/airbyte/pull/38243) | Replace AirbyteLogger with logging.Logger |
| 4.1.4 | 2024-05-16 | [38286](https://github.com/airbytehq/airbyte/pull/38286) | Added default schema normalization for the `Tickets` stream, to ensure the data types |
Expand Down

0 comments on commit d279f54

Please sign in to comment.