Skip to content

Commit

Permalink
Improve caching; add academicyear as a parameter; harden errors and w…
Browse files Browse the repository at this point in the history
…arnings; break apart processing from api requests
  • Loading branch information
coatless committed Nov 18, 2024
1 parent 9644e8b commit 60ccc54
Show file tree
Hide file tree
Showing 39 changed files with 1,247 additions and 440 deletions.
19 changes: 10 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ License: AGPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
Imports:
httr2,
xml2,
dplyr,
jsonlite,
purrr,
dplyr,
tibble,
glue,
fs,
tibble,
future.apply,
progressr,
readr
cli,
furrr,
progressr
Collate:
'cache.R'
'constants.R'
'api.R'
'cache.R'
'explorecourses-package.R'
'fetch.R'
'schedule.R'
'process.R'
'utils.R'
Depends:
R (>= 2.10)
LazyData: true
8 changes: 6 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(print,explorecourses_cache_list)
export(cache_exists)
export(clear_cache)
export(fetch_all_courses)
export(fetch_department_courses)
export(fetch_departments)
export(parse_courses)
export(read_cache)
export(generate_academic_year)
export(list_cache)
export(read_xml_cache)
48 changes: 48 additions & 0 deletions R/api.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Make API request to ExploreCourses endpoint
#'
#' @param url API endpoint URL
#' @return Raw response content or error
#' @keywords internal
#' @include constants.R
make_api_request <- function(url) {
tryCatch({
req <- httr2::request(url) |>
httr2::req_perform()

if (httr2::resp_status(req) != 200) {
cli::cli_abort(c(
"API request failed",
"i" = "Status code: {httr2::resp_status(req)}",
"i" = "Endpoint: {url}"
))
}

xml2::read_xml(httr2::resp_body_string(req))
}, error = function(e) {
cli::cli_abort(c(
"Failed to make API request",
"x" = "{conditionMessage(e)}",
"i" = "Endpoint: {url}"
))
})
}

#' Fetch raw department data from API
#'
#' @return Raw XML content
#' @keywords internal
fetch_departments_raw <- function() {
make_api_request(DEPARTMENTS_ENDPOINT)
}

#' Fetch raw course data for a department from API
#'
#' @param name Department code
#' @param year Academic year in format YYYYYYYY or NULL for current year
#' @return Raw XML content
#' @keywords internal
fetch_department_courses_raw <- function(name, year = NULL) {
year <- validate_academic_year(year)
url <- glue::glue(COURSE_ENDPOINT, year = year, name = name)
make_api_request(url)
}
Loading

0 comments on commit 60ccc54

Please sign in to comment.