An R6 class that provides a base HTTP client for interacting with Azure APIs. This client handles authentication, request building, retry logic, logging, and error handling for Azure API requests.
Details
The api_client class is designed to be a base class for Azure service-specific
clients. It provides:
Automatic authentication using Azure credentials
Configurable retry logic with exponential backoff
Request and response logging
JSON, XML, and HTML content type handling
Standardized error handling
Public fields
.host_urlBase URL for the API
.base_reqBase httr2 request object
.providerCredential provider R6 object
.credentialsCredentials function for authentication
.optionsRequest options (timeout, connecttimeout, max_tries)
.response_handlerOptional callback function to process response content
Methods
Method new()
Create a new API client instance
Usage
api_client$new(
host_url,
provider = NULL,
credentials = NULL,
timeout = 60L,
connecttimeout = 30L,
max_tries = 5L,
response_handler = NULL
)Arguments
host_urlA character string specifying the base URL for the API (e.g.,
"https://management.azure.com").providerAn R6 credential provider object that inherits from the
CredentialorDefaultCredentialclass. If provided, the credential'sreq_authmethod will be used for authentication. Takes precedence overcredentials.credentialsA function that adds authentication to requests. If both
providerandcredentialsareNULL, usesdefault_non_auth(). The function should accept an httr2 request object and return a modified request with authentication.timeoutAn integer specifying the request timeout in seconds. Defaults to
60.connecttimeoutAn integer specifying the connection timeout in seconds. Defaults to
30.max_triesAn integer specifying the maximum number of retry attempts for failed requests. Defaults to
5.response_handlerAn optional function to process the parsed response content. The function should accept one argument (the parsed response) and return the processed content. If
NULL, usesdefault_response_handler()which converts data frames to data.table objects. Defaults toNULL.
Method .fetch()
Make an HTTP request to the API
Usage
api_client$.fetch(
path,
...,
query = NULL,
body = NULL,
headers = NULL,
method = "get",
verbosity = 0L,
content = c("body", "headers", "response", "request"),
content_type = NULL
)Arguments
pathA character string specifying the API endpoint path. Supports
rlang::englue()syntax for variable interpolation using named arguments passed via.......Named arguments used for path interpolation with
rlang::englue().queryA named list of query parameters to append to the URL.
bodyRequest body data. Sent as JSON in the request body. Can be a list or character string (JSON).
headersA named list of additional HTTP headers to include in the request.
methodA character string specifying the HTTP method. One of
"get","post","put","patch", or"delete". Defaults to"get".verbosityAn integer specifying the verbosity level for request debugging (passed to
httr2::req_perform()). Defaults to0.contentA character string specifying what to return. One of:
"body"(default): Return the parsed response body"headers": Return response headers"response": Return the full httr2 response object"request": Return the prepared request object without executing it
content_typeA character string specifying how to parse the response body. If
NULL, uses the response's Content-Type header. Common values:"application/json","application/xml","text/html".
Returns
Depends on the content parameter:
"body": Parsed response body (list, data.frame, or character)"headers": List of response headers"response": Fullhttr2::response()object"request":httr2::request()object
Method .resp_content()
Extract content from a response object
Arguments
respAn
httr2::response()objectcontentA character string specifying what to return. One of:
"body": Return the parsed response body"headers": Return response headers"response": Return the full httr2 response object
content_typeA character string specifying how to parse the response body. Only used when
content = "body". IfNULL, uses the response's Content-Type header.
Returns
Depends on the content parameter:
"body": Parsed response body (list, data.frame, or character)"headers": List of response headers"response": Fullhttr2::response()object
Method .build_request()
Build an HTTP request object
Usage
api_client$.build_request(
path,
...,
query = NULL,
body = NULL,
headers = NULL,
method = "get"
)Arguments
pathA character string specifying the API endpoint path. Supports
rlang::englue()syntax for variable interpolation using named arguments passed via.......Named arguments used for path interpolation with
rlang::englue().queryA named list of query parameters to append to the URL.
bodyRequest body data. Sent as JSON in the request body. Can be a list or character string (JSON).
headersA named list of additional HTTP headers to include in the request.
methodA character string specifying the HTTP method. One of
"get","post","put","patch", or"delete". Defaults to"get".
Returns
An httr2::request() object ready for execution
Method .send_request()
Perform an HTTP request and log the results
Arguments
reqAn
httr2::request()object to executeverbosityAn integer specifying the verbosity level for request debugging (passed to
httr2::req_perform()). Defaults to0.
Returns
An httr2::response() object containing the API response
Method .resp_body_content()
Extract and parse response content
Arguments
respAn
httr2::response()objectcontent_typeA character string specifying how to parse the response body. If
NULL, uses the response's Content-Type header. Common values:"application/json","application/xml","text/html".
Method .get_token()
Get authentication token from the credential provider
Returns
An httr2::oauth_token() object if a provider is available,
otherwise returns NULL with a warning.
Examples
if (FALSE) { # \dontrun{
# Create a client with default credentials
client <- api_client$new(
host_url = "https://management.azure.com"
)
# Create a client with a credential provider
cred_provider <- get_credential_provider(
scope = "https://management.azure.com/.default"
)
client <- api_client$new(
host_url = "https://management.azure.com",
provider = cred_provider
)
# Create a client with custom credentials function
client <- api_client$new(
host_url = "https://management.azure.com",
credentials = my_credential_function,
timeout = 120,
max_tries = 3
)
# Create a client with custom response handler
custom_handler <- function(content) {
# Custom processing logic - e.g., keep data frames as-is
content
}
client <- api_client$new(
host_url = "https://management.azure.com",
response_handler = custom_handler
)
# Make a GET request
response <- client$.fetch(
path = "/subscriptions/{subscription_id}/resourceGroups",
subscription_id = "my-subscription-id",
query = list(`api-version` = "2021-04-01"),
method = "get"
)
} # }