| Title: | Authenticated HTTP Request Client for the 'Hakai' API |
|---|---|
| Description: | Initializes a class that obtains API credentials and provides a method to use those credentials to make GET requests to the 'Hakai' API server. Usage instructions are documented at <https://hakaiinstitute.github.io/hakai-api/>. |
| Authors: | Sam Albers [aut, cre], Taylor Denouden [aut], Brett Johnson [aut], Nate Rosenstock [ctb], Chris Davis [ctb], Hakai Institute [cph] |
| Maintainer: | Sam Albers <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.5.9000 |
| Built: | 2026-06-07 06:45:10 UTC |
| Source: | https://github.com/HakaiInstitute/hakai-api-client-r |
Class to use to make authenticated API requests for Hakai data. Credentials can be provided via the HAKAI_API_TOKEN environment variable or through a credentials file.
api_rootThe api_root you are logged in to
new()
Log into Google to gain credential access to the API
Client$new( api_root = "https://portal.hakai.org/api", login_page = "https://portal.hakai.org/api-client-login", credentials_file = NULL )
api_rootOptional API base url to fetch data. Defaults to "https://portal.hakai.org/api"
login_pageOptional API login page url to display to user. Defaults to "https://portal.hakai.org/api-client-login"
credentials_fileOptional path to the credentials cache file. Defaults to a file in the user's data directory as determined by tools::R_user_dir()
Credentials can be provided in two ways: 1. Via the HAKAI_API_TOKEN environment variable (contains query string: "token_type=Bearer&access_token=...") 2. Via a credentials file (default: in user data directory via tools::R_user_dir()) The environment variable takes precedence if both are available.
A client instance
try( client <- Client$new() ) # Using environment variable Sys.setenv(HAKAI_API_TOKEN = "token_type=Bearer&access_token=TOKEN") try( client <- Client$new() ) # Using custom credentials file try( client <- Client$new(credentials_file = "/path/to/creds") )
get()
Send a GET request to the API
Client$get(endpoint_url, col_types = NULL)
endpoint_urlThe API endpoint url - can be a full URL or a relative path that will be appended to the api_root
col_typesa readr type specification
A dataframe of the requested data
try(client$get("/aco/views/projects"))
try(client$get("https://portal.hakai.org/api/aco/views/projects"))
get_stations()
Get recent sensor nodes and their locations as an sf object
Client$get_stations()
An sf object of sensor nodes and their locations
try(client$get_stations())
post()
Send a POST request to the API
Client$post(endpoint_url, rec_data)
endpoint_urlThe API endpoint url - can be a full URL or a relative path that will be appended to the api_root
rec_datadataframe, list, or other R data structure to send as part of the post request payload
post request response status code and description
put()
Send a PUT request to the API
Client$put(endpoint_url, rec_data)
endpoint_urlThe API endpoint url - can be a full URL or a relative path that will be appended to the api_root
rec_datadataframe, list, or other R data structure to send as part of the post request payload
PUT request response status code and description
patch()
Send a PATCH request to the API
Client$patch(endpoint_url, rec_data)
endpoint_urlThe API endpoint url - can be a full URL or a relative path that will be appended to the api_root
rec_datadataframe, list, or other R data structure to send as part of the post request payload
PATCH request response status code and description
remove_credentials()
Remove your cached login credentials to logout of the client
Client$remove_credentials()
try( client$remove_credentials() )
clone()
The objects of this class are cloneable with this method.
Client$clone(deep = FALSE)
deepWhether to make a deep clone.
## Not run: # Initialize a new client try( client <- Client$new() ) # Or use environment variable for token Sys.setenv(HAKAI_API_TOKEN = "token_type=Bearer&access_token=TOKEN") try( client <- Client$new() ) # Follow authorization prompts to log in # Retrieve some data. See <https://hakaiinstitute.github.io/hakai-api/> for options. try( projects_2020 <- client$get("/aco/views/projects?project_year=2020&fields=project_name") ) try( print(projects_2020) ) # # A tibble: 20 x 1 # project_name # <chr> # 1 Fountain FN # 2 Haig Glacier # 3 Fraser River - Chimney Creek West William Canyon # 4 Cruickshank WS # ... ## End(Not run) ## ------------------------------------------------ ## Method `Client$new` ## ------------------------------------------------ try( client <- Client$new() ) # Using environment variable Sys.setenv(HAKAI_API_TOKEN = "token_type=Bearer&access_token=TOKEN") try( client <- Client$new() ) # Using custom credentials file try( client <- Client$new(credentials_file = "/path/to/creds") ) ## ------------------------------------------------ ## Method `Client$get` ## ------------------------------------------------ try(client$get("/aco/views/projects")) try(client$get("https://portal.hakai.org/api/aco/views/projects")) ## ------------------------------------------------ ## Method `Client$get_stations` ## ------------------------------------------------ try(client$get_stations()) ## ------------------------------------------------ ## Method `Client$remove_credentials` ## ------------------------------------------------ try( client$remove_credentials() )## Not run: # Initialize a new client try( client <- Client$new() ) # Or use environment variable for token Sys.setenv(HAKAI_API_TOKEN = "token_type=Bearer&access_token=TOKEN") try( client <- Client$new() ) # Follow authorization prompts to log in # Retrieve some data. See <https://hakaiinstitute.github.io/hakai-api/> for options. try( projects_2020 <- client$get("/aco/views/projects?project_year=2020&fields=project_name") ) try( print(projects_2020) ) # # A tibble: 20 x 1 # project_name # <chr> # 1 Fountain FN # 2 Haig Glacier # 3 Fraser River - Chimney Creek West William Canyon # 4 Cruickshank WS # ... ## End(Not run) ## ------------------------------------------------ ## Method `Client$new` ## ------------------------------------------------ try( client <- Client$new() ) # Using environment variable Sys.setenv(HAKAI_API_TOKEN = "token_type=Bearer&access_token=TOKEN") try( client <- Client$new() ) # Using custom credentials file try( client <- Client$new(credentials_file = "/path/to/creds") ) ## ------------------------------------------------ ## Method `Client$get` ## ------------------------------------------------ try(client$get("/aco/views/projects")) try(client$get("https://portal.hakai.org/api/aco/views/projects")) ## ------------------------------------------------ ## Method `Client$get_stations` ## ------------------------------------------------ try(client$get_stations()) ## ------------------------------------------------ ## Method `Client$remove_credentials` ## ------------------------------------------------ try( client$remove_credentials() )