Skip to content

Commit 1a7aff0

Browse files
[core] chore: ensure that Rest and FakeApiReportFetcher are confirming ApiReportFetcher interface
* Always accept api_client as a first argument of __init__ * Add class methods for initializing clients (FakeApiReportFetcher.from_data and RestApiClient.from_endpoint)
1 parent 5c6e60f commit 1a7aff0

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

libs/garf_core/garf_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '0.1.0'
15+
__version__ = '0.1.1'

libs/garf_core/garf_core/fetchers/fake.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import logging
2222
import os
23+
from collections.abc import Sequence
24+
from typing import Any
2325

2426
from garf_core import (
2527
api_clients,
@@ -36,7 +38,7 @@ class FakeApiReportFetcher(report_fetcher.ApiReportFetcher):
3638

3739
def __init__(
3840
self,
39-
data: list[dict[str, parsers.ApiRowElement]] | None = None,
41+
api_client: api_clients.FakeApiClient | None = None,
4042
parser: parsers.BaseParser = parsers.DictParser,
4143
query_specification_builder: query_editor.QuerySpecification = (
4244
query_editor.QuerySpecification
@@ -46,29 +48,33 @@ def __init__(
4648
json_location: str | os.PathLike[str] | None = None,
4749
**kwargs: str,
4850
) -> None:
49-
if not data and not (
51+
if not api_client and not (
5052
data_location := json_location or csv_location or data_location
5153
):
5254
raise report_fetcher.ApiReportFetcherError(
5355
'Missing fake data for the fetcher.'
5456
)
55-
api_client = (
56-
api_clients.FakeApiClient(data)
57-
if data
58-
else api_clients.FakeApiClient.from_file(data_location)
59-
)
57+
if not api_client:
58+
api_client = api_clients.FakeApiClient.from_file(data_location)
6059
super().__init__(api_client, parser, query_specification_builder, **kwargs)
6160

61+
@classmethod
62+
def from_data(cls, data: Sequence[dict[str, Any]]) -> FakeApiReportFetcher:
63+
"""Initializes FakeApiReportFetcher from a sequence of data."""
64+
return FakeApiReportFetcher(
65+
api_client=api_clients.FakeApiClient(results=data)
66+
)
67+
6268
@classmethod
6369
def from_csv(
6470
cls, file_location: str | os.PathLike[str]
6571
) -> FakeApiReportFetcher:
66-
"""Initialized FakeApiReportFetcher from a csv file."""
72+
"""Initializes FakeApiReportFetcher from a csv file."""
6773
return FakeApiReportFetcher(csv_location=file_location)
6874

6975
@classmethod
7076
def from_json(
7177
cls, file_location: str | os.PathLike[str]
7278
) -> FakeApiReportFetcher:
73-
"""Initialized FakeApiReportFetcher from a json file."""
79+
"""Initializes FakeApiReportFetcher from a json file."""
7480
return FakeApiReportFetcher(json_location=file_location)

libs/garf_core/garf_core/fetchers/rest.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ class RestApiReportFetcher(report_fetcher.ApiReportFetcher):
4040

4141
def __init__(
4242
self,
43-
endpoint: str,
43+
api_client: api_clients.RestApiClient | None = None,
4444
parser: parsers.BaseParser = parsers.DictParser,
4545
query_specification_builder: query_editor.QuerySpecification = (
4646
query_editor.QuerySpecification
4747
),
48+
endpoint: str | None = None,
4849
**kwargs: str,
4950
) -> None:
5051
"""Instantiates RestApiReportFetcher.
@@ -54,5 +55,17 @@ def __init__(
5455
parser: Type of parser to convert API response.
5556
query_specification_builder: Class to perform query parsing.
5657
"""
57-
api_client = api_clients.RestApiClient(endpoint)
58+
if not api_client and not endpoint:
59+
raise report_fetcher.ApiReportFetcherError(
60+
'Missing api_client or endpoint for the fetcher.'
61+
)
62+
if not api_client:
63+
api_client = api_clients.RestApiClient(endpoint)
5864
super().__init__(api_client, parser, query_specification_builder, **kwargs)
65+
66+
@classmethod
67+
def from_endpoint(cls, endpoint: str) -> RestApiReportFetcher:
68+
"""Initializes RestApiReportFetcher: from an API endpoint."""
69+
return RestApiReportFetcher(
70+
api_client=api_clients.RestApiClient(endpoint=endpoint)
71+
)

libs/garf_core/tests/unit/fetchers/test_fake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TestFakeApiReportFetcher:
2727
]
2828

2929
def test_fetch_from_data_returns_correct_result(self):
30-
fetcher = FakeApiReportFetcher(data=self.data)
30+
fetcher = FakeApiReportFetcher.from_data(self.data)
3131

3232
result = fetcher.fetch(
3333
'SELECT field1.subfield AS column_name FROM resource_name'

0 commit comments

Comments
 (0)