Skip to content

Commit 0434baa

Browse files
ASA-151: Refactor GET calls
1 parent 4f6641c commit 0434baa

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

R-package/R/entities.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ get_entity <- function(type, self) {
88
url <- glue::glue("{self$BASE_URL}{league}/{type}s")
99
}
1010

11-
r <- httpcache::GET(url)
12-
httr::stop_for_status(r)
13-
response <- r %>%
14-
httr::content(as = "text", encoding = "UTF-8") %>%
15-
jsonlite::fromJSON() %>%
16-
dplyr::mutate(competition = league)
11+
response <- .execute_query(url)
12+
response <- response %>% dplyr::mutate(competition = league)
1713

1814
entity_all <- entity_all %>% dplyr::bind_rows(response)
1915
}
@@ -73,11 +69,7 @@ get_games <- function(self, leagues, game_ids, team_ids, team_names, seasons, st
7369
for (league in leagues) {
7470
url <- glue::glue("{self$BASE_URL}{league}/games")
7571

76-
r <- httpcache::GET(url, query = query)
77-
httr::stop_for_status(r)
78-
response <- r %>%
79-
httr::content(as = "text", encoding = "UTF-8") %>%
80-
jsonlite::fromJSON()
72+
response <- .execute_query(url, query)
8173

8274
games <- games %>%
8375
dplyr::bind_rows(response) %>%
@@ -136,3 +128,13 @@ get_games <- function(self, leagues, game_ids, team_ids, team_names, seasons, st
136128
names <- tolower(names)
137129
return(names)
138130
}
131+
132+
.execute_query <- function(url, query = list()) {
133+
r <- httpcache::GET(url, query = query)
134+
httr::stop_for_status(r)
135+
response <- r %>%
136+
httr::content(as = "text", encoding = "UTF-8") %>%
137+
jsonlite::fromJSON()
138+
139+
return(response)
140+
}

0 commit comments

Comments
 (0)