Skip to content

Commit cbddb06

Browse files
committed
Document the package more extensively...
1 parent 3cc0ac4 commit cbddb06

26 files changed

+2728
-169
lines changed

R/api.R

Lines changed: 173 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,60 @@
1-
#' Make API request to ExploreCourses endpoint
1+
#' Make HTTP request to Stanford ExploreCourses API endpoint
2+
#'
3+
#' Makes an HTTP GET request to a Stanford ExploreCourses API endpoint and returns
4+
#' the response as an XML document. Handles response validation and error cases.
5+
#'
6+
#' @param url Character string. Complete URL for the ExploreCourses API endpoint.
7+
#' Expected to be one of:
8+
#' - Departments endpoint: `DEPARTMENTS_ENDPOINT`
9+
#' - Course search endpoint: `COURSE_ENDPOINT` with parameters
10+
#'
11+
#' @return
12+
#' An [xml2::xml_document-class] object containing the parsed API response.
13+
#' The structure depends on the specific endpoint called.
14+
#'
15+
#' @seealso
16+
#'
17+
#' - [httr2::request()] for the underlying HTTP request functionality
18+
#' - [xml2::read_xml()] for XML parsing
19+
#'
20+
#' @details
21+
#' The function performs these steps:
22+
#'
23+
#' 1. Makes HTTP GET request using `httr2`
24+
#' 2. Validates HTTP response status (must be 200)
25+
#' 3. Converts response body to XML document
26+
#'
27+
#' Error handling covers:
28+
#'
29+
#' - Network/connection failures
30+
#' - Non-200 HTTP status codes
31+
#' - XML parsing errors
32+
#'
33+
#' All errors are converted to standardized error messages with
34+
#' endpoint information and original error details.
35+
#'
36+
#' @section Rate Limiting:
37+
#' The ExploreCourses API does not currently require authentication or impose
38+
#' rate limits, but considerate usage is recommended.
39+
#'
40+
#' @section Response Format:
41+
#' The API returns XML data in the ExploreCourses schema version 20140630.
42+
#' The exact structure varies by endpoint:
43+
#'
44+
#' - Departments: List of schools containing departments
45+
#' - Courses: List of courses with sections and schedules
246
#'
3-
#' @param url API endpoint URL
4-
#' @return Raw response content or error
547
#' @keywords internal
48+
#'
49+
#' @examples
50+
#' \dontrun{
51+
#' # Get departments list
52+
#' deps_xml <- make_api_request(DEPARTMENTS_ENDPOINT)
53+
#'
54+
#' # Get courses for CS department
55+
#' courses_url <- sprintf(COURSE_ENDPOINT, year = "20232024", name = "CS")
56+
#' cs_xml <- make_api_request(courses_url)
57+
#' }
658
#' @include constants.R
759
make_api_request <- function(url) {
860
tryCatch({
@@ -27,20 +79,132 @@ make_api_request <- function(url) {
2779
})
2880
}
2981

30-
#' Fetch raw department data from API
82+
#' Fetch raw department list from Stanford ExploreCourses API
83+
#'
84+
#' Makes a direct API call to retrieve the complete list of Stanford departments
85+
#' and their associated schools. Returns the raw XML response without any
86+
#' processing or transformation.
87+
#'
88+
#' @details
89+
#' This function is a thin wrapper around [make_api_request()] specifically for
90+
#' the departments endpoint. It retrieves the current academic year's department
91+
#' structure including:
92+
#'
93+
#' - Schools
94+
#' - Departments within each school
95+
#' - Department codes and full names
96+
#'
97+
#' The department list is relatively static, changing only when Stanford's
98+
#' organizational structure changes.
99+
#'
100+
#' @return
101+
#' An [xml2::xml_document-class] object containing the raw departments XML.
102+
#'
103+
#' The XML structure includes:
104+
#'
105+
#' - Root `schools` element
106+
#' - Multiple `school` elements, each containing:
107+
#' - `@name` attribute: School name
108+
#' - Multiple `department` elements, each containing:
109+
#' - `@name` attribute: Department code
110+
#' - `@longname` attribute: Full department name
111+
#'
112+
#' @seealso
113+
#'
114+
#' - [make_api_request()] for the underlying API request functionality
115+
#' - [process_departments_xml()] for processing this XML data
116+
#' - [fetch_departments()] for the high-level interface
117+
#'
118+
#' @section API Endpoint:
119+
#' Uses the `DEPARTMENTS_ENDPOINT` constant, which points to:
120+
#' `https://explorecourses.stanford.edu/?view=xml-20140630`
31121
#'
32-
#' @return Raw XML content
33122
#' @keywords internal
123+
#'
124+
#' @examples
125+
#' \dontrun{
126+
#' xml_doc <- fetch_departments_raw()
127+
#' schools <- xml2::xml_find_all(xml_doc, "//school")
128+
#' }
34129
fetch_departments_raw <- function() {
35130
make_api_request(DEPARTMENTS_ENDPOINT)
36131
}
37132

38-
#' Fetch raw course data for a department from API
133+
#' Fetch raw course data for a department from Stanford ExploreCourses API
134+
#'
135+
#' Makes a direct API call to retrieve all courses for a specific department and
136+
#' academic year from Stanford's ExploreCourses system. Returns the raw XML
137+
#' response without processing.
138+
#'
139+
#' @param name Character string. Department code (e.g., `"CS"` for Computer Science).
140+
#' Must be a valid Stanford department code.
141+
#' @param year Character string or `NULL`. Academic year in format `YYYYYYYY`
142+
#' (e.g., `"20232024"`). If `NULL`, defaults to current academic year.
143+
#' Will be validated using [validate_academic_year()].
144+
#'
145+
#' @return
146+
#' An [xml2::xml_document-class] object containing the raw courses XML.
147+
#' The XML structure includes:
148+
#'
149+
#' - Root `courses` element containing multiple `course` elements
150+
#' - Each `course` element contains:
151+
#' - Basic course information (ID, title, description)
152+
#' - Units information
153+
#' - Section data
154+
#' - Schedule information
155+
#' - Instructor details
156+
#'
157+
#' @seealso
158+
#'
159+
#' - [validate_academic_year()] for year format validation
160+
#' - [make_api_request()] for the underlying API request
161+
#' - [process_courses_xml()] for processing this XML data
162+
#' - [fetch_department_courses()] for the high-level interface
163+
#'
164+
#' @details
165+
#' This function:
166+
#'
167+
#' 1. Validates the academic year format
168+
#' 2. Constructs the API URL using `COURSE_ENDPOINT` template
169+
#' 3. Makes the API request for course data
170+
#'
171+
#' The API returns all active courses for the specified department,
172+
#' including detailed information about:
173+
#'
174+
#' - Course metadata (title, description, units)
175+
#' - Sections and components
176+
#' - Meeting schedules
177+
#' - Instructor information
178+
#'
179+
#' @section API Endpoint:
180+
#'
181+
#' Uses the `COURSE_ENDPOINT` template, which expands to:
182+
#'
183+
#' ```
184+
#' https://explorecourses.stanford.edu/search?view=xml-20140630
185+
#' &academicYear={year}
186+
#' &q={name}
187+
#' &filter-departmentcode-{name}=on
188+
#' &filter-coursestatus-Active=on
189+
#' ```
190+
#'
191+
#' @section Filters:
192+
#'
193+
#' The API request automatically includes filters for:
194+
#'
195+
#' - Department code match
196+
#' - Active courses only
39197
#'
40-
#' @param name Department code
41-
#' @param year Academic year in format YYYYYYYY or NULL for current year
42-
#' @return Raw XML content
43198
#' @keywords internal
199+
#'
200+
#' @examples
201+
#' \dontrun{
202+
#' # Get current year CS courses
203+
#' cs_xml <- fetch_department_courses_raw("CS")
204+
#'
205+
#' # Get specific year Math courses
206+
#' math_xml <- fetch_department_courses_raw("MATH", "20232024")
207+
#' }
44208
fetch_department_courses_raw <- function(name, year = NULL) {
45209
year <- validate_academic_year(year)
46210
url <- glue::glue(COURSE_ENDPOINT, year = year, name = name)

0 commit comments

Comments
 (0)