# Constants
url_body <- paste0(url_api, "body/")
#' @title Get Body Fat Logs
#'
#' @description
#' \code{get_body_fat_logs()} retrieves a list of all user's body fat log entries for a given day.
#'
#' @inheritParams inheritparams_token
#' @param date The end date of the period specified in the format yyyy-MM-dd or today.
#' @param base_date The range start date, in the format yyyy-MM-dd or today.
#' @param period The range for which data will be returned. Options are 1d, 7d, 30d, 1w, or 1m.
#' @param end_date The end date of the range. The period must not be longer than 31 days.
#' @inheritParams inheritparams_simplify
#'
#' @export
get_body_fat_logs <- function(token, date="", base_date="", period="", end_date="", simplify=TRUE)
{
url <- if(date != "" && period != ""){
paste0(url_body, sprintf("log/fat/date/%s/%s.json", format_date(date), period))
} else if(base_date != "" & end_date != ""){
paste0(url_body, sprintf("log/fat/date/%s/%s.json", format_date(base_date), format_date(end_date)))
} else if(date != ""){
paste0(url_body, sprintf("log/fat/date/%s.json", format_date(date)))
}
tidy_output(get(url, token), simplify)
}
#' @title Log Body Fat
#'
#' @description
#' \code{log_body_fat()} creates a log entry for body fat.
#'
#' @inheritParams inheritparams_token
#' @param fat Body fat; in the format X.XX, in the unit system that corresponds to the Accept-Language header provided.
#' @param date Log entry date; in the format yyyy-MM-dd.
#' @param time Time of the measurement; hours and minutes in the format HH:mm:ss, set to the last second of the day if not provided.
#'
#' @export
log_body_fat <- function(token, fat, date, time=NULL)
{
url <- paste0(url_body, "log/fat.json")
body <- list(fat=fat, date=format_date(date), time=time)
tidy_output(post(url, token, body=body), simplify=TRUE)
}
#' @title Delete Body Fat Log
#'
#' @description
#' \code{delete_body_fat_log()} deletes a user's body fat log entry with the given ID.
#'
#' @inheritParams inheritparams_token
#' @param log_id The ID of the body fat log entry.
#'
#' @export
delete_body_fat_log <- function(token, log_id)
{
url <- paste0(url_body, sprintf("log/fat/%s.json", as.character(log_id)))
invisible(delete(url, token))
}
#' @title Get Body Time Series
#'
#' @description
#' \code{get_body_time_series()} returns time series data in the specified range for a given resource.
#'
#' @inheritParams inheritparams_token
#' @param resource_path The resource path. Options are "bmi", "fat", or "weight".
#' @param base_date The range start date, in the format yyyy-MM-dd or today.
#' @param end_date The end date of the range.
#' @param date The end date of the period specified in the format yyyy-MM-dd or today.
#' @param period The range for which data will be returned. Options are 1d, 7d, 30d, 1w, 1m, 3m, 6m, 1y, or max.
#'
#' @export
get_body_time_series <- function(token, resource_path, base_date=NULL, end_date=NULL, date=NULL, period=NULL)
{
url <- if(!is.null(date) && !is.null(period)){
paste0(url_body, sprintf("%s/date/%s/%s.json", resource_path, format_date(date), period))
} else if(!is.null(base_date) && !is.null(end_date)){
paste0(url_body, sprintf("%s/date/%s/%s.json", resource_path, format_date(base_date), format_date(end_date)))
} else {
stop("Please supply either base_date and end_date or date and period parameters.")
}
get(url, token)[[1]]
}
#' @title Get Body Goals
#'
#' @description
#' \code{get_body_goals()} retrieves a user's current body fat percentage or weight goal.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param goal_type weight or fat
#'
#' @export
get_body_goals <- function(token, goal_type)
{
if (!(goal_type %in% c("weight", "fat"))) {
stop("goal_type must be one of 'weight' or 'fat'")
}
url <- paste0(url_body, sprintf("log/%s/goal.json", goal_type))
get(url, token)
}
#' @title Update Body Fat Goal
#'
#' @description
#' \code{update_body_fat_goal()} creates or updates user's fat percentage goal.
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param fat (required) Target body fat percentage; in the format X.XX.
#'
#' @export
update_body_fat_goal <- function(token, fat)
{
url <- paste0(url_body, "log/fat/goal.json")
post(url, token, body=list(fat=fat))
}
#' @title Update Weight Goal
#'
#' @description
#' \code{update_weight_goal()} creates or updates user's fat or weight goal
#'
#' @param token An OAuth 2.0 token generated by oauth_token()
#' @param goal_weight (required/optional) Target weight in the unit systems that corresponds to the Accept-Language header provided in the format requested. Required if user does not have an existing weight goal.
#' @param start_weight (required) Starting weight in the unit systems that corresponds to the Accept-Language header provided in the format requested.
#' @param start_date Weight goal start date; in the format yyyy-MM-dd.
#'
#' @export
update_weight_goal <- function(token, goal_weight=NULL, start_weight, start_date)
{
url <- paste0(url_body, "log/weight/goal.json")
body <- if(!is.null(goal_weight)){
list(weight=goal_weight, startWeight=start_weight, startDate=format_date(start_date))
} else {
list(startWeight=start_weight, startDate=format_date(start_date))
}
post(url, token, body=list)
}
#' @title Get Weight Logs
#'
#' @description
#' \code{get_weight_logs()} returns time series data in the specified range for a given resource.
#'
#' @inheritParams inheritparams_token
#' @param date The end date of the period specified in the format yyyy-MM-dd or today.
#' @param base_date The range start date, in the format yyyy-MM-dd or today.
#' @param period The range for which data will be returned. Options are 1d, 7d, 30d, 1w, or 1m.
#' @param end_date The end date of the range. The period must not be longer than 31 days.
#'
#' @export
get_weight_logs <- function(token, date=NULL, base_date=NULL, period=NULL, end_date=NULL)
{
url <- if(!is.null(date) && !is.null(period)){
paste0(url_body, sprintf("log/weight/date/%s/%s.json", format_date(date), period))
} else if(!is.null(base_date) && !is.null(end_date)){
paste0(url_body, sprintf("log/weight/date/%s/%s.json", format_date(base_date), format_date(end_date)))
} else if(!is.null(date)){
paste0(url_body, sprintf("log/weight/date/%s.json", format_date(date)))
}
get(url, token)[[1]]
}
#' @title Log Weight
#'
#' @description
#' \code{log_weight()} creates log entry for a body weight and get a response in the format requested.
#'
#' @inheritParams inheritparams_token
#' @param weight Weight - in the format X.XX.
#' @param date Log entry date - in the format yyyy-MM-dd.
#' @param time Time of the measurement - hours and minutes in the format HH:mm:ss, which is set to the last second of the day if time is not provided.
#'
#' @export
log_weight <- function(token, weight, date, time=NULL)
{
url <- paste0(url_body, "log/weight.json")
body <- if (!is.null(time)){
list(weight=weight, date=format_date(date), time=time)
} else {
list(weight=weight, date=format_date(date))
}
post(url, token, body=body)
}
#' @title Delete Weight Log
#'
#' @description
#' \code{delete_weight_log()} deletes a user's body weight log entry with the given ID.
#'
#' @inheritParams inheritparams_token
#' @param log_id The ID of the weight log entry.
#'
#' @export
delete_weight_log <- function(token, log_id)
{
url <- paste0(url_body, sprintf("log/weight/%s.json", as.character(log_id)))
delete(url, token)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.