R/get_teams.R

#' Returns a data frame with the teams currently active in SHL
#'
#' @param acces_token an access_token generated by get_shl_access_token()
#' @export
#' @examples
#' \dontrun{
#' get_teams()
#' }

get_teams <- function(access_token = get_shl_access_token()) {

  # Search Spotify API for artist name
  res <- GET(url = 'https://openapi.shl.se/teams',
             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(
      team_code = this_team$team_code,
      chairman = this_team$chairman,
      president = this_team$president,
      finals = this_team$finals,
      founded = this_team$founded,
      contact_adress = this_team$contact$address
    )
  })

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