|
| 1 | +# ApiReportFetcher |
| 2 | + |
| 3 | +## Initialization |
| 4 | + |
| 5 | +### Api Client |
| 6 | +To initialize `ApiReportFetcher` you need an instance of an API client to |
| 7 | +interact with an API. You can choose from [built-in](api-client.md) API clients |
| 8 | +or [create](../development/create-api-client.md) your own. |
| 9 | +```python |
| 10 | +from garf_core import ApiReportFetcher |
| 11 | + |
| 12 | +report_fetcher = ApiReportFetcher(api_client) |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +### Parser |
| 17 | + |
| 18 | +Under the hood `ApiReportFetcher` fetches data from an API as list of |
| 19 | +dictionaries and tries to access elements in each dictionary via `DictParser`. |
| 20 | + |
| 21 | +You can overwrite this behaviour by using one of [built-in parsers](parsers.md) |
| 22 | +or [implementing](../development/create-api-response-parser.md) your own. |
| 23 | + |
| 24 | +Suppose you want to use `NumericConverterDictParser` to automatically convert |
| 25 | +strings to int/float whenever possible. |
| 26 | + |
| 27 | + |
| 28 | +```python |
| 29 | +from garf_core import ApiReportFetcher |
| 30 | +from garf_core.parsers import NumericConverterDictParser |
| 31 | + |
| 32 | +report_fetcher = ApiReportFetcher( |
| 33 | + api_client=api_client, |
| 34 | + parser=NumericConverterDictParser |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +### Built-in queries |
| 40 | + |
| 41 | +Some queries for a particular API can be quite common so you want to create one |
| 42 | +or several [built-in queries](queries.md#built-in-queries). |
| 43 | + |
| 44 | +You can specified them in `builtin_queries` parameters during `ApiReportFetcher` |
| 45 | +initialization. |
| 46 | + |
| 47 | +```python |
| 48 | +from garf_core import ApiReportFetcher |
| 49 | +from garf_core.report import GarfReport |
| 50 | +from garf_core.parsers import NumericConverterDictParser |
| 51 | + |
| 52 | +def builtin_query(fetcher: ApiReportFetcher) -> GarfReport: |
| 53 | + return fetcher.fetch('SELECT field FROM resource') |
| 54 | + |
| 55 | + |
| 56 | +builtin_queries = {'my_query': builtin_query} |
| 57 | + |
| 58 | +report_fetcher = ApiReportFetcher( |
| 59 | + api_client=api_client, |
| 60 | + builtin_queries=builtin_queries |
| 61 | +) |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +## Fetching |
| 66 | +To fetch data from an API use `fetch` method. |
| 67 | + |
| 68 | +```python |
| 69 | +from garf_core import ApiReportFetcher |
| 70 | + |
| 71 | +report_fetcher = ApiReportFetcher(api_client) |
| 72 | +query = 'SELECT metric FROM resource' |
| 73 | +report = report_fetcher.fetch(query) |
| 74 | +``` |
| 75 | + |
| 76 | +`fetch` method returns `GarfReport` which can be [processed](reports.md) in Python |
| 77 | +or [written](writers.md) to local / remote storage. |
| 78 | + |
| 79 | +### Parametrization |
| 80 | + |
| 81 | +If your query contains [macros](queries.md/#macros) or [templates](queries.md#templates), you need to pass values for them via `args` parameters. |
| 82 | + |
| 83 | +```python |
| 84 | +from garf_core import ApiReportFetcher |
| 85 | +from garf_core.query_editor import GarfQueryParameters |
| 86 | + |
| 87 | +report_fetcher = ApiReportFetcher(api_client) |
| 88 | + |
| 89 | +query = 'SELECT metric FROM resource WHERE dimension={dimension}' |
| 90 | + |
| 91 | +query_parameters = GarfQueryParameters( |
| 92 | + macro={'dimension': 'value'}, |
| 93 | + template={'dimension': 'value'}, |
| 94 | +) |
| 95 | + |
| 96 | +report = report_fetcher.fetch(query, args=query_parameters) |
| 97 | +``` |
| 98 | + |
| 99 | +!!! note |
| 100 | + You can pass a dictionary instead of `GarfQueryParameters`. |
| 101 | + |
| 102 | + ```python |
| 103 | + |
| 104 | + query_parameters = { |
| 105 | + 'macro': { |
| 106 | + 'dimension': 'value', |
| 107 | + }, |
| 108 | + 'template': { |
| 109 | + 'dimension': 'value', |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + report = report_fetcher.fetch(query, args=query_parameters) |
| 114 | + ``` |
| 115 | + |
| 116 | + |
| 117 | +## Built-in report fetchers |
| 118 | + |
| 119 | +To simplify testing and working with REST APIs `garf` has two built-in report fetchers: |
| 120 | + |
| 121 | +* `FakeApiReportFetcher` - simulates API response based on provided data |
| 122 | +* `RestApiReportFetcher` - interacts with APIs with REST interface. |
| 123 | + |
| 124 | +### Fake |
| 125 | + |
| 126 | +`FakeApiReportFetcher` is based on [`FakeApiClient`](api-client.md#fake). |
| 127 | + |
| 128 | +It's ideal for prototyping and testing APIs without interacting with them directly. |
| 129 | + |
| 130 | +```python |
| 131 | +from garf_core.fetchers import FakeApiReportFetcher |
| 132 | + |
| 133 | +fake_data = [ |
| 134 | + {'field1': {'subfield': 1}, 'field2': 2}, |
| 135 | + {'field1': {'subfield': 10}, 'field2': 2}, |
| 136 | +] |
| 137 | +report_fetcher = FakeApiReportFetcher.from_data(fake_data) |
| 138 | + |
| 139 | +query = 'SELECT field1.subfield AS column FROM resource' |
| 140 | +report = report_fetcher.fetch(query) |
| 141 | +``` |
| 142 | + |
| 143 | +!!!note |
| 144 | + Instead providing data directly you can use helper methods - `from_json` and `from_csv`: |
| 145 | + |
| 146 | + ```python |
| 147 | + report_fetcher = FakeApiReportFetcher.from_json('path/to/json') |
| 148 | + report_fetcher = FakeApiReportFetcher.from_csv('path/to/csv') |
| 149 | + |
| 150 | + ``` |
| 151 | + |
| 152 | +### Rest |
| 153 | + |
| 154 | +`RestApiReportFetcher` is based on [`RestApiClient`](api-client.md#rest). |
| 155 | + |
| 156 | +It's can be used with any API that provides REST interface. |
| 157 | + |
| 158 | +You need to provide `endpoint` parameter which specifies root level address where |
| 159 | +API exists. |
| 160 | + |
| 161 | +When writing queries specify relative address of the resource you want to fetch from |
| 162 | +(i.e. `customers/1/orders`). |
| 163 | + |
| 164 | +```python |
| 165 | +from garf_core.fetchers import RestApiReportFetcher |
| 166 | + |
| 167 | +endpoint= 'https://api.restful-api.dev' |
| 168 | +report_fetcher = RestApiReportFetcher.from_endpoint(endpoint) |
| 169 | + |
| 170 | +query = 'SELECT id FROM objects' |
| 171 | +report = report_fetcher.fetch(query) |
| 172 | +``` |
0 commit comments