From 689a1e05abcd525f067e97f36246ed32422181aa Mon Sep 17 00:00:00 2001 From: Florian Schmitt Date: Wed, 8 Oct 2025 12:22:31 +0200 Subject: [PATCH 1/2] introduce host parameter to query different envs --- R/getAccessToken.R | 10 +++---- R/getEvents.R | 51 ++++++++++++++++++++++------------ R/getFormations.R | 18 ++++++++---- R/getIterations.R | 6 ++-- R/getMatches.R | 12 +++++--- R/getPlayerIterationAverages.R | 21 ++++++++++---- R/getPlayerIterationScores.R | 22 +++++++++++---- R/getPlayerMatchScores.R | 31 +++++++++++++++------ R/getPlayerMatchsums.R | 26 +++++++++++------ R/getPlayerProfileScores.R | 22 +++++++++++---- R/getSetPieces.R | 20 ++++++++----- R/getSquadIterationAverages.R | 18 ++++++++---- R/getSquadIterationScores.R | 18 ++++++++---- R/getSquadMatchScores.R | 27 +++++++++++++----- R/getSquadMatchsums.R | 27 +++++++++++++----- R/getSquadRatings.R | 15 +++++++--- R/getStartingPositions.R | 21 ++++++++++---- R/getSubstitutions.R | 21 ++++++++++---- R/utils.R | 14 ++++++---- 19 files changed, 279 insertions(+), 121 deletions(-) diff --git a/R/getAccessToken.R b/R/getAccessToken.R index 44a953c..a34a731 100644 --- a/R/getAccessToken.R +++ b/R/getAccessToken.R @@ -14,16 +14,16 @@ #' \dontrun{ #' token <- getAccessToken(username = "yourUsername", password = "yourPassword") #' } -getAccessToken <- function(username, password) { +getAccessToken <- function( + username, + password, + token_url = "https://login.impect.com/auth/realms/production/protocol/openid-connect/token" + ) { # validate input parameters if (missing(username) || missing(password) || username == "" || password == "") { stop("Username and password are required.") } - # create tokenURL - token_url <- - "https://login.impect.com/auth/realms/production/protocol/openid-connect/token" - # compose login link login <- base::paste0( "client_id=api&grant_type=password&username=", diff --git a/R/getEvents.R b/R/getEvents.R index ec72291..ff2702a 100644 --- a/R/getEvents.R +++ b/R/getEvents.R @@ -5,6 +5,7 @@ #' @param token bearer token #' @param include_kpis include KPIs in event data #' @param include_set_pieces include additional set piece data in event data +#' @param host host environment #' #' @export #' @@ -35,8 +36,9 @@ getEvents <- function ( matches, token, include_kpis = TRUE, - include_set_pieces = FALSE - ) { + include_set_pieces = FALSE, + host = "https://api.impect.com" +) { # check if match input is not a list and convert to list if required if (!base::is.list(matches)) { @@ -55,9 +57,12 @@ getEvents <- function ( httr::content( .callAPIlimited( base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), + ), "text", encoding = "UTF-8" ) @@ -98,18 +103,19 @@ getEvents <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/events", token = token ), "text", encoding = "UTF-8" - ) - )$data %>% + ) + )$data %>% dplyr::mutate(matchId = ..1) %>% jsonlite::flatten() - ) + ) # fix column names using regex base::names(events) <- @@ -123,7 +129,8 @@ getEvents <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/event-kpis", token = token @@ -138,13 +145,14 @@ getEvents <- function ( kpis <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/kpis/event", + host, + base_url = "/v5/customerapi/kpis/event", token = token - ), + ), "text", encoding = "UTF-8" - ) - )$data %>% + ) + )$data %>% jsonlite::flatten() %>% dplyr::select(.data$id, .data$name) } @@ -157,7 +165,8 @@ getEvents <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/set-pieces", token = token @@ -167,7 +176,7 @@ getEvents <- function ( ) )$data %>% dplyr::mutate(matchId = ..1) %>% - jsonlite::flatten() + jsonlite::flatten() ) %>% tidyr::unnest_longer(.data$setPieceSubPhase) %>% tidyr::unnest(.data$setPieceSubPhase, names_sep = ".") %>% @@ -201,7 +210,8 @@ getEvents <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/players", token = token @@ -221,7 +231,8 @@ getEvents <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -241,10 +252,14 @@ getEvents <- function ( # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host) + ) # get iterations - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # account for matches without dribbles, duels, or opponents tagged attributes <- c( @@ -316,7 +331,7 @@ getEvents <- function ( dplyr::left_join( dplyr::select( players, .data$id, setPieceSubPhaseMainEventPlayerName = .data$commonname - ), + ), by = base::c("setPieceSubPhaseMainEventPlayerId" = "id") ) %>% dplyr::left_join( diff --git a/R/getFormations.R b/R/getFormations.R index fce188c..dba1677 100644 --- a/R/getFormations.R +++ b/R/getFormations.R @@ -3,6 +3,7 @@ #' #' @param matches 'IMPECT' match ID or a list of match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -27,7 +28,8 @@ #' } getFormations <- function ( matches, - token + token, + host = "https://api.impect.com" ) { # check if match input is not a list and convert to list if required @@ -46,7 +48,8 @@ getFormations <- function ( response <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -113,7 +116,8 @@ getFormations <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -133,10 +137,14 @@ getFormations <- function ( # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host) + ) # get iterations - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # extract formations formations_home <- match_info %>% diff --git a/R/getIterations.R b/R/getIterations.R index 5ccd3fc..b433726 100644 --- a/R/getIterations.R +++ b/R/getIterations.R @@ -1,6 +1,7 @@ #' Return a dataframe containing all iterations available to the user #' #' @param token bearer token +#' @param host host environment #' #' @export @@ -19,12 +20,13 @@ #' token = "yourToken" #' ) #' } -getIterations <- function(token) { +getIterations <- function(token, host = "https://api.impect.com") { # get iteration data from API iterations <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations", + host, + base_url = "/v5/customerapi/iterations", token = token ), "text", diff --git a/R/getMatches.R b/R/getMatches.R index e7cda8c..1be5f30 100644 --- a/R/getMatches.R +++ b/R/getMatches.R @@ -2,6 +2,7 @@ #' #' @param iteration 'IMPECT' iteration ID #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -23,13 +24,14 @@ #' token = "yourToken" #' ) #' } -getMatches <- function(iteration, token) { +getMatches <- function(iteration, token, host = "https://api.impect.com") { # get matches from API matches <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/matches", token = token @@ -47,7 +49,8 @@ getMatches <- function(iteration, token) { squads <-jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -90,7 +93,8 @@ getMatches <- function(iteration, token) { countries <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/countries/", + host, + base_url = "/v5/customerapi/countries/", token = token ), "text", diff --git a/R/getPlayerIterationAverages.R b/R/getPlayerIterationAverages.R index d7d8c78..bdb2241 100644 --- a/R/getPlayerIterationAverages.R +++ b/R/getPlayerIterationAverages.R @@ -2,6 +2,7 @@ #' #' @param iteration 'IMPECT' iteration ID #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -24,7 +25,11 @@ #' token = "yourToken" #' ) #' } -getPlayerIterationAverages <- function (iteration, token) { +getPlayerIterationAverages <- function ( + iteration, + token, + host = "https://api.impect.com" +) { # check if iteration input is a string or integer if (!(base::is.numeric(iteration) || @@ -36,7 +41,8 @@ getPlayerIterationAverages <- function (iteration, token) { squads <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -60,8 +66,9 @@ getPlayerIterationAverages <- function (iteration, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( + host, base_url = paste0( - "https://api.impect.com/v5/customerapi/iterations/", + "/v5/customerapi/iterations/", iteration, "/squads/", ., @@ -80,7 +87,8 @@ getPlayerIterationAverages <- function (iteration, token) { players <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/players", token = token @@ -98,7 +106,8 @@ getPlayerIterationAverages <- function (iteration, token) { kpis <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/kpis", + host, + base_url = "/v5/customerapi/kpis", token = token ), "text", @@ -109,7 +118,7 @@ getPlayerIterationAverages <- function (iteration, token) { dplyr::select(.data$id, .data$name) # get iterations from API - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate averages diff --git a/R/getPlayerIterationScores.R b/R/getPlayerIterationScores.R index 3ff8d37..1b62a91 100644 --- a/R/getPlayerIterationScores.R +++ b/R/getPlayerIterationScores.R @@ -21,6 +21,7 @@ allowed_positions <- c( #' "DEFENSE_MIDFIELD", "CENTRAL_MIDFIELD", "ATTACKING_MIDFIELD", "LEFT_WINGER", #' "RIGHT_WINGER", "CENTER_FORWARD" #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -45,7 +46,12 @@ allowed_positions <- c( #' token = "yourToken" #' ) #' } -getPlayerIterationScores <- function (iteration, positions, token) { +getPlayerIterationScores <- function ( + iteration, + positions, + token, + host = "https://api.impect.com" +) { # check if iteration input is a string or integer if (!(base::is.numeric(iteration) || @@ -67,7 +73,8 @@ getPlayerIterationScores <- function (iteration, positions, token) { squads <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -92,8 +99,9 @@ getPlayerIterationScores <- function (iteration, positions, token) { response <- jsonlite::fromJSON( httr::content( .callAPIlimited( + host, base_url = paste0( - "https://api.impect.com/v5/customerapi/iterations/", + "/v5/customerapi/iterations/", iteration, "/squads/", ., @@ -145,7 +153,8 @@ getPlayerIterationScores <- function (iteration, positions, token) { players <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/players", token = token @@ -163,7 +172,8 @@ getPlayerIterationScores <- function (iteration, positions, token) { score_list <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/player-scores", + host, + base_url = "/v5/customerapi/player-scores", token = token ), "text", @@ -174,7 +184,7 @@ getPlayerIterationScores <- function (iteration, positions, token) { dplyr::select(.data$id, .data$name) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate averages diff --git a/R/getPlayerMatchScores.R b/R/getPlayerMatchScores.R index 4f85f76..ae45008 100644 --- a/R/getPlayerMatchScores.R +++ b/R/getPlayerMatchScores.R @@ -22,6 +22,7 @@ allowed_positions <- c( #' "DEFENSE_MIDFIELD", "CENTRAL_MIDFIELD", "ATTACKING_MIDFIELD", "LEFT_WINGER", #' "RIGHT_WINGER", "CENTER_FORWARD" #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -46,7 +47,12 @@ allowed_positions <- c( #' token = "yourToken" #' ) #' } -getPlayerMatchScores <- function (matches, positions, token) { +getPlayerMatchScores <- function ( + matches, + positions, + token, + host = "https://api.impect.com" +) { # check if match input is a list and convert to list if required if (!base::is.list(matches)) { @@ -71,7 +77,8 @@ getPlayerMatchScores <- function (matches, positions, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -118,8 +125,9 @@ getPlayerMatchScores <- function (matches, positions, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( + host, base_url = base::paste0( - "https://api.impect.com/v5/customerapi/matches/", + "/v5/customerapi/matches/", ., "/positions/", position_string, @@ -145,7 +153,8 @@ getPlayerMatchScores <- function (matches, positions, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/players", token = token @@ -172,7 +181,8 @@ getPlayerMatchScores <- function (matches, positions, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -190,7 +200,8 @@ getPlayerMatchScores <- function (matches, positions, token) { score_list <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/player-scores", + host, + base_url = "/v5/customerapi/player-scores", token = token ), "text", @@ -202,10 +213,14 @@ getPlayerMatchScores <- function (matches, positions, token) { # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host + )) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate scores diff --git a/R/getPlayerMatchsums.R b/R/getPlayerMatchsums.R index 19bc41b..0cafafb 100644 --- a/R/getPlayerMatchsums.R +++ b/R/getPlayerMatchsums.R @@ -2,6 +2,7 @@ #' #' @param matches 'IMPECT' match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -23,7 +24,11 @@ #' token = "yourToken" #' ) #' } -getPlayerMatchsums <- function (matches, token) { +getPlayerMatchsums <- function ( + matches, + token, + host = "https://api.impect.com" +) { # check if match input is a list and convert to list if required if (!base::is.list(matches)) { @@ -41,7 +46,8 @@ getPlayerMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -86,7 +92,8 @@ getPlayerMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/player-kpis", token = token @@ -109,7 +116,8 @@ getPlayerMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/players", token = token @@ -137,7 +145,8 @@ getPlayerMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -156,7 +165,8 @@ getPlayerMatchsums <- function (matches, token) { kpis <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/kpis", + host, + base_url = "/v5/customerapi/kpis", token = token ), "text", @@ -169,10 +179,10 @@ getPlayerMatchsums <- function (matches, token) { # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches(iteration = ., token = token, host = host)) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate matchsums diff --git a/R/getPlayerProfileScores.R b/R/getPlayerProfileScores.R index 2133bc5..8eaa1f8 100644 --- a/R/getPlayerProfileScores.R +++ b/R/getPlayerProfileScores.R @@ -22,6 +22,7 @@ allowed_positions <- c( #' "DEFENSE_MIDFIELD", "CENTRAL_MIDFIELD", "ATTACKING_MIDFIELD", "LEFT_WINGER", #' "RIGHT_WINGER", "CENTER_FORWARD" #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -46,7 +47,12 @@ allowed_positions <- c( #' token = "yourToken" #' ) #' } -getPlayerProfileScores <- function (iteration, positions, token) { +getPlayerProfileScores <- function ( + iteration, + positions, + token, + host = "https://api.impect.com" +) { # check if iteration input is a string or integer if (!(base::is.numeric(iteration) || @@ -68,7 +74,8 @@ getPlayerProfileScores <- function (iteration, positions, token) { squads <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -93,8 +100,9 @@ getPlayerProfileScores <- function (iteration, positions, token) { response <- jsonlite::fromJSON( httr::content( .callAPIlimited( + host, base_url = paste0( - "https://api.impect.com/v5/customerapi/iterations/", + "/v5/customerapi/iterations/", iteration, "/squads/", ., @@ -146,7 +154,8 @@ getPlayerProfileScores <- function (iteration, positions, token) { players <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/players", token = token @@ -164,7 +173,8 @@ getPlayerProfileScores <- function (iteration, positions, token) { profile_list <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/player-profiles", + host, + base_url = "/v5/customerapi/player-profiles", token = token ), "text", @@ -175,7 +185,7 @@ getPlayerProfileScores <- function (iteration, positions, token) { dplyr::select(.data$name) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate averages diff --git a/R/getSetPieces.R b/R/getSetPieces.R index 30cc930..94801f5 100644 --- a/R/getSetPieces.R +++ b/R/getSetPieces.R @@ -3,6 +3,7 @@ #' #' @param matches list fo 'IMPECT' match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -27,7 +28,8 @@ #' } getSetPieces <- function ( matches, - token + token, + host = "https://api.impect.com" ) { # check if match input is not a list and convert to list if required @@ -46,7 +48,8 @@ getSetPieces <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -91,7 +94,8 @@ getSetPieces <- function ( jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/set-pieces", token = token @@ -124,7 +128,8 @@ getSetPieces <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/players", token = token @@ -144,7 +149,8 @@ getSetPieces <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -161,10 +167,10 @@ getSetPieces <- function ( # get matchplan data matchplan <- purrr::map_df(iterations, - ~ getMatches(iteration = ., token = token)) + ~ getMatches(iteration = ., token = token, host = host)) # get iterations - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # start merging dfs diff --git a/R/getSquadIterationAverages.R b/R/getSquadIterationAverages.R index f529cfc..fb370ec 100644 --- a/R/getSquadIterationAverages.R +++ b/R/getSquadIterationAverages.R @@ -2,6 +2,7 @@ #' #' @param iteration 'IMPECT' iteration ID #' @param token bearer token +#' @param host host environment #' @export @@ -24,7 +25,11 @@ #' token = "yourToken" #' ) #' } -getSquadIterationAverages <- function (iteration, token) { +getSquadIterationAverages <- function ( + iteration, + token, + host = "https://api.impect.com" +) { # check if iteration input is a string or integer if (!(base::is.numeric(iteration) || @@ -36,7 +41,8 @@ getSquadIterationAverages <- function (iteration, token) { squads <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -58,7 +64,8 @@ getSquadIterationAverages <- function (iteration, token) { averages_raw <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squad-kpis", token = token @@ -73,7 +80,8 @@ getSquadIterationAverages <- function (iteration, token) { kpis <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/kpis", + host, + base_url = "/v5/customerapi/kpis", token = token ), "text", @@ -84,7 +92,7 @@ getSquadIterationAverages <- function (iteration, token) { dplyr::select(.data$id, .data$name) # get competitions from API - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate averages diff --git a/R/getSquadIterationScores.R b/R/getSquadIterationScores.R index e606aff..ea0ea9f 100644 --- a/R/getSquadIterationScores.R +++ b/R/getSquadIterationScores.R @@ -2,6 +2,7 @@ #' #' @param iteration 'IMPCET' iteration ID #' @param token bearer token +#' @param host host environment #' @export @@ -24,7 +25,11 @@ #' token = "yourToken" #' ) #' } -getSquadIterationScores <- function (iteration, token) { +getSquadIterationScores <- function ( + iteration, + token, + host = "https://api.impect.com" +) { # check if iteration input is a string or integer if (!(base::is.numeric(iteration) || @@ -36,7 +41,8 @@ getSquadIterationScores <- function (iteration, token) { squads <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -60,7 +66,8 @@ getSquadIterationScores <- function (iteration, token) { scores_raw <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squad-scores", token = token @@ -75,7 +82,8 @@ getSquadIterationScores <- function (iteration, token) { score_list <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/squad-scores", + host, + base_url = "/v5/customerapi/squad-scores", token = token ), "text", @@ -86,7 +94,7 @@ getSquadIterationScores <- function (iteration, token) { dplyr::select(.data$id, .data$name) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate averages diff --git a/R/getSquadMatchScores.R b/R/getSquadMatchScores.R index dd092be..a90e5c3 100644 --- a/R/getSquadMatchScores.R +++ b/R/getSquadMatchScores.R @@ -2,6 +2,7 @@ #' #' @param matches 'IMPECT' match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -23,7 +24,11 @@ #' token = "yourToken" #' ) #' } -getSquadMatchScores <- function (matches, token) { +getSquadMatchScores <- function ( + matches, + token, + host = "https://api.impect.com" +) { # check if match input is a list and convert to list if required if (!base::is.list(matches)) { @@ -41,7 +46,8 @@ getSquadMatchScores <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -85,7 +91,8 @@ getSquadMatchScores <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/squad-scores", token = token @@ -108,7 +115,8 @@ getSquadMatchScores <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -129,7 +137,8 @@ getSquadMatchScores <- function (matches, token) { scores_list <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/squad-scores", + host, + base_url = "/v5/customerapi/squad-scores", token = token ), "text", @@ -141,10 +150,14 @@ getSquadMatchScores <- function (matches, token) { # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host + )) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate matchsums diff --git a/R/getSquadMatchsums.R b/R/getSquadMatchsums.R index ebfff35..eebfb8c 100644 --- a/R/getSquadMatchsums.R +++ b/R/getSquadMatchsums.R @@ -2,6 +2,7 @@ #' #' @param matches 'IMPECT' match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -23,7 +24,11 @@ #' token = "yourToken" #' ) #' } -getSquadMatchsums <- function (matches, token) { +getSquadMatchsums <- function ( + matches, + token, + host = "https://api.impect.com" +) { # check if match input is a list and convert to list if required if (!base::is.list(matches)) { @@ -41,7 +46,8 @@ getSquadMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -85,7 +91,8 @@ getSquadMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., suffix = "/squad-kpis", token = token @@ -108,7 +115,8 @@ getSquadMatchsums <- function (matches, token) { ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -129,7 +137,8 @@ getSquadMatchsums <- function (matches, token) { kpis <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/kpis", + host, + base_url = "/v5/customerapi/kpis", token = token ), "text", @@ -141,10 +150,14 @@ getSquadMatchsums <- function (matches, token) { # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host + )) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # manipulate matchsums diff --git a/R/getSquadRatings.R b/R/getSquadRatings.R index 2fa4fda..51a33a3 100644 --- a/R/getSquadRatings.R +++ b/R/getSquadRatings.R @@ -2,6 +2,7 @@ #' #' @param iteration 'IMPECT' iteration ID #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -23,7 +24,11 @@ #' token = "yourToken" #' ) #' } -getSquadRatings <- function (iteration, token) { +getSquadRatings <- function ( + iteration, + token, + host = "https://api.impect.com" +) { # check if iteration input is a int if (!base::is.numeric(iteration)) { @@ -33,7 +38,8 @@ getSquadRatings <- function (iteration, token) { squads <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads", token = token @@ -54,7 +60,8 @@ getSquadRatings <- function (iteration, token) { jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = iteration, suffix = "/squads/ratings", token = token @@ -71,7 +78,7 @@ getSquadRatings <- function (iteration, token) { gsub("\\.(.)", "\\U\\1", base::names(ratings), perl = TRUE) # get competitions - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # merge with other data ratings <- ratings %>% diff --git a/R/getStartingPositions.R b/R/getStartingPositions.R index aa4606c..7363a02 100644 --- a/R/getStartingPositions.R +++ b/R/getStartingPositions.R @@ -3,6 +3,7 @@ #' #' @param matches 'IMPECT' match ID or a list of match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -27,7 +28,8 @@ #' } getStartingPositions <- function ( matches, - token + token, + host = "https://api.impect.com" ) { # check if match input is not a list and convert to list if required @@ -46,7 +48,8 @@ getStartingPositions <- function ( response <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -116,7 +119,8 @@ getStartingPositions <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/players", token = token @@ -138,7 +142,8 @@ getStartingPositions <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -158,10 +163,14 @@ getStartingPositions <- function ( # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host + )) # get iterations - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # extract shirt numbers shirt_numbers_home <- match_info %>% diff --git a/R/getSubstitutions.R b/R/getSubstitutions.R index 7d75a48..e05e51f 100644 --- a/R/getSubstitutions.R +++ b/R/getSubstitutions.R @@ -3,6 +3,7 @@ #' #' @param matches 'IMPECT' match ID or a list of match IDs #' @param token bearer token +#' @param host host environment #' #' @export #' @@ -27,7 +28,8 @@ #' } getSubstitutions <- function ( matches, - token + token, + host = "https://api.impect.com" ) { # check if match input is not a list and convert to list if required @@ -46,7 +48,8 @@ getSubstitutions <- function ( response <- jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", + host, + base_url = "/v5/customerapi/matches/", id = ., token = token ), @@ -116,7 +119,8 @@ getSubstitutions <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/players", token = token @@ -138,7 +142,8 @@ getSubstitutions <- function ( ~ jsonlite::fromJSON( httr::content( .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/iterations/", + host, + base_url = "/v5/customerapi/iterations/", id = ., suffix = "/squads", token = token @@ -158,10 +163,14 @@ getSubstitutions <- function ( # get matchplan data matchplan <- - purrr::map_df(iterations, ~ getMatches(iteration = ., token = token)) + purrr::map_df(iterations, ~ getMatches( + iteration = ., + token = token, + host = host + )) # get iterations - iterations <- getIterations(token = token) + iterations <- getIterations(token = token, host = host) # extract shirt numbers shirt_numbers_home <- match_info %>% diff --git a/R/utils.R b/R/utils.R index 2b0115c..bdb853c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -4,13 +4,14 @@ #' #' @noRd #' +#' @param host host environment #' @param base_url API endpoint URL #' @param id The id of the object to be retrieved #' @param suffix The suffix of the endpoint URL that comes after the id #' @param token Bearer token #' #' @return Response content of the API endpoint -.callAPI <- function(base_url, id = "", suffix = "", token, +.callAPI <- function(host, base_url, id = "", suffix = "", token, max_retries = 3, retry_delay = 1) { # try API call @@ -18,7 +19,7 @@ # get API response response <- httr::GET( - url = base::paste0(base_url, id, suffix), + url = base::paste0(host, base_url, id, suffix), httr::add_headers( Authorization = base::paste("Bearer", token, sep = " ")) ) @@ -59,19 +60,20 @@ #' #' @noRd #' +#' @param host host environment #' @param base_url Impect API endpoint URL #' @param id the id of the object to be retrieved #' @param suffix suffix of the endpoint URL that comes after the id #' @param token bearer token #' #' @return a dataframe containing the response of an API endpoint -.callAPIlimited <- function(base_url, id = "", suffix = "", token) { +.callAPIlimited <- function(host, base_url, id = "", suffix = "", token) { # check if Token bucket exist and create it if not if (is.null(.api_state$bucket)) { # get response from API - response <- .callAPI(base_url, id, suffix, token) + response <- .callAPI(host, base_url, id, suffix, token) # get rate limit policy policy <- response[["all_headers"]][[1]][["headers"]][["ratelimit-policy"]] @@ -100,7 +102,7 @@ # check if a token is available if (.api_state$bucket$isTokenAvailable()) { # get API response - response <- .callAPI(base_url, id, suffix, token) + response <- .callAPI(host, base_url, id, suffix, token) # consume a token .api_state$bucket$consumeToken() @@ -109,7 +111,7 @@ Sys.sleep(.api_state$bucket$intervall) # call function again - response <- .callAPIlimited(base_url, id, suffix, token) + response <- .callAPIlimited(host, base_url, id, suffix, token) } # return response From b64ab6589c7ef00239e7a572f8df49a766a2e267 Mon Sep 17 00:00:00 2001 From: Florian Schmitt Date: Wed, 8 Oct 2025 13:57:16 +0200 Subject: [PATCH 2/2] add coaches info to matchsums (players and squads), match scores (player and squads) and events --- R/getEvents.R | 99 +++++++++++++++++++++++++++++++------ R/getPlayerMatchScores.R | 103 +++++++++++++++++++++++++++++++++------ R/getPlayerMatchsums.R | 97 ++++++++++++++++++++++++++++++------ R/getSquadMatchScores.R | 98 +++++++++++++++++++++++++++++++------ R/getSquadMatchsums.R | 96 ++++++++++++++++++++++++++++++------ 5 files changed, 419 insertions(+), 74 deletions(-) diff --git a/R/getEvents.R b/R/getEvents.R index ff2702a..2b23e91 100644 --- a/R/getEvents.R +++ b/R/getEvents.R @@ -53,23 +53,34 @@ getEvents <- function ( matchInfo <- purrr::map_df( matches, - ~ jsonlite::fromJSON( - httr::content( - .callAPIlimited( - base_url = "https://api.impect.com/v5/customerapi/matches/", - host, - base_url = "/v5/customerapi/matches/", - id = ., - token = token + ~ { + temp <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/matches/", + id = ., + token = token ), - ), - "text", - encoding = "UTF-8" + "text", + encoding = "UTF-8" ) )$data - ) %>% - dplyr::select(.data$id, .data$iterationId, .data$lastCalculationDate) %>% - base::unique() + + response <- dplyr::tibble( + id = temp$id, + dateTime = temp$dateTime, + iterationId = temp$iterationId, + lastCalculationDate = temp$lastCalculationDate, + squadHomeId = temp$squadHome$id, + squadAwayId = temp$squadAway$id, + homeCoachId = temp$squadHome$coachId, + awayCoachId = temp$squadAway$coachId, + formationHome = temp$squadHome$startingFormation, + formationAway = temp$squadAway$startingFormation + ) + } + ) # filter for fail matches fail_matches <- matchInfo %>% @@ -250,6 +261,34 @@ getEvents <- function ( base::names(squads) <- gsub("\\.(.)", "\\U\\1", base::names(squads), perl = TRUE) + # get coach master data from API + coaches <- + purrr::map_df( + iterations, + ~ { + response <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/iterations/", + id = ., + suffix = "/coaches", + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + if (base::length(response) > 0) { + response <- response %>% + jsonlite::flatten() + } + } + ) %>% + dplyr::select(.data$id, .data$name) %>% + base::unique() + # get matchplan data matchplan <- purrr::map_df(iterations, ~ getMatches( @@ -299,6 +338,34 @@ getEvents <- function ( by = base::c("currentAttackingSquadId" = "squadId") ) + # merge events with matchInfo & coaches + events <- events %>% + dplyr::left_join( + dplyr::select( + matchInfo, + matchId = .data$id, + .data$homeCoachId, + .data$awayCoachId + ), + by = base::c("matchId" = "matchId") + ) %>% + dplyr::left_join( + dplyr::select( + coaches, + homeCoachId = .data$id, + homeCoachName = .data$name + ), + by = base::c("homeCoachId" = "homeCoachId") + ) %>% + dplyr::left_join( + dplyr::select( + coaches, + awayCoachId = .data$id, + awayCoachName = .data$name + ), + by = base::c("awayCoachId" = "awayCoachId") + ) + # merge events with players events <- events %>% dplyr::left_join( @@ -421,11 +488,15 @@ getEvents <- function ( "homeSquadCountryId", "homeSquadCountryName", "homeSquadType", + "homeCoachId", + "homeCoachName", "awaySquadId", "awaySquadName", "awaySquadCountryId", "awaySquadCountryName", "awaySquadType", + "awayCoachId", + "awayCoachName", "eventId", "eventNumber", "sequenceIndex", diff --git a/R/getPlayerMatchScores.R b/R/getPlayerMatchScores.R index ae45008..81e0f8a 100644 --- a/R/getPlayerMatchScores.R +++ b/R/getPlayerMatchScores.R @@ -70,25 +70,38 @@ getPlayerMatchScores <- function ( ".\nChoose one or more of: ", paste(allowed_positions, collapse = ", ")) } - # get match info from API + # get matchInfo from API matchInfo <- purrr::map_df( matches, - ~ jsonlite::fromJSON( - httr::content( - .callAPIlimited( - host, - base_url = "/v5/customerapi/matches/", - id = ., - token = token - ), - "text", - encoding = "UTF-8" + ~ { + temp <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/matches/", + id = ., + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + response <- dplyr::tibble( + id = temp$id, + dateTime = temp$dateTime, + iterationId = temp$iterationId, + lastCalculationDate = temp$lastCalculationDate, + squadHomeId = temp$squadHome$id, + squadAwayId = temp$squadAway$id, + homeCoachId = temp$squadHome$coachId, + awayCoachId = temp$squadAway$coachId, + formationHome = temp$squadHome$startingFormation, + formationAway = temp$squadAway$startingFormation ) - )$data - ) %>% - dplyr::select(.data$id, .data$iterationId, .data$lastCalculationDate) %>% - base::unique() + } + ) # filter for fail matches fail_matches <- matchInfo %>% @@ -196,6 +209,36 @@ getPlayerMatchScores <- function ( dplyr::select(.data$id, .data$name) %>% base::unique() + # get coach master data from API + + # get coach master data from API + coaches <- + purrr::map_df( + iterations, + ~ { + response <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/iterations/", + id = ., + suffix = "/coaches", + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + if (base::length(response) > 0) { + response <- response %>% + jsonlite::flatten() + } + } + ) %>% + dplyr::select(.data$id, .data$name) %>% + base::unique() + # get kpi names from API score_list <- jsonlite::fromJSON( httr::content( @@ -342,7 +385,33 @@ getPlayerMatchScores <- function ( .data$skillCornerId, .data$playerName, .data$firstname, .data$lastname, .data$birthdate, .data$birthplace, .data$leg ), - by = c("playerId" = "id")) %>% + by = c("playerId" = "id") + ) %>% + dplyr::left_join( + bind_rows( + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadHomeId, + coachId = .data$homeCoachId + ), + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadAwayId, + coachId = .data$awayCoachId + ) + ), + by = base::c("matchId" = "matchId", "squadId" = "squadId") + ) %>% + dplyr::left_join( + dplyr::select( + coaches, + coachId = .data$id, + coachName = .data$name + ), + by = base::c("coachId" = "coachId") + ) %>% # fix some column names dplyr::rename( dateTime = .data$scheduledDate @@ -361,6 +430,8 @@ getPlayerMatchScores <- function ( "matchDayName", "squadId", "squadName", + "coachId", + "coachName", "playerId", "wyscoutId", "heimSpielId", diff --git a/R/getPlayerMatchsums.R b/R/getPlayerMatchsums.R index 0cafafb..24376e5 100644 --- a/R/getPlayerMatchsums.R +++ b/R/getPlayerMatchsums.R @@ -43,22 +43,34 @@ getPlayerMatchsums <- function ( matchInfo <- purrr::map_df( matches, - ~ jsonlite::fromJSON( - httr::content( - .callAPIlimited( - host, - base_url = "/v5/customerapi/matches/", - id = ., - token = token - ), - "text", - encoding = "UTF-8" - ) - )$data - ) %>% - dplyr::select(.data$id, .data$iterationId, .data$lastCalculationDate) %>% - base::unique() + ~ { + temp <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/matches/", + id = ., + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + response <- dplyr::tibble( + id = temp$id, + dateTime = temp$dateTime, + iterationId = temp$iterationId, + lastCalculationDate = temp$lastCalculationDate, + squadHomeId = temp$squadHome$id, + squadAwayId = temp$squadAway$id, + homeCoachId = temp$squadHome$coachId, + awayCoachId = temp$squadAway$coachId, + formationHome = temp$squadHome$startingFormation, + formationAway = temp$squadAway$startingFormation + ) + } + ) # filter for fail matches fail_matches <- matchInfo %>% @@ -160,6 +172,34 @@ getPlayerMatchsums <- function ( dplyr::select(.data$id, .data$name) %>% base::unique() + # get coach master data from API + coaches <- + purrr::map_df( + iterations, + ~ { + response <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/iterations/", + id = ., + suffix = "/coaches", + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + if (base::length(response) > 0) { + response <- response %>% + jsonlite::flatten() + } + } + ) %>% + dplyr::select(.data$id, .data$name) %>% + base::unique() + # get kpi names from API kpis <- jsonlite::fromJSON( @@ -268,6 +308,31 @@ getPlayerMatchsums <- function ( .data$skillCornerId), by = c("playerId" = "id") ) %>% + dplyr::left_join( + bind_rows( + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadHomeId, + coachId = .data$homeCoachId + ), + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadAwayId, + coachId = .data$awayCoachId + ) + ), + by = base::c("matchId" = "matchId", "squadId" = "squadId") + ) %>% + dplyr::left_join( + dplyr::select( + coaches, + coachId = .data$id, + coachName = .data$name + ), + by = base::c("coachId" = "coachId") + ) %>% # fix some column names dplyr::rename( dateTime = .data$scheduledDate @@ -286,6 +351,8 @@ getPlayerMatchsums <- function ( "matchDayName", "squadId", "squadName", + "coachId", + "coachName", "playerId", "wyscoutId", "heimSpielId", diff --git a/R/getSquadMatchScores.R b/R/getSquadMatchScores.R index a90e5c3..7b5c55d 100644 --- a/R/getSquadMatchScores.R +++ b/R/getSquadMatchScores.R @@ -39,25 +39,38 @@ getSquadMatchScores <- function ( } } - # get match info from API + # get matchInfo from API matchInfo <- purrr::map_df( matches, - ~ jsonlite::fromJSON( - httr::content( - .callAPIlimited( - host, - base_url = "/v5/customerapi/matches/", - id = ., - token = token - ), - "text", - encoding = "UTF-8" + ~ { + temp <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/matches/", + id = ., + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + response <- dplyr::tibble( + id = temp$id, + dateTime = temp$dateTime, + iterationId = temp$iterationId, + lastCalculationDate = temp$lastCalculationDate, + squadHomeId = temp$squadHome$id, + squadAwayId = temp$squadAway$id, + homeCoachId = temp$squadHome$coachId, + awayCoachId = temp$squadAway$coachId, + formationHome = temp$squadHome$startingFormation, + formationAway = temp$squadAway$startingFormation ) - )$data - ) %>% - dplyr::select(.data$id, .data$iterationId, .data$lastCalculationDate) %>% - base::unique() + } + ) # filter for fail matches fail_matches <- matchInfo %>% @@ -133,6 +146,34 @@ getSquadMatchScores <- function ( # clean data squads <- .cleanData(squads) + # get coach master data from API + coaches <- + purrr::map_df( + iterations, + ~ { + response <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/iterations/", + id = ., + suffix = "/coaches", + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + if (base::length(response) > 0) { + response <- response %>% + jsonlite::flatten() + } + } + ) %>% + dplyr::select(.data$id, .data$name) %>% + base::unique() + # get score names from API scores_list <- jsonlite::fromJSON( httr::content( @@ -233,6 +274,31 @@ getSquadMatchScores <- function ( ), by = c("squadId" = "id") ) %>% + dplyr::left_join( + bind_rows( + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadHomeId, + coachId = .data$homeCoachId + ), + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadAwayId, + coachId = .data$awayCoachId + ) + ), + by = base::c("matchId" = "matchId", "squadId" = "squadId") + ) %>% + dplyr::left_join( + dplyr::select( + coaches, + coachId = .data$id, + coachName = .data$name + ), + by = base::c("coachId" = "coachId") + ) %>% # fix some column names dplyr::rename( dateTime = .data$scheduledDate @@ -254,6 +320,8 @@ getSquadMatchScores <- function ( "heimSpielId", "skillCornerId", "squadName", + "coachId", + "coachName", scores_list$name ) diff --git a/R/getSquadMatchsums.R b/R/getSquadMatchsums.R index eebfb8c..97790e6 100644 --- a/R/getSquadMatchsums.R +++ b/R/getSquadMatchsums.R @@ -43,21 +43,34 @@ getSquadMatchsums <- function ( matchInfo <- purrr::map_df( matches, - ~ jsonlite::fromJSON( - httr::content( - .callAPIlimited( - host, - base_url = "/v5/customerapi/matches/", - id = ., - token = token - ), - "text", - encoding = "UTF-8" + ~ { + temp <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/matches/", + id = ., + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + response <- dplyr::tibble( + id = temp$id, + dateTime = temp$dateTime, + iterationId = temp$iterationId, + lastCalculationDate = temp$lastCalculationDate, + squadHomeId = temp$squadHome$id, + squadAwayId = temp$squadAway$id, + homeCoachId = temp$squadHome$coachId, + awayCoachId = temp$squadAway$coachId, + formationHome = temp$squadHome$startingFormation, + formationAway = temp$squadAway$startingFormation ) - )$data - ) %>% - dplyr::select(.data$id, .data$iterationId, .data$lastCalculationDate) %>% - base::unique() + } + ) # filter for fail matches fail_matches <- matchInfo %>% @@ -133,6 +146,34 @@ getSquadMatchsums <- function ( # clean data squads <- .cleanData(squads) + # get coach master data from API + coaches <- + purrr::map_df( + iterations, + ~ { + response <- jsonlite::fromJSON( + httr::content( + .callAPIlimited( + host, + base_url = "/v5/customerapi/iterations/", + id = ., + suffix = "/coaches", + token = token + ), + "text", + encoding = "UTF-8" + ) + )$data + + if (base::length(response) > 0) { + response <- response %>% + jsonlite::flatten() + } + } + ) %>% + dplyr::select(.data$id, .data$name) %>% + base::unique() + # get kpi names from API kpis <- jsonlite::fromJSON( httr::content( @@ -233,6 +274,31 @@ getSquadMatchsums <- function ( ), by = c("squadId" = "squadId") ) %>% + dplyr::left_join( + bind_rows( + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadHomeId, + coachId = .data$homeCoachId + ), + select( + matchInfo, + matchId = .data$id, + squadId = .data$squadAwayId, + coachId = .data$awayCoachId + ) + ), + by = base::c("matchId" = "matchId", "squadId" = "squadId") + ) %>% + dplyr::left_join( + dplyr::select( + coaches, + coachId = .data$id, + coachName = .data$name + ), + by = base::c("coachId" = "coachId") + ) %>% # fix some column names dplyr::rename( dateTime = .data$scheduledDate, @@ -254,6 +320,8 @@ getSquadMatchsums <- function ( "heimSpielId", "skillCornerId", "squadName", + "coachId", + "coachName", kpis$name )