Skip to content

Commit

Permalink
set user agent in get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JaseZiv committed Jul 31, 2024
1 parent 58ada89 commit 94a3465
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: bettRtab
Title: API wrapper to interact with the betting company TAB
Version: 0.0.0.8000
Version: 0.0.1
Authors@R: c(
person("Jason", "Zivkovic", , "jaseziv83@gmail.com", role = c("aut", "cre", "cph"))
)
Expand Down Expand Up @@ -29,5 +29,5 @@ Suggests:
rmarkdown,
testthat (>= 3.0.0)
Encoding: UTF-8
RoxygenNote: 7.2.1
RoxygenNote: 7.3.2
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
### Bugs

* `get_race_meet_meta()` not retrying when content returned is empty ("") (0.0.0.8000)
* Manually set a user agent as it appears to be a new requirement of the TAB (0.0.1)

12 changes: 2 additions & 10 deletions R/get_race_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
#'
.get_each_past <- function(url) {
Sys.sleep(1)
resp <- httr::RETRY("GET",
url = url,
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2) %>%
resp <- .RETRY_GET_tab(url = url) %>%
httr::content(as = "text", encoding = "UTF-8")

# need a while loop here as there were still times when the API was failing and returning a list of length zero
Expand All @@ -30,11 +26,7 @@
stopifnot("The API is not accepting this request. Please try again." = iter <21)

Sys.sleep(1)
resp <- httr::RETRY("GET",
url = url,
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2) %>%
resp <- .RETRY_GET_tab(url = url) %>%
httr::content(as = "text", encoding = "UTF-8")
}

Expand Down
12 changes: 2 additions & 10 deletions R/get_race_meets.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

each_url <- paste0('https://api.beta.tab.com.au/v1/historical-results-service/VIC/racing/', each_date)

history <- httr::RETRY("GET",
url = each_url,
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2)
history <- .RETRY_GET_tab(url = each_url)

history_content <- suppressMessages(tryCatch(httr::content(history, "text"), error = function(e) NA_character_))

Expand All @@ -35,11 +31,7 @@

Sys.sleep(2)

history <- httr::RETRY("GET",
url = each_url,
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2)
history <- .RETRY_GET_tab(url = each_url)


history_content <- suppressMessages(tryCatch(httr::content(history, "text"), error = function(e) NA_character_))
Expand Down
3 changes: 2 additions & 1 deletion R/get_sports_market.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ get_sports_market <- function(competition_name) {
dplyr::pull(.data[["self"]]) %>% unlist()


res <- httr::GET(link_url) %>% httr::content()
res <- .RETRY_GET_tab(url = link_url) %>% httr::content()


aa <- res$matches

Expand Down
26 changes: 26 additions & 0 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,29 @@
tryCatch(readRDS(url(file_url)), error = function(e) data.frame()) %>%
suppressWarnings()
}


#' GET with specified user agent
#'
#' GET a URL, but with a pre-specified user agent
#'
#' @param url Uthe url of the page to retrieve
#'
#' @noRd
#'
.RETRY_GET_tab <- function(url) {

headers = c(
`User-Agent` = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)

res <- httr::RETRY("GET",
config = httr::add_headers(.headers=headers),
url = url,
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2)
}



13 changes: 3 additions & 10 deletions R/live_markets.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
#' })
#' }
get_live_sports <- function() {
res <- httr::RETRY("GET",
url = "https://api.beta.tab.com.au/v1/recommendation-service/live-events?jurisdiction=VIC",
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2)

res <- .RETRY_GET_tab(url = "https://api.beta.tab.com.au/v1/recommendation-service/live-events?jurisdiction=VIC")

resp <- suppressMessages(tryCatch(httr::content(res, "text"), error = function(e) NA_character_))

Expand All @@ -36,11 +33,7 @@ get_live_sports <- function() {
stopifnot("The API is not accepting this request. Please try again." = iter <21)

Sys.sleep(1)
res <- httr::RETRY("GET",
url = "https://api.beta.tab.com.au/v1/recommendation-service/live-events?jurisdiction=VIC",
times = 5, # the function has other params to tweak its behavior
pause_min = 5,
pause_base = 2) %>%
res <- .RETRY_GET_tab(url = "https://api.beta.tab.com.au/v1/recommendation-service/live-events?jurisdiction=VIC") %>%
httr::content(as = "text", encoding = "UTF-8")

resp <- suppressMessages(tryCatch(httr::content(res, "text"), error = function(e) NA_character_))
Expand Down

0 comments on commit 94a3465

Please sign in to comment.