read_api
ReadAPIAction
¶
Bases: PipelineAction
Reads data from an API and loads it into a Spark DataFrame.
This action executes HTTP requests (optionally paginated) in parallel using the
APIReader and returns a DataFrame
containing the response payloads plus request/response metadata. No intermediate
files are written.
Example
Read API:
action: READ_API
options:
base_url: https://some_url.com/api/
endpoint: my/endpoint/
auth:
- type: basic
username: my_username
password: my_password
- type: env
header_template:
"X-API-Key": "<ENV_VAR_NAME>"
- type: secret_scope
secret_scope: my_secret_scope
header_template:
"X-ORG-Token": "<SECRET_NAME>"
- type: azure_oauth
client_id: my_client_id
client_secret: my_client_secret
tenant_id: my_tenant_id
scope: <entra-id-client-id>
ChainedAuth) so that headers from env/secret_scope
are merged and auth flows like Basic / Azure OAuth are applied to each request.
If the API returns a large JSON object but you only want a nested list (e.g. data.items):
Only page_based and limit_offset strategies are currently supported. You may also
supply the shared/advanced options check_field, next_page_field, max_page,
pages_per_array_limit, and preliminary_probe.
1) Page-Based Pagination
Read API:
action: READ_API
options:
base_url: https://some_url.com/api/
endpoint: items/
params:
page: 1 # starting page (optional; defaults to 1)
per_page: 100
pagination:
strategy: page_based
page_field: page # required
# Shared/advanced (optional):
check_field: results # e.g. list to check for emptiness
next_page_field: info.has_next # boolean flag; if present it is trusted
max_page: -1 # -1 = all pages
pages_per_array_limit: 2 # chunk output rows every 2 pages
preliminary_probe: false # set true to pre-scan/build all page params
2) Limit/Offset Pagination
Read API:
action: READ_API
options:
base_url: https://some_url.com/api/
endpoint: products/
params:
limit: 50
offset: 0
pagination:
strategy: limit_offset
limit_field: limit # required
offset_field: offset # required
# Shared/advanced (optional):
check_field: data.items
next_page_field: page_info.has_next
max_page: -1
pages_per_array_limit: -1
preliminary_probe: false
GET .../products/?limit=50&offset=0
GET .../products/?limit=50&offset=50
GET .../products/?limit=50&offset=100
...
Using preliminary_probe to pre-compute all pages
If preliminary_probe: true is set, the reader will first probe the API to determine
the final page (using check_field and/or next_page_field) and then fan out one request
per page/offset—useful when driving fully parallel execution:
When requests_from_context: true, distinct rows from the upstream context.data
are converted into individual requests (enabling heterogeneous endpoints/params).
The DataFrame must have columns: endpoint, params, headers, data, json_body.
# Upstream step produces rows like:
# | endpoint | params | headers | data | json_body |
# | "u/123/profile" | {"verbose": "true"} | null | null | null |
# | "u/456/profile" | {"verbose": "false"} | null | null | null |
Read API:
action: READ_API
options:
base_url: https://some_url.com/api/
requests_from_context: true
method: GET
timeout: 45
Output
The action returns a Spark DataFrame with one column json_response (ArrayType).
Each element contains:
{
"response": "<json string of the API payload (optionally reduced by 'key')>",
"__metadata": {
"timestamp": "YYYY-MM-DD HH:MM:SS.ssssss",
"base_url": "https://some_url.com/api/",
"url": "https://some_url.com/api/my/endpoint/?q=...",
"status_code": 200,
"reason": "OK",
"elapsed": 0.123,
"endpoint": "my/endpoint/",
"query_parameters": { "q": "..." }
}
}
pages_per_array_limit > 0, responses are chunked
into arrays of that many pages; otherwise all pages for a request are grouped together.
Validation & Errors:
- base_url must be provided.
- Either endpoint must be provided or requests_from_context must be true.
- If requests_from_context is true, context.data must be present and non-empty.
- Pagination config:
- strategy must be page_based or limit_offset (other strategies are not yet supported).
- For page_based, page_field is required.
- For limit_offset, both limit_field and offset_field are required.
Secret information
Don't write sensitive information like passwords or tokens directly in the pipeline configuration. Use secret scopes or environment variables instead.
Source code in src/cloe_nessy/pipeline/actions/read_api.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
run(context, *, base_url=None, auth=None, endpoint=None, default_headers=None, method='GET', key=None, timeout=30, params=None, headers=None, data=None, json_body=None, pagination=None, max_retries=0, backoff_factor=0, max_concurrent_requests=8, requests_from_context=False, **_)
¶
Executes API requests in parallel by using mapInPandas.
We do NOT write intermediate files; instead we directly return the responses as rows in a Spark DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PipelineContext
|
The pipeline context used to carry data between actions. |
required |
base_url
|
str | None
|
The base URL for all API requests. |
None
|
auth
|
Mapping[str, str | Mapping[str, str] | list[Mapping[str, str]]] | None
|
Authentication configuration, which may be a simple header map, a nested map for different auth scopes, or a list thereof. |
None
|
endpoint
|
str | None
|
The specific path to append to the base URL for this call. |
None
|
default_headers
|
dict[str, Any] | None
|
Headers to include on every request. |
None
|
method
|
str
|
HTTP method to use. |
'GET'
|
key
|
str | None
|
JSON field name to extract from each response. |
None
|
timeout
|
int
|
Request timeout in seconds. |
30
|
params
|
dict[str, Any] | None
|
Query parameters to append to the URL. |
None
|
headers
|
dict[str, Any] | None
|
Additional request-specific headers. |
None
|
data
|
dict[str, Any] | None
|
Form-encoded body to send. |
None
|
json_body
|
dict[str, Any] | None
|
JSON-encoded body to send. |
None
|
pagination
|
PaginationConfigData | None
|
Configuration for paginated endpoints. |
None
|
max_retries
|
int
|
Number of times to retry on failure. |
0
|
backoff_factor
|
int
|
Multiplier for retry backoff delays. |
0
|
max_concurrent_requests
|
int
|
Maximum number of parallel API calls. |
8
|
requests_from_context
|
bool
|
Whether to derive request parameters from context data. |
False
|
Returns:
| Type | Description |
|---|---|
PipelineContext
|
The updated context, with the read data as a DataFrame. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no base URL is provided. |
ValueError
|
If neither an endpoint nor context-derived requests are specified. |
ValueError
|
If context-derived requests are enabled but no data is present in context. |
Source code in src/cloe_nessy/pipeline/actions/read_api.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
process_auth(auth)
¶
Processes the auth parameter to create an AuthBase object.