#' Base api input
#'
#' Minimum required for api request
#'
#' @param address request address
#' @param params Set to true to include empty parameter list
#'
#' @return basic input call
input_base <- function(address, params = TRUE){
input_raw <- list(method = jsonlite::unbox(address))
if(params){
input_raw$params <- list()
}
return(input_raw)
}
#' API filter
#'
#' Minimum required for api request
#'
#' @param marketTypeCodes Markets
#' @param marketCountries Country (code: eg "GB" of c("GB", "IRE"))
#' @param eventTypeIds The event IDs
#' @param inPlay Is the market in play
#' @param from Start date "YYYY-MM-DD" (or date and time "YYYY-MM-DD 00:00:00") filter
#' @param to End date "YYYY-MM-DD" (or date and time "YYYY-MM-DD 00:00:00") filter
#'
#'
#' @return filter list for api requests
api_filter <- function(eventTypeIds = NULL,
marketTypeCodes = NULL,
marketCountries = NULL,
inPlay = NULL,
from = NULL, to = NULL){
filter <- list()
filter$eventTypeIds <- eventTypeIds
filter$marketTypeCodes <- marketTypeCodes
filter$marketCountries <- marketCountries
filter$inPlayOnly <- jsonlite::unbox(inPlay)
if(!is.null(from) | !is.null(to)){
filter$marketStartTime <- api_starttime(from, to)
}
return(filter)
}
#' Make call
#'
#' Formats and submits call to api
#'
#' @param postfields Input fields
#' @param headers Input headers
#'
#' @return Login headers list
post <- function(postfields, headers){
call <- httr::POST(url = "https://api.betfair.com/exchange/betting/json-rpc/v1",
body = postfields,
httr::add_headers(unlist(headers)))
raw_result <- httr::content(call, as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON(flatten = TRUE)
return(raw_result)
}
#' API filter time period
#'
#' Adds time periods to filter
#'
#' @param from Start date/time
#' @param to End date/time
#'
#' @return period filter
api_starttime <- function(from, to){
marketStartTime <- list()
marketStartTime$from <- jsonlite::unbox(api_date(from))
marketStartTime$to <- jsonlite::unbox(api_date(to))
return(marketStartTime)
}
#' API date
#'
#' Format date for api requests
#'
#' @param date Date "YYYY-MM-DD" (or date and time "YYYY-MM-DD 00:00:00")
#'
#' @return formatted date
api_date <- function(date){
if(is.null(date)) return(NULL)
format(as.POSIXct(date), "%Y-%m-%dT%TZ")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.