Skip to content

Commit

Permalink
[source-recharge] - Migrate to CDK v3.7.0 (#42076)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnilan authored and xiaohansong committed Jul 23, 2024
1 parent fba41d3 commit 36ba028
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data:
connectorBuildOptions:
baseImage: docker.io/airbyte/python-connector-base:2.0.0@sha256:c44839ba84406116e8ba68722a0f30e8f6e7056c726f447681bb9e9ece8bd916
definitionId: 45d2e135-2ede-49e1-939f-3e3ec357a65e
dockerImageTag: 2.2.0
dockerImageTag: 2.3.0
dockerRepository: airbyte/source-recharge
githubIssueLabel: source-recharge
icon: recharge.svg
Expand Down
8 changes: 4 additions & 4 deletions airbyte-integrations/connectors/source-recharge/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 = "2.2.0"
version = "2.3.0"
name = "source-recharge"
description = "Source implementation for Recharge."
authors = [ "Airbyte <contact@airbyte.io>",]
Expand All @@ -17,7 +17,7 @@ include = "source_recharge"

[tool.poetry.dependencies]
python = "^3.9,<3.12"
airbyte-cdk = "^2"
airbyte-cdk = "^3"
freezegun = "^1.4.0"

[tool.poetry.scripts]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
#

import logging
from typing import Optional, Union

from airbyte_cdk.sources.streams.http.error_handlers import HttpStatusErrorHandler
from airbyte_cdk.sources.streams.http.error_handlers.response_models import ErrorResolution, FailureType, ResponseAction
from requests import Response


class RechargeErrorHandler(HttpStatusErrorHandler):
def __init__(self, logger: logging.Logger) -> None:
self.logger = logger
super().__init__(logger=logger)

def interpret_response(self, response_or_exception: Optional[Union[Response, Exception]] = None) -> ErrorResolution:

if isinstance(response_or_exception, Response):
content_length = int(response_or_exception.headers.get("Content-Length", 0))
incomplete_data_response = response_or_exception.status_code == 200 and content_length > len(response_or_exception.content)
if incomplete_data_response:
return ErrorResolution(
response_action=ResponseAction.RETRY,
failure_type=FailureType.transient_error,
error_message="The response is incomplete, retrying the request.",
)

return super().interpret_response(response_or_exception)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import pendulum
import requests
from airbyte_cdk.sources.streams.http import HttpStream
from airbyte_cdk.sources.streams.http.error_handlers import ErrorHandler
from airbyte_cdk.sources.streams.http.requests_native_auth import TokenAuthenticator
from airbyte_cdk.sources.utils.transform import TransformConfig, TypeTransformer
from source_recharge.components.recharge_error_handler import RechargeErrorHandler


class ApiVersion(Enum):
Expand Down Expand Up @@ -113,12 +115,8 @@ def get_stream_data(self, response_data: Any) -> List[dict]:
else:
return [response_data]

def should_retry(self, response: requests.Response) -> bool:
content_length = int(response.headers.get("Content-Length", 0))
incomplete_data_response = response.status_code == 200 and content_length > len(response.content)
if incomplete_data_response:
return True
return super().should_retry(response)
def get_error_handler(self) -> ErrorHandler:
return RechargeErrorHandler(logger=self.logger)

def stream_slices(self, stream_state: Mapping[str, Any] = None, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
start_date_value = (stream_state or {}).get(self.cursor_field, self._start_date) if self.cursor_field else self._start_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest
import requests
from airbyte_cdk.sources.streams.http.error_handlers.response_models import ResponseAction
from source_recharge.source import Orders, RechargeTokenAuthenticator, SourceRecharge


Expand Down Expand Up @@ -69,23 +70,23 @@ def test_path(self, config, stream_cls, stream_type, expected) -> None:
assert expected == result

@pytest.mark.parametrize(
("http_status", "headers", "should_retry"),
("http_status", "headers", "expected_action"),
[
(HTTPStatus.OK, {"Content-Length": 256}, True),
(HTTPStatus.BAD_REQUEST, {}, False),
(HTTPStatus.TOO_MANY_REQUESTS, {}, True),
(HTTPStatus.INTERNAL_SERVER_ERROR, {}, True),
(HTTPStatus.FORBIDDEN, {}, False),
(HTTPStatus.OK, {"Content-Length": 256}, ResponseAction.RETRY),
(HTTPStatus.BAD_REQUEST, {}, ResponseAction.FAIL),
(HTTPStatus.TOO_MANY_REQUESTS, {}, ResponseAction.RATE_LIMITED),
(HTTPStatus.INTERNAL_SERVER_ERROR, {}, ResponseAction.RETRY),
(HTTPStatus.FORBIDDEN, {}, ResponseAction.FAIL),
],
)
def test_should_retry(self, config, http_status, headers, should_retry) -> None:
def test_should_retry(self, config, http_status, headers, expected_action) -> None:
response = requests.Response()
response.status_code = http_status
response._content = b""
response.headers = headers
stream = Orders(config, authenticator=None)
assert stream.should_retry(response) == should_retry

error_resolution = stream.get_error_handler().interpret_response(response)
error_resolution.response_action == expected_action

class TestFullRefreshStreams:
def generate_records(self, stream_name, count) -> Union[Mapping[str, List[Mapping[str, Any]]], Mapping[str, Any]]:
Expand Down
1 change: 1 addition & 0 deletions docs/integrations/sources/recharge.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ The Recharge connector should gracefully handle Recharge API limitations under n

| Version | Date | Pull Request | Subject |
|:--------|:-----------| :------------------------------------------------------- |:-------------------------------------------------------------------------------------------------------------------------------|
| 2.3.0 | 2024-07-17 | [42076](https://github.com/airbytehq/airbyte/pull/42076) | Migrate to CDK v3.7.0 |
| 2.2.0 | 2024-07-17 | [42075](https://github.com/airbytehq/airbyte/pull/42075) | Migrate to CDK v2.4.0 |
| 2.1.0 | 2024-07-17 | [42069](https://github.com/airbytehq/airbyte/pull/42069) | Migrate to CDK v1.8.0 |
| 2.0.6 | 2024-07-13 | [41748](https://github.com/airbytehq/airbyte/pull/41748) | Update dependencies |
Expand Down

0 comments on commit 36ba028

Please sign in to comment.