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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: impectR
Title: Access Data from the 'Impect' API
Version: 2.5.1
Version: 2.5.2
Authors@R: c(
person("Impect", "GmbH", , "info.impect@impect.com", role = c("cph")),
person("Florian", "Schmitt", , "florian.schmitt@impect.com", role = c("aut", "cre")),
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# impectR 2.5.2

## Minor Changes
* fix bugs in the following functions that occured if either the coaches endpoint returned no coaches or the coaches endpoint being blacklisted for the user:
* `getEvents()`
* `getPlayerMatchSums()`
* `getSquadMatchSums()`
* `getPlayerMatchScores()`
* `getSquadMatchScores()`

# impectR 2.5.1

## Minor Changes
Expand Down
105 changes: 66 additions & 39 deletions R/getEvents.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,35 +262,50 @@ getEvents <- function (
gsub("\\.(.)", "\\U\\1", base::names(squads), perl = TRUE)

# get coach master data from API
coaches_blacklisted = FALSE
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"
response <- .callAPIlimited(
host,
base_url = "/v5/customerapi/iterations/",
id = .,
suffix = "/coaches",
token = token,
ignore_403 = TRUE
)
)$data

if (base::length(response) > 0) {
response <- response %>%
jsonlite::flatten()
} else {
response <- base::data.frame(
id = -1,
name = "",
stringsAsFactors = FALSE
)
# check status
status <- httr::status_code(response)

if (status == 403) {
coaches_blacklisted <<- TRUE

# insert empty df as response
response <- base::data.frame(
id = -1,
name = "",
stringsAsFactors = FALSE
)
} else {
response <- jsonlite::fromJSON(
httr::content(response, "text", encoding = "UTF-8")
)$data

# flatten response
if (base::length(response) > 0) {
response <- response %>%
jsonlite::flatten()
} else {
response <- base::data.frame(
id = -1,
name = "",
stringsAsFactors = FALSE
)
}
}
}
}
) %>%
dplyr::select(.data$id, .data$name) %>%
base::unique()
Expand Down Expand Up @@ -344,7 +359,7 @@ getEvents <- function (
by = base::c("currentAttackingSquadId" = "squadId")
)

# merge events with matchInfo & coaches
# merge events with matchInfo
events <- events %>%
dplyr::left_join(
dplyr::select(
Expand All @@ -354,24 +369,29 @@ getEvents <- function (
.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 coaches
if (coaches_blacklisted == FALSE) {
events <- events %>%
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 @@ -632,6 +652,13 @@ getEvents <- function (
order <- base::c(order, kpis$name)
}

# check if coaches are blacklisted
if (coaches_blacklisted) {
order <- order[!order %in% c(
"homeCoachId", "homeCoachName", "awayCoachId", "awayCoachName"
)]
}

# reorder data
events <- events %>%
dplyr::select(dplyr::all_of(order))
Expand Down
92 changes: 53 additions & 39 deletions R/getPlayerMatchScores.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,49 +238,53 @@ getPlayerMatchScores <- function (
base::unique()

# get coach master data from API

# get coach master data from API
coaches_blacklisted = FALSE
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
response <- .callAPIlimited(
host,
base_url = "/v5/customerapi/iterations/",
id = .,
suffix = "/coaches",
token = token,
ignore_403 = TRUE
)

if (base::length(response) > 0) {
response <- response %>%
jsonlite::flatten()
} else {
# check status
status <- httr::status_code(response)

if (status == 403) {
coaches_blacklisted <<- TRUE

# insert empty df as response
response <- base::data.frame(
id = -1,
name = "",
stringsAsFactors = FALSE
)
} else {
response <- jsonlite::fromJSON(
httr::content(response, "text", encoding = "UTF-8")
)$data

# flatten response
if (base::length(response) > 0) {
response <- response %>%
jsonlite::flatten()
} else {
response <- base::data.frame(
id = -1,
name = "",
stringsAsFactors = FALSE
)
}
}
}
)
if (base::length(coaches) > 0) {
coaches <- coaches %>%
dplyr::select(.data$id, .data$name) %>%
base::unique()
} else {
coaches <- coaches %>%
dplyr::mutate(
id = NA,
name = NA
)
}
) %>%
dplyr::select(.data$id, .data$name) %>%
base::unique()

# get kpi names from API
score_list <- jsonlite::fromJSON(
Expand Down Expand Up @@ -471,19 +475,24 @@ getPlayerMatchScores <- function (
),
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
)

# merge events with coaches
if (coaches_blacklisted == FALSE) {
scores <- scores %>%
dplyr::left_join(
dplyr::select(
coaches,
coachId = .data$id,
coachName = .data$name
),
by = base::c("coachId" = "coachId")
)
}

# define column order
order <- c(
"matchId",
Expand Down Expand Up @@ -519,6 +528,11 @@ getPlayerMatchScores <- function (
order[order == "positions"] <- "position"
}

# check if coaches are blacklisted
if (coaches_blacklisted) {
order <- order[!order %in% c("coachId", "coachName")]
}

# select columns
scores <- scores %>%
dplyr::select(dplyr::all_of(order))
Expand Down
Loading