Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions R/getAccessToken.R
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down
144 changes: 115 additions & 29 deletions R/getEvents.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down Expand Up @@ -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)) {
Expand All @@ -51,20 +53,34 @@ getEvents <- function (
matchInfo <-
purrr::map_df(
matches,
~ jsonlite::fromJSON(
httr::content(
.callAPIlimited(
base_url = "https://api.impect.com/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 %>%
Expand Down Expand Up @@ -98,18 +114,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) <-
Expand All @@ -123,7 +140,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
Expand All @@ -138,13 +156,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)
}
Expand All @@ -157,7 +176,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
Expand All @@ -167,7 +187,7 @@ getEvents <- function (
)
)$data %>%
dplyr::mutate(matchId = ..1) %>%
jsonlite::flatten()
jsonlite::flatten()
) %>%
tidyr::unnest_longer(.data$setPieceSubPhase) %>%
tidyr::unnest(.data$setPieceSubPhase, names_sep = ".") %>%
Expand Down Expand Up @@ -201,7 +221,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
Expand All @@ -221,7 +242,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
Expand All @@ -239,12 +261,44 @@ 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(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(
Expand Down Expand Up @@ -284,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(
Expand Down Expand Up @@ -316,7 +398,7 @@ getEvents <- function (
dplyr::left_join(
dplyr::select(
players, .data$id, setPieceSubPhaseMainEventPlayerName = .data$commonname
),
),
by = base::c("setPieceSubPhaseMainEventPlayerId" = "id")
) %>%
dplyr::left_join(
Expand Down Expand Up @@ -406,11 +488,15 @@ getEvents <- function (
"homeSquadCountryId",
"homeSquadCountryName",
"homeSquadType",
"homeCoachId",
"homeCoachName",
"awaySquadId",
"awaySquadName",
"awaySquadCountryId",
"awaySquadCountryName",
"awaySquadType",
"awayCoachId",
"awayCoachName",
"eventId",
"eventNumber",
"sequenceIndex",
Expand Down
18 changes: 13 additions & 5 deletions R/getFormations.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#'
#' @param matches 'IMPECT' match ID or a list of match IDs
#' @param token bearer token
#' @param host host environment
#'
#' @export
#'
Expand All @@ -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
Expand All @@ -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
),
Expand Down Expand Up @@ -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
Expand All @@ -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 %>%
Expand Down
6 changes: 4 additions & 2 deletions R/getIterations.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Return a dataframe containing all iterations available to the user
#'
#' @param token bearer token
#' @param host host environment
#'
#' @export

Expand All @@ -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",
Expand Down
Loading