I have the following function
my_func <- function(sport,
eventId = eventids,
apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx",
regions = "us",
markets = "alternate_spreads",
odds_format = "american",
date_format = "iso"){
base_url = glue::glue("https://xxx.xxx-xxx-
xxx.xxx/xx/xxxxxx/{sport}/xxxxxx/{eventId}/odds")
query_params <- list(apiKey = apiKey,
sport = sport,
eventId = eventId,
regions = regions,
markets = markets,
oddsFormat = odds_format,
dateFormat = date_format)
endpoint <- httr::modify_url(base_url, query = query_params)
resp <- fromJSON(endpoint) %>%
as_tibble(validate = F) %>%
unnest(cols = bookmakers) %>%
rename("bookmaker_key" = "key") %>%
unnest(cols = markets) %>%
unnest(cols = outcomes)
return(resp)
}
I am trying pass the following list of eventids as an argument
eventids <- followinglist
A tibble: 2 × 1
id
<chr>
1 xxxxxxxxxxxxxxxxadaf06d2660af34
2 xxxxxxxxxxxxxxxa67aa5a4bce731a0
When I execute the following syntax
my_func('basketball')
I get the following error:
Error in open.connection(con, "rb") :
cannot open the connection to 'https://xxx.xxx-xxxx-xx/sports/basketball/events/truncated?
In addition: Warning message:
In open.connection(con, "rb") :
cannot open URL 'https://xxx.xxx-xxxx-xx/sports/basketball/events/truncated?
HTTP status was '422 Unknown Error'
I have also tried using the following format of eventids
eventids <- c("xxxxxxxxxxxxxx57adaf06d2660af34", "xxxxxxxxxxx7aa5a4bce731a0")
and get the following error when running running the function
Error in parse_url(url) : length(url) == 1 is not TRUE
I found another post that suggested using
eventids1 = paste(eventids, collapse = ",")
However, I get the same error as above
Error in open.connection(con, "rb") :
cannot open the connection to 'https://xxx.xxx-xxxx - this has been truncated
I have also tried using the following
map(eventids, my_func)
but get the following error
Error in `map()`:
ℹ In index: 1.
Caused by error in `parse_url()`:
! length(url) == 1 is not TRUE
Run `rlang::last_error()` to see where the error occurred.
I tried running the following
map(eventids, ~my_func(sport = "basketball", eventId = .x))
but got the following error.
Error in `map()`:
ℹ In index: 1.
ℹ With name: id.
Caused by error in `parse_url()`:
! length(url) == 1 is not TRUE
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/purrr_error_indexed>
Error in `map()`:
ℹ In index: 1.
ℹ With name: id.
Caused by error in `parse_url()`:
! length(url) == 1 is not TRUE
---
Backtrace:
1. purrr::map(eventids, ~my_func(sport = "basketball_nba", eventId =
.x))
2. purrr:::map_("list", .x, .f, ..., .progress = .progress)
6. global .f(.x[[i]], ...)
7. global my_func(sport = "basketball_nba", eventId = .x)
8. httr::modify_url(base_url, query = query_params)
9. httr::parse_url(url)
10. base::stopifnot(length(url) == 1)
11. base::stop(simpleError(msg, call = if (p <- sys.parent(1L))
sys.call(p)))
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/purrr_error_indexed>
Error in `map()`:
ℹ In index: 1.
ℹ With name: id.
Caused by error in `parse_url()`:
! length(url) == 1 is not TRUE
---
Backtrace:
▆
1. ├─purrr::map(eventids, ~my_func(sport = "basketball_nba", eventId =
.x))
2. │ └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
3. │ ├─purrr:::with_indexed_errors(...)
4. │ │ └─base::withCallingHandlers(...)
5. │ ├─purrr:::call_with_cleanup(...)
6. │ └─global .f(.x[[i]], ...)
7. │ └─global my_func(sport = "basketball_nba", eventId = .x)
8. │ └─httr::modify_url(base_url, query = query_params)
9. │ └─httr::parse_url(url)
10. │ └─base::stopifnot(length(url) == 1)
11. │ └─base::stop(simpleError(msg, call = if (p <-
sys.parent(1L)) sys.call(p)))
12. └─purrr (local) `<fn>`(`<smplErrr>`)
13. └─cli::cli_abort(...)
14. └─rlang::abort(...)
I know the function works because if I pass a single eventid, I get a response and data
my_func('basketball', 'xxxxxxxxxxxxxxxx4bce731a0')
The API only allows a single eventid to be passed at a time which is my reason for wanting to pass a list of eventids. How can I query the API by passing a list and having the responses written to a tibble? Thank you in advance.