R/get_team_standing.R

#' Returns a data.frame of teams and their statistics
#'
#' @param season Season to be used, i.e. '2015' for the SHL season 2015/2016
#' @param team_id An ID for the team which the list should be based on, for example "DIF"
#' @param acces_token an access_token generated by get_shl_access_token()
#' @export
#' @examples
#' \dontrun{
#' get_teams()
#' }

get_team_standing <- function(season = season, team_id = NULL, access_token = get_shl_access_token()) {

  # Search SHL api for team standing
  res <- GET(url =
             stringr::str_glue('https://openapi.shl.se/seasons/{season}/statistics/teams/standings'),
             query = list("teamIds[]" = team_id),
             add_headers(Authorization = paste("Bearer", access_token, sep = " "))) %>%
    content()

  if (!is.null(res$error)) {
    stop(str_glue('{res$error$message} ({res$error$status})'))
  }

  num_loops <- seq_len(length(res))

teams <-  purrr::map_df(num_loops, function(this_row){
    this_team <- res[[this_row]]
    list(
      gp = this_team$gp,
      rank = this_team$rank,
      team_code = this_team$team$code,
      diff = this_team$diff,
      g = this_team$g,
      ga = this_team$ga,
      non_reg_l = this_team$non_reg_l,
      non_reg_non_w = this_team$non_reg_non_w,
      non_reg_t = this_team$non_reg_t,
      non_reg_w = this_team$non_reg_w,
      otl = this_team$otl,
      ott = this_team$ott,
      otw = this_team$otw,
      points = this_team$points,
      reg_l = this_team$reg_l,
      reg_t = this_team$reg_t,
      reg_w = this_team$reg_w,
      sol = this_team$sol,
      sow = this_team$sow
    )
  })

  return(teams)
}
filipwastberg/shlr documentation built on May 17, 2019, 1:14 a.m.