Skip to content

Commit

Permalink
Update to new API url
Browse files Browse the repository at this point in the history
  • Loading branch information
llrs committed Jan 25, 2025
1 parent 1d9d9f4 commit b9e0e5b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# BOE (development version)

* Update url to retrieve data from the API.

* Now `retrieve_sumario()` provides informative error if a cve is provided
instead of a date (#17).

Expand Down
8 changes: 7 additions & 1 deletion R/codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ sumario_nbo <- function(date, journal = "BOE") {

#' @describeIn sumario_nbo For compatibility with previous version
#' @export
sumario_xml <- sumario_nbo
sumario_xml <- function(date) {
if (is(date, "Date")) {
date <- format(date, "%Y%m%d")
}
check_date(date)
date
}

#' Create the number of the _sumario_
#'
Expand Down
15 changes: 13 additions & 2 deletions R/queries.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ query_xml <- function(id) {
check_code(id)
force(BASE_URL)
force(JOURNAL_URL)

journal <- strsplit(id, split = "-", fixed = TRUE)[[1]][1]
journal <- match.arg(journal, c("BOE", "BORME"))
httr::modify_url(BASE_URL,
Expand All @@ -27,6 +28,16 @@ query_xml <- function(id) {
}


query_xml_sumario <- function(id, journal = "BOE") {
force(BASE_URL)
force(JOURNAL_URL)

# On 2024-10-08
# https://boe.es/datosabiertos/api/boe/sumario/20250125
journal <- match.arg(journal, c("BOE", "BORME"))
path <- paste0("datosabiertos/api/", tolower(journal), "/sumario/", id)
httr::modify_url(BASE_URL, path = path)
}

#' Build a query for the webpage
#'
Expand Down Expand Up @@ -111,7 +122,7 @@ query_pdf <- function(year, month, day, code) {
#' \donttest{get_xml(url)}
get_xml <- function(query) {
user_agent <- user_agent("https://github.com/llrs/BOE")
response <- GET(query, user_agent)
response <- GET(query, httr::accept_xml(), user_agent)
httr::stop_for_status(response)
if (status_code(response) != 200) {
stop("Could not retrieve the data.", call. = FALSE)
Expand All @@ -122,5 +133,5 @@ get_xml <- function(query) {
if (http_type(response) != "application/xml") {
stop("API did not find the requested document.", call. = FALSE)
}
content(response)
content(response, encoding = "UTF-8")
}
5 changes: 2 additions & 3 deletions R/retrieve.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ retrieve_sumario <- function(date, journal = "BOE") {
}
check_date(date)
journal <- match.arg(journal, c("BOE", "BORME"))
sumario_nbo <- sumario_nbo(date, journal)

tidy_sumario(get_xml(query_xml(sumario_nbo)))
sumario_nbo <- sumario_xml(date)
tidy_sumario(get_xml(query_xml_sumario(sumario_nbo)))
}


Expand Down

0 comments on commit b9e0e5b

Please sign in to comment.