Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aberuiz committed Nov 17, 2024
1 parent 18cf1b0 commit 85282f9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
Binary file modified .DS_Store
Binary file not shown.
35 changes: 26 additions & 9 deletions R/getMonitorList.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ getMonitorList <- function(legiKey = NULL){
return(paste0("Invalid API Key: ",legiKey," Register <https://legiscan.com/user/register> Store with `setlegiKey`"))
}

req <- httr2::request("https://api.legiscan.com") |>
httr2::req_url_query(
key = legiKey,
op = op,
.multi = "explode"
) |>
httr2::req_perform() |>
httr2::resp_body_json()
tryCatch({
req <- httr2::request("https://api.legiscan.com") |>
httr2::req_url_query(
key = legiKey,
op = op,
.multi = "explode"
) |>
httr2::req_perform()

return(dplyr::bind_rows(req$monitorlist))
status <- httr2::resp_status(req)
if (status != 200) {
stop(sprintf("API request failed with status code: %d", status))
}

response <- httr2::resp_body_json(req)

if (!is.null(response$status)) {
if (response$status != "OK") {
stop(sprintf("API returned error: %s", response$alert))
}
}

return(dplyr::bind_rows(response$monitorlist))

}, error = function(e) {
stop(sprintf("Error in API request: %s", e$message))
})
}
25 changes: 25 additions & 0 deletions R/getPerson.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#'
#' @param PeopleID integer value from legiscan
#'
#' @param legiKey 32 character string provided by legiscan
#'
#' @export
getPerson <- function(PeopleID = NULL, legiKey = NULL){
op <- "getPerson"
Expand All @@ -27,4 +29,27 @@ getPerson <- function(PeopleID = NULL, legiKey = NULL){
httr2::resp_body_json()
print(req$person$name)
return(dplyr::bind_rows(req$person))

tryCatch({
req <- httr2::request("https://api.legiscan.com") |>
httr2::req_url_query(
key = legiKey,
op = op,
id = PeopleID
) |>
httr2::req_perform()

response <- httr2::resp_body_json(req)

if (!is.null(response$status)) {
if (response$status != "OK") {
stop(sprintf("API returned error: %s", response$alert))
}
}

return(dplyr::bind_rows(req$person))

}, error = function(e) {
stop(sprintf("Error in API request: %s", e$message))
})
}
2 changes: 1 addition & 1 deletion R/getSessionPeople.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @param legiKey 32 character string provided by legiscan
#'
#' @examples
#' getSessionPeople(session = 2003)
#' getSessionPeople(session = 2160)
#'
#' @export
getSessionPeople <- function(session = NULL, legiKey = NULL){
Expand Down
2 changes: 2 additions & 0 deletions man/getPerson.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/getSessionPeople.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85282f9

Please sign in to comment.