Skip to content

Commit

Permalink
Restore HttpAvailabilityStrategy as default (revert #21488) (#21924)
Browse files Browse the repository at this point in the history
  • Loading branch information
erohmensing committed Feb 1, 2023
1 parent a8bdbe2 commit 19c05f7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 29 deletions.
6 changes: 6 additions & 0 deletions airbyte-cdk/python/airbyte_cdk/sources/streams/http/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import requests
import requests_cache
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources.streams.availability_strategy import AvailabilityStrategy
from airbyte_cdk.sources.streams.core import Stream, StreamData
from airbyte_cdk.sources.streams.http.availability_strategy import HttpAvailabilityStrategy
from requests.auth import AuthBase
from requests_cache.session import CachedSession

Expand Down Expand Up @@ -113,6 +115,10 @@ def retry_factor(self) -> float:
def authenticator(self) -> HttpAuthenticator:
return self._authenticator

@property
def availability_strategy(self) -> Optional[AvailabilityStrategy]:
return HttpAvailabilityStrategy()

@abstractmethod
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
"""
Expand Down
17 changes: 11 additions & 6 deletions airbyte-cdk/python/docs/concepts/http-streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,20 @@ be returned as a keyword argument.
The CDK defines an `AvailabilityStrategy` for a stream, which is used to perform the `check_availability` method. This method checks whether
the stream is available before performing `read_records`.

For HTTP streams, a `HttpAvailabilityStrategy` is defined, which attempts to read the first record of the stream, and excepts
For HTTP streams, a default `HttpAvailabilityStrategy` is defined, which attempts to read the first record of the stream, and excepts
a dictionary of known error codes and associated reasons, `reasons_for_unavailable_status_codes`. By default, this list contains only
`requests.status_codes.FORBIDDEN` (403), with an associated error message that tells the user that they are likely missing permissions associated with that stream.

You can use this `HttpAvailabilityStrategy` in your `HttpStream` by adding the following property to your stream class:
### Customizing stream availability

You can subclass `HttpAvailabilityStrategy` to override the `reasons_for_unavailable_status_codes` to except more HTTP error codes and inform the user how to resolve errors specific to your connector or stream.

### Disabling stream availability check

You can disable the `HttpAvailabilityStrategy` in your `HttpStream` by adding the following property to your stream class:

```python
@property
def availability_strategy(self) -> Optional[AvailabilityStrategy]:
return HttpAvailabilityStrategy()
```

You can also subclass `HttpAvailabilityStrategy` to override the list of known errors to except more error codes and inform the user how to resolve errors specific to your connector or stream.
return None
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import pytest
import requests
from airbyte_cdk.sources.declarative.checks.check_stream import CheckStream
from airbyte_cdk.sources.streams.availability_strategy import AvailabilityStrategy
from airbyte_cdk.sources.streams.http import HttpStream
from airbyte_cdk.sources.streams.http.availability_strategy import HttpAvailabilityStrategy

Expand Down Expand Up @@ -120,11 +119,6 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
yield stub_resp
pass

# TODO (Ella): Remove explicit definition when turning on default
@property
def availability_strategy(self) -> Optional["AvailabilityStrategy"]:
return HttpAvailabilityStrategy()

http_stream = MockHttpStream()
assert isinstance(http_stream, HttpStream)
assert isinstance(http_stream.availability_strategy, HttpAvailabilityStrategy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import requests
from airbyte_cdk.sources import AbstractSource
from airbyte_cdk.sources.streams import Stream
from airbyte_cdk.sources.streams.availability_strategy import AvailabilityStrategy
from airbyte_cdk.sources.streams.http.availability_strategy import HttpAvailabilityStrategy
from airbyte_cdk.sources.streams.http.http import HttpStream
from requests import HTTPError
Expand Down Expand Up @@ -40,11 +39,6 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
def retry_factor(self) -> float:
return 0.01

# TODO (Ella): Remove explicit definition when turning on default
@property
def availability_strategy(self) -> Optional["AvailabilityStrategy"]:
return HttpAvailabilityStrategy()


@pytest.mark.parametrize(
("status_code", "json_contents", "expected_is_available", "expected_messages"),
Expand Down
11 changes: 0 additions & 11 deletions airbyte-cdk/python/unit_tests/sources/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
Type,
)
from airbyte_cdk.sources import AbstractSource, Source
from airbyte_cdk.sources.streams.availability_strategy import AvailabilityStrategy
from airbyte_cdk.sources.streams.core import Stream
from airbyte_cdk.sources.streams.http.availability_strategy import HttpAvailabilityStrategy
from airbyte_cdk.sources.streams.http.http import HttpStream
Expand Down Expand Up @@ -496,11 +495,6 @@ def __init__(self, *args, **kvargs):
HttpStream.__init__(self, *args, kvargs)
self.read_records = mocker.MagicMock()

# TODO (Ella): Remove explicit definition when turning on default
@property
def availability_strategy(self) -> Optional["AvailabilityStrategy"]:
return HttpAvailabilityStrategy()

class MockStream(mocker.MagicMock, Stream):
page_size = None
get_json_schema = mocker.MagicMock()
Expand Down Expand Up @@ -554,11 +548,6 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
self.resp_counter += 1
yield stub_response

# TODO (Ella): Remove explicit definition when turning on default
@property
def availability_strategy(self) -> Optional["AvailabilityStrategy"]:
return HttpAvailabilityStrategy()

class MockStream(mocker.MagicMock, Stream):
page_size = None
get_json_schema = mocker.MagicMock()
Expand Down

0 comments on commit 19c05f7

Please sign in to comment.