Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to new API url #21

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading