|
| 1 | +use crate::Auth; |
| 2 | +use crate::api::{CampusQuery, CampusesQuery, CollegeQuery, CollegesQuery}; |
| 3 | +use crate::auth::AuthenticatedClient; |
| 4 | +use crate::error::Result; |
| 5 | +use crate::{GroupsQuery, ScheduleQuery, error::Error}; |
1 | 6 | /// A client for interacting with the educational schedule API. |
2 | 7 | /// |
3 | 8 | /// The `Client` provides methods to query colleges, campuses, groups, and schedules. |
|
14 | 19 | #[cfg(feature = "logging")] |
15 | 20 | use tracing::{debug, error}; |
16 | 21 |
|
17 | | -use crate::api::{CampusQuery, CampusesQuery, CollegeQuery, CollegesQuery}; |
18 | | -use crate::error::Result; |
19 | | -use crate::{GroupsQuery, ScheduleQuery, error::Error}; |
20 | | - |
21 | 22 | #[derive(Debug, Clone)] |
22 | 23 | pub struct Client { |
23 | 24 | pub(crate) base_url: String, |
@@ -216,6 +217,95 @@ impl Client { |
216 | 217 | )) |
217 | 218 | } |
218 | 219 | } |
| 220 | + pub(crate) async fn post_json<T, B>( |
| 221 | + &self, |
| 222 | + path: &str, |
| 223 | + body: Option<&B>, |
| 224 | + auth: Option<&Auth>, |
| 225 | + ) -> Result<T> |
| 226 | + where |
| 227 | + T: serde::de::DeserializeOwned, |
| 228 | + B: serde::Serialize, |
| 229 | + { |
| 230 | + let url = format!("{}{}", self.base_url, path); |
| 231 | + #[cfg(feature = "logging")] |
| 232 | + debug!("POST {}", url); |
| 233 | + |
| 234 | + let mut request = self.http_client.post(&url); |
| 235 | + |
| 236 | + if let Some(auth) = auth { |
| 237 | + request = auth.apply_to_request(request); |
| 238 | + } |
| 239 | + |
| 240 | + if let Some(body) = body { |
| 241 | + request = request.json(body); |
| 242 | + } |
| 243 | + |
| 244 | + let response = request.send().await.map_err(crate::error::Error::Reqwest)?; |
| 245 | + |
| 246 | + self.handle_response(response).await |
| 247 | + } |
| 248 | + |
| 249 | + pub(crate) async fn delete_json<T>(&self, path: &str, auth: Option<&Auth>) -> Result<T> |
| 250 | + where |
| 251 | + T: serde::de::DeserializeOwned, |
| 252 | + { |
| 253 | + let url = format!("{}{}", self.base_url, path); |
| 254 | + #[cfg(feature = "logging")] |
| 255 | + debug!("DELETE {}", url); |
| 256 | + |
| 257 | + let mut request = self.http_client.delete(&url); |
| 258 | + |
| 259 | + if let Some(auth) = auth { |
| 260 | + request = auth.apply_to_request(request); |
| 261 | + } |
| 262 | + |
| 263 | + let response = request.send().await.map_err(crate::error::Error::Reqwest)?; |
| 264 | + |
| 265 | + self.handle_response(response).await |
| 266 | + } |
| 267 | + |
| 268 | + async fn handle_response<T>(&self, response: reqwest::Response) -> Result<T> |
| 269 | + where |
| 270 | + T: serde::de::DeserializeOwned, |
| 271 | + { |
| 272 | + let status = response.status(); |
| 273 | + let raw_body = response |
| 274 | + .text() |
| 275 | + .await |
| 276 | + .map_err(crate::error::Error::Reqwest)?; |
| 277 | + |
| 278 | + #[cfg(feature = "logging")] |
| 279 | + { |
| 280 | + if status.is_success() { |
| 281 | + debug!("Success {}: raw response = {}", status, raw_body); |
| 282 | + } else { |
| 283 | + error!("API error {}: raw response = {}", status, raw_body); |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + if status.is_success() { |
| 288 | + if raw_body.is_empty() { |
| 289 | + // Handle empty response for DELETE and some POST requests |
| 290 | + serde_json::from_str("null").map_err(|e| { |
| 291 | + #[cfg(feature = "logging")] |
| 292 | + error!("JSON parse error for empty response: {}", e); |
| 293 | + crate::error::Error::Serialization(e) |
| 294 | + }) |
| 295 | + } else { |
| 296 | + serde_json::from_str(&raw_body).map_err(|e| { |
| 297 | + #[cfg(feature = "logging")] |
| 298 | + error!("JSON parse error: {}\nRaw body: {}", e, raw_body); |
| 299 | + crate::error::Error::Serialization(e) |
| 300 | + }) |
| 301 | + } |
| 302 | + } else { |
| 303 | + Err(crate::error::Error::from_response( |
| 304 | + status.as_u16(), |
| 305 | + raw_body, |
| 306 | + )) |
| 307 | + } |
| 308 | + } |
219 | 309 |
|
220 | 310 | /// Creates a query to list groups for a campus. |
221 | 311 | /// |
@@ -252,6 +342,10 @@ impl Client { |
252 | 342 | pub fn tomorrow(&self, group_id: u32) -> ScheduleQuery { |
253 | 343 | self.schedule(group_id).tomorrow() |
254 | 344 | } |
| 345 | + /// Create an authenticated client for private endpoints |
| 346 | + pub fn authenticated(&self) -> AuthenticatedClient { |
| 347 | + AuthenticatedClient::new(self.clone()) |
| 348 | + } |
255 | 349 | } |
256 | 350 |
|
257 | 351 | #[cfg(test)] |
|
0 commit comments