R/forceframe_reps_by_id.R

Defines functions get_forceframe_reps_by_id

Documented in get_forceframe_reps_by_id

#' Get ForceFrame repetitions by test ID
#'
#' Calls the ForceFrame API endpoint `/tests/{testId}/repetitions` to retrieve
#' repetition records for a single test.
#'
#' @param ... The GUID of the ForceFrame test. Input as a string.
#' @return A data frame containing repetition details, or an empty data frame if none are found.
#' Internal function (not designed to be used directly by end users)
#' @keywords internal
get_forceframe_reps_by_id <- function(...) {
    # Argument validation
    test_id <- .validate_single_guid(...)

    # Build API request
    config <- get_config(quiet = TRUE)
    access_token <- authenticate()

    url <- paste0(
        config$endpoints$forceframe,
        "/tests/",
        utils::URLencode(test_id, reserved = TRUE),
        "/repetitions"
    )
    query_params <- list(tenantId = config$tenant_id)

    response <- tryCatch(
        httr::GET(url, query = query_params, .add_vald_headers(access_token)),
        error = function(e) {
            stop("Failed to connect to the ForceFrame API: ", e$message, call. = FALSE)
        }
    )

    # Consistent response handling
    .handle_api_response(response)

    if (response$status_code == 204) {
        message("No repetitions found for the given test ID.")
        return(data.frame())
    }

    body_txt <- httr::content(response, as = "text", encoding = "UTF-8")
    parsed <- tryCatch(
        jsonlite::fromJSON(body_txt, simplifyVector = FALSE),
        error = function(e) {
            stop("Failed to parse JSON from API: ", e$message, call. = FALSE)
        }
    )

    if (length(parsed) == 0L) {
        return(data.frame())
    }

    # Build dataframe using helper function
    .build_forceframe_reps_df(parsed)
}

Try the valdr package in your browser

Any scripts or data that you put into this service are public.

valdr documentation built on April 22, 2026, 9:08 a.m.