| Title: | Token-Authenticated REST API Retrieval Toolkit |
|---|---|
| Description: | A small, dependency-light toolkit for talking to token-authenticated REST APIs. It manages authentication tokens in process environment variables (never written to disk), builds requests with configurable authentication and pagination strategies, and retrieves paginated data either one page at a time or in chunks combined into a single tibble. The design is API-agnostic: a single 'apifetch_api' profile describes an endpoint together with how it authenticates and paginates, so the same verbs work across different services. |
| Authors: | André Leite [aut, cre], Hugo Vasconcelos [aut], Diogo Bezerra [aut], Marcos Wasilew [aut], Carlos Amorin [aut] |
| Maintainer: | André Leite <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-07-02 21:18:56 UTC |
| Source: | https://github.com/cran/apifetch |
Bundles an endpoint URL with its authentication and pagination strategies and
a namespace service (used to look up tokens). The resulting object is passed
to af_fetch() and af_fetch_all().
af_api( endpoint, service = "apifetch", auth = af_auth_bearer(), pagination = af_paginate_offset(), drop_cols = character(0), connect_hint = NULL )af_api( endpoint, service = "apifetch", auth = af_auth_bearer(), pagination = af_paginate_offset(), drop_cols = character(0), connect_hint = NULL )
endpoint |
The base API URL. |
service |
Namespace used to look up the token (see |
auth |
An |
pagination |
An |
drop_cols |
Character vector of response columns to drop after parsing (e.g. a status column). Default none. |
connect_hint |
Optional extra line shown when a connection error occurs (e.g. a VPN requirement). |
An apifetch_api object.
af_api( endpoint = "https://www.bigdata.pe.gov.br/api/buscar", service = "BigDataPE", auth = af_auth_raw(), pagination = af_paginate_offset("header"), drop_cols = "Mensagem", connect_hint = "Ensure you are on the PE Conectado network or VPN." )af_api( endpoint = "https://www.bigdata.pe.gov.br/api/buscar", service = "BigDataPE", auth = af_auth_raw(), pagination = af_paginate_offset("header"), drop_cols = "Mensagem", connect_hint = "Ensure you are on the PE Conectado network or VPN." )
Constructors describing how a token is attached to a request. Pass the result
to the auth argument of af_api().
af_auth_raw(header = "Authorization") af_auth_bearer(header = "Authorization", prefix = "Bearer ") af_auth_header(header = "X-API-Key") af_auth_query(param = "api_key")af_auth_raw(header = "Authorization") af_auth_bearer(header = "Authorization", prefix = "Bearer ") af_auth_header(header = "X-API-Key") af_auth_query(param = "api_key")
header |
Header name to use. |
prefix |
String prepended to the token (bearer scheme). |
param |
Query-parameter name (query scheme). |
af_auth_raw(): send the token verbatim in a header (default
Authorization). This is what the Big Data PE API expects.
af_auth_bearer(): send "Bearer <token>" in the Authorization header.
af_auth_header(): send the token in an arbitrary header (e.g.
X-API-Key).
af_auth_query(): send the token as a URL query parameter.
An apifetch_auth object.
af_auth_raw() af_auth_bearer() af_auth_header("X-API-Key") af_auth_query("api_key")af_auth_raw() af_auth_bearer() af_auth_header("X-API-Key") af_auth_query("api_key")
Performs one authenticated request against an af_api() profile, applying its
pagination strategy, and returns the parsed body as a tibble. HTTP errors and
connection failures are translated into friendly cli messages.
af_fetch(api, name, limit = Inf, offset = 0L, query = list(), verbosity = 0L)af_fetch(api, name, limit = Inf, offset = 0L, query = list(), verbosity = 0L)
api |
An |
name |
The token name to authenticate with (looked up via the API's
|
limit |
Maximum number of records to request. Default |
offset |
Starting record. Default |
query |
A named list of additional query-string filters. Default empty. |
verbosity |
|
A tibble with the parsed response.
## Not run: api <- af_api("https://www.bigdata.pe.gov.br/api/buscar", service = "BigDataPE", auth = af_auth_raw(), pagination = af_paginate_offset("header")) af_store_token("dengue", "token", service = "BigDataPE") af_fetch(api, "dengue", limit = 50) ## End(Not run)## Not run: api <- af_api("https://www.bigdata.pe.gov.br/api/buscar", service = "BigDataPE", auth = af_auth_raw(), pagination = af_paginate_offset("header")) af_store_token("dengue", "token", service = "BigDataPE") af_fetch(api, "dengue", limit = 50) ## End(Not run)
Iteratively calls af_fetch() with an advancing offset, stopping when a
chunk comes back empty or total_limit is reached, then row-binds the chunks
into one tibble. Columns listed in the API profile's drop_cols are removed.
af_fetch_all( api, name, total_limit = Inf, chunk_size = 50000L, query = list(), verbosity = 0L )af_fetch_all( api, name, total_limit = Inf, chunk_size = 50000L, query = list(), verbosity = 0L )
api |
An |
name |
The token name to authenticate with (looked up via the API's
|
total_limit |
Maximum number of records to retrieve in total. Default
|
chunk_size |
Records to request per chunk. Default |
query |
A named list of additional query-string filters. Default empty. |
verbosity |
|
A tibble with all retrieved records.
## Not run: api <- af_api("https://www.bigdata.pe.gov.br/api/buscar", service = "BigDataPE", auth = af_auth_raw(), pagination = af_paginate_offset("header"), drop_cols = "Mensagem") af_fetch_all(api, "dengue", total_limit = 500, chunk_size = 100) ## End(Not run)## Not run: api <- af_api("https://www.bigdata.pe.gov.br/api/buscar", service = "BigDataPE", auth = af_auth_raw(), pagination = af_paginate_offset("header"), drop_cols = "Mensagem") af_fetch_all(api, "dengue", total_limit = 500, chunk_size = 100) ## End(Not run)
Retrieves the token stored for name under service. Returns NULL
(with a warning) rather than erroring when no token is found.
af_get_token(name, service = "apifetch")af_get_token(name, service = "apifetch")
name |
The identifier for this token (e.g. a dataset or resource name). |
service |
A namespace prefix grouping tokens for one API. Default
|
The token string, or NULL if not found.
token <- af_get_token("dengue", service = "BigDataPE")token <- af_get_token("dengue", service = "BigDataPE")
Returns the names (without the service prefix) of all tokens stored for a
given service in environment variables.
af_list_tokens(service = "apifetch")af_list_tokens(service = "apifetch")
service |
A namespace prefix grouping tokens for one API. Default
|
A character vector of token names, empty if none are found.
af_list_tokens(service = "BigDataPE")af_list_tokens(service = "BigDataPE")
Constructors describing how limit/offset are sent with a request. Pass
the result to the pagination argument of af_api().
af_paginate_offset( where = c("header", "query"), limit_param = "limit", offset_param = "offset" ) af_paginate_none()af_paginate_offset( where = c("header", "query"), limit_param = "limit", offset_param = "offset" ) af_paginate_none()
where |
Either |
limit_param, offset_param
|
Parameter names to use. |
af_paginate_offset(): send limit/offset either as HTTP headers
(default, as the Big Data PE API expects) or as URL query parameters.
af_paginate_none(): send no pagination parameters.
Non-positive or infinite values are omitted from the request.
An apifetch_pagination object.
af_paginate_offset("header") af_paginate_offset("query", limit_param = "per_page") af_paginate_none()af_paginate_offset("header") af_paginate_offset("query", limit_param = "per_page") af_paginate_none()
Removes the token stored for name under service. Does nothing (beyond a
warning) when no token is found.
af_remove_token(name, service = "apifetch")af_remove_token(name, service = "apifetch")
name |
The identifier for this token (e.g. a dataset or resource name). |
service |
A namespace prefix grouping tokens for one API. Default
|
Invisibly NULL; called for its side effect.
af_remove_token("dengue", service = "BigDataPE")af_remove_token("dengue", service = "BigDataPE")
Stores an authentication token in a process environment variable named
"<service>_<name>". The token is never written to disk. If a non-empty
variable with that name already exists, the function refuses to overwrite it.
af_store_token(name, token, service = "apifetch")af_store_token(name, token, service = "apifetch")
name |
The identifier for this token (e.g. a dataset or resource name). |
token |
The authentication token (character). |
service |
A namespace prefix grouping tokens for one API. Default
|
Invisibly NULL; called for its side effect.
bdpe <- af_store_token("dengue", "your-token-here", service = "BigDataPE")bdpe <- af_store_token("dengue", "your-token-here", service = "BigDataPE")
Appends a named list of query parameters to a base URL, URL-encoding both names and values and dropping parameters whose value is the empty string.
parse_queries(url, query_list)parse_queries(url, query_list)
url |
The base URL. |
query_list |
A named list of query parameters. |
The URL with the query string appended (or the base URL unchanged when there are no parameters to add).
parse_queries("https://example.com", list(a = "1", b = "2"))parse_queries("https://example.com", list(a = "1", b = "2"))