garf-core 0.0.6__tar.gz → 0.0.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: garf-core
3
- Version: 0.0.6
3
+ Version: 0.0.8
4
4
  Summary: Abstracts fetching data from API based on provided SQL-like query.
5
5
  Author-email: "Google Inc. (gTech gPS CSE team)" <no-reply@google.com>
6
6
  License: Apache 2.0
@@ -12,4 +12,4 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- __version__ = '0.0.6'
15
+ __version__ = '0.0.8'
@@ -19,6 +19,7 @@ import abc
19
19
  import dataclasses
20
20
  from collections.abc import Sequence
21
21
 
22
+ import requests
22
23
  from typing_extensions import override
23
24
 
24
25
 
@@ -34,16 +35,40 @@ class GarfApiResponse:
34
35
  results: list
35
36
 
36
37
 
38
+ class GarfApiError(Exception):
39
+ """API specific exception."""
40
+
41
+
37
42
  class BaseClient(abc.ABC):
38
43
  """Base API client class."""
39
44
 
40
45
  @abc.abstractmethod
41
46
  def get_response(
42
- self, request: GarfApiRequest = GarfApiRequest()
47
+ self, request: GarfApiRequest = GarfApiRequest(), **kwargs: str
43
48
  ) -> GarfApiResponse:
44
49
  """Method for getting response."""
45
50
 
46
51
 
52
+ class RestApiClient(BaseClient):
53
+ """Specifies REST client."""
54
+
55
+ OK = 200
56
+
57
+ def __init__(self, endpoint: str, **kwargs: str) -> None:
58
+ """Initializes RestApiClient."""
59
+ self.endpoint = endpoint
60
+ self.query_args = kwargs
61
+
62
+ @override
63
+ def get_response(
64
+ self, request: GarfApiRequest = GarfApiRequest(), **kwargs: str
65
+ ) -> GarfApiResponse:
66
+ response = requests.get(f'{self.endpoint}/{request.resource_name}')
67
+ if response.status_code == self.OK:
68
+ return GarfApiResponse(response.json())
69
+ raise GarfApiError('Failed to get data from API')
70
+
71
+
47
72
  class FakeApiClient(BaseClient):
48
73
  """Fake class for specifying API client."""
49
74
 
@@ -38,21 +38,30 @@ class ApiReportFetcher:
38
38
 
39
39
  Attributes:
40
40
  api_client: a client used for connecting to API.
41
+ parser: Type of parser to convert API response.
42
+ query_specification_builder: Class to perform query parsing.
41
43
  """
42
44
 
43
45
  def __init__(
44
46
  self,
45
47
  api_client: api_clients.BaseApiClient,
46
48
  parser: parsers.BaseParser = parsers.ListParser,
49
+ query_specification_builder: query_editor.QuerySpecification = (
50
+ query_editor.QuerySpecification
51
+ ),
52
+ **kwargs: str,
47
53
  ) -> None:
48
54
  """Instantiates ApiReportFetcher based on provided api client.
49
55
 
50
56
  Args:
51
57
  api_client: Instantiated api client.
52
- parser: Parser.
58
+ parser: Type of parser to convert API response.
59
+ query_specification_builder: Class to perform query parsing.
53
60
  """
54
61
  self.api_client = api_client
55
62
  self.parser = parser()
63
+ self.query_specification_builder = query_specification_builder
64
+ self.query_args = kwargs
56
65
 
57
66
  async def afetch(
58
67
  self,
@@ -63,12 +72,12 @@ class ApiReportFetcher:
63
72
  """Asynchronously fetches data from API based on query_specification.
64
73
 
65
74
  Args:
66
- query_specification: Query text that will be passed to API
67
- alongside column_names, customizers and virtual columns.
68
- args: Arguments that need to be passed to the query.
75
+ query_specification: Query text that will be passed to API
76
+ alongside column_names, customizers and virtual columns.
77
+ args: Arguments that need to be passed to the query.
69
78
 
70
79
  Returns:
71
- GarfReport with results of query execution.
80
+ GarfReport with results of query execution.
72
81
  """
73
82
  return self.fetch(query_specification, args, **kwargs)
74
83
 
@@ -81,19 +90,19 @@ class ApiReportFetcher:
81
90
  """Fetches data from API based on query_specification.
82
91
 
83
92
  Args:
84
- query_specification: Query text that will be passed to API
85
- alongside column_names, customizers and virtual columns.
86
- args: Arguments that need to be passed to the query.
93
+ query_specification: Query text that will be passed to API
94
+ alongside column_names, customizers and virtual columns.
95
+ args: Arguments that need to be passed to the query.
87
96
 
88
97
  Returns:
89
- GarfReport with results of query execution.
98
+ GarfReport with results of query execution.
90
99
 
91
100
  Raises:
92
- GarfExecutorException:
93
- When customer_ids are not provided or API returned error.
101
+ GarfExecutorException:
102
+ When customer_ids are not provided or API returned error.
94
103
  """
95
104
  if not isinstance(query_specification, query_editor.QuerySpecification):
96
- query_specification = query_editor.QuerySpecification(
105
+ query_specification = self.query_specification_builder(
97
106
  text=str(query_specification),
98
107
  args=args,
99
108
  )
@@ -103,3 +112,26 @@ class ApiReportFetcher:
103
112
  return report.GarfReport(
104
113
  results=parsed_response, column_names=query.column_names
105
114
  )
115
+
116
+
117
+ class RestApiReportFetcher(ApiReportFetcher):
118
+ """Fetches data from an REST API endpoint.
119
+
120
+ Attributes:
121
+ api_client: Initialized RestApiClient.
122
+ parser: Type of parser to convert API response.
123
+ """
124
+
125
+ def __init__(
126
+ self,
127
+ endpoint: str,
128
+ parser: parsers.BaseParser = parsers.DictParser,
129
+ ) -> None:
130
+ """Instantiates RestApiReportFetcher.
131
+
132
+ Args:
133
+ endpoint: URL of API endpoint.
134
+ parser: Type of parser to convert API response.
135
+ """
136
+ self.api_client = api_clients.RestApiClient(endpoint)
137
+ self.parser = parser()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: garf-core
3
- Version: 0.0.6
3
+ Version: 0.0.8
4
4
  Summary: Abstracts fetching data from API based on provided SQL-like query.
5
5
  Author-email: "Google Inc. (gTech gPS CSE team)" <no-reply@google.com>
6
6
  License: Apache 2.0
@@ -11,5 +11,6 @@ garf_core/report_fetcher.py
11
11
  garf_core.egg-info/PKG-INFO
12
12
  garf_core.egg-info/SOURCES.txt
13
13
  garf_core.egg-info/dependency_links.txt
14
+ garf_core.egg-info/entry_points.txt
14
15
  garf_core.egg-info/requires.txt
15
16
  garf_core.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [garf]
2
+ rest = garf_core.report_fetcher
@@ -33,6 +33,9 @@ dynamic=["version"]
33
33
  [tool.setuptools.dynamic]
34
34
  version = {attr = "garf_core.__version__"}
35
35
 
36
+ [project.entry-points.garf]
37
+ rest = "garf_core.report_fetcher"
38
+
36
39
  [project.optional-dependencies]
37
40
  pandas=[
38
41
  "pandas",
File without changes
File without changes
File without changes