Skip to content

Commit 60ccc54

Browse files
committed
Improve caching; add academicyear as a parameter; harden errors and warnings; break apart processing from api requests
1 parent 9644e8b commit 60ccc54

39 files changed

+1247
-440
lines changed

DESCRIPTION

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,25 @@ License: AGPL (>= 3)
1212
Encoding: UTF-8
1313
Roxygen: list(markdown = TRUE)
1414
RoxygenNote: 7.3.2
15-
Imports:
15+
Imports:
1616
httr2,
1717
xml2,
18-
dplyr,
19-
jsonlite,
2018
purrr,
19+
dplyr,
20+
tibble,
2121
glue,
2222
fs,
23-
tibble,
24-
future.apply,
25-
progressr,
26-
readr
23+
cli,
24+
furrr,
25+
progressr
2726
Collate:
28-
'cache.R'
2927
'constants.R'
28+
'api.R'
29+
'cache.R'
3030
'explorecourses-package.R'
3131
'fetch.R'
32-
'schedule.R'
32+
'process.R'
33+
'utils.R'
3334
Depends:
3435
R (>= 2.10)
3536
LazyData: true

NAMESPACE

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,explorecourses_cache_list)
4+
export(cache_exists)
5+
export(clear_cache)
36
export(fetch_all_courses)
47
export(fetch_department_courses)
58
export(fetch_departments)
6-
export(parse_courses)
7-
export(read_cache)
9+
export(generate_academic_year)
10+
export(list_cache)
11+
export(read_xml_cache)

R/api.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#' Make API request to ExploreCourses endpoint
2+
#'
3+
#' @param url API endpoint URL
4+
#' @return Raw response content or error
5+
#' @keywords internal
6+
#' @include constants.R
7+
make_api_request <- function(url) {
8+
tryCatch({
9+
req <- httr2::request(url) |>
10+
httr2::req_perform()
11+
12+
if (httr2::resp_status(req) != 200) {
13+
cli::cli_abort(c(
14+
"API request failed",
15+
"i" = "Status code: {httr2::resp_status(req)}",
16+
"i" = "Endpoint: {url}"
17+
))
18+
}
19+
20+
xml2::read_xml(httr2::resp_body_string(req))
21+
}, error = function(e) {
22+
cli::cli_abort(c(
23+
"Failed to make API request",
24+
"x" = "{conditionMessage(e)}",
25+
"i" = "Endpoint: {url}"
26+
))
27+
})
28+
}
29+
30+
#' Fetch raw department data from API
31+
#'
32+
#' @return Raw XML content
33+
#' @keywords internal
34+
fetch_departments_raw <- function() {
35+
make_api_request(DEPARTMENTS_ENDPOINT)
36+
}
37+
38+
#' Fetch raw course data for a department from API
39+
#'
40+
#' @param name Department code
41+
#' @param year Academic year in format YYYYYYYY or NULL for current year
42+
#' @return Raw XML content
43+
#' @keywords internal
44+
fetch_department_courses_raw <- function(name, year = NULL) {
45+
year <- validate_academic_year(year)
46+
url <- glue::glue(COURSE_ENDPOINT, year = year, name = name)
47+
make_api_request(url)
48+
}

0 commit comments

Comments
 (0)