|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use url::Url; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + default_reqwest_builder, CourseDetails, CourseInfo, Language, QueryError, SearchOptions, |
| 7 | + DEFAULT_API_URL, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct ClientBuilder { |
| 12 | + reqwest_client: reqwest::Client, |
| 13 | + base_url: Url, |
| 14 | +} |
| 15 | + |
| 16 | +impl ClientBuilder { |
| 17 | + pub fn new() -> Self { |
| 18 | + ClientBuilder { |
| 19 | + reqwest_client: default_reqwest_builder().build().unwrap(), |
| 20 | + base_url: Url::parse(&DEFAULT_API_URL).unwrap(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn reqwest_client(mut self, client: reqwest::Client) -> Self { |
| 25 | + self.reqwest_client = client; |
| 26 | + self |
| 27 | + } |
| 28 | + |
| 29 | + pub fn api_url(mut self, url: Url) -> Self { |
| 30 | + self.base_url = url; |
| 31 | + self |
| 32 | + } |
| 33 | + |
| 34 | + pub fn build(self) -> Q { |
| 35 | + Q { |
| 36 | + http_client: self.reqwest_client, |
| 37 | + base_url: self.base_url, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub struct Q { |
| 43 | + http_client: reqwest::Client, |
| 44 | + base_url: Url, |
| 45 | +} |
| 46 | + |
| 47 | +impl Q { |
| 48 | + pub fn new() -> Self { |
| 49 | + ClientBuilder::new().build() |
| 50 | + } |
| 51 | + |
| 52 | + pub async fn search( |
| 53 | + &self, |
| 54 | + options: &SearchOptions, |
| 55 | + merge_courses: bool, |
| 56 | + ) -> Result<Vec<CourseInfo>, QueryError> { |
| 57 | + let url = self.base_url.join("courses").unwrap(); |
| 58 | + |
| 59 | + let resp = match self.http_client.post(url).json(&options).send().await { |
| 60 | + Ok(resp) => resp, |
| 61 | + Err(e) => return Err(QueryError::HttpError(format!("{}", e))), |
| 62 | + }; |
| 63 | + |
| 64 | + match resp.json::<Vec<CourseInfo>>().await { |
| 65 | + Ok(json) => Ok(if merge_courses { |
| 66 | + crate::merge_courses(json) |
| 67 | + } else { |
| 68 | + json |
| 69 | + }), |
| 70 | + Err(e) => return Err(QueryError::ParseError(format!("{}", e))), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + pub async fn query( |
| 75 | + &self, |
| 76 | + semester: &str, |
| 77 | + course_no: &str, |
| 78 | + language: Language, |
| 79 | + ) -> Result<CourseDetails, QueryError> { |
| 80 | + // is "coursedetials" not "coursedetails" |
| 81 | + // looks like an idiotic typo in the API |
| 82 | + let url = self.base_url.join("coursedetials").unwrap(); |
| 83 | + |
| 84 | + let mut params = HashMap::new(); |
| 85 | + params.insert("semester", semester); |
| 86 | + params.insert("course_no", course_no); |
| 87 | + params.insert("language", language.as_str()); |
| 88 | + |
| 89 | + let resp = match self.http_client.get(url).query(¶ms).send().await { |
| 90 | + Ok(resp) => resp, |
| 91 | + Err(e) => return Err(QueryError::HttpError(format!("{}", e))), |
| 92 | + }; |
| 93 | + |
| 94 | + let full = match resp.bytes().await { |
| 95 | + Ok(bytes) => bytes, |
| 96 | + Err(e) => return Err(QueryError::ParseError(format!("{}", e))), |
| 97 | + }; |
| 98 | + |
| 99 | + // # debug |
| 100 | + // let text = match String::from_utf8(full.to_vec()) { |
| 101 | + // Ok(bytes) => bytes, |
| 102 | + // Err(e) => return Err(QueryError::ParseError(format!("{}", e))), |
| 103 | + // }; |
| 104 | + // println!("{}", text); |
| 105 | + |
| 106 | + let json = match serde_json::from_slice::<Vec<CourseDetails>>(&full) { |
| 107 | + Ok(json) => json, |
| 108 | + Err(e) => return Err(QueryError::ParseError(format!("{}", e))), |
| 109 | + }; |
| 110 | + |
| 111 | + if let Some(course_details) = json.get(0) { |
| 112 | + Ok(course_details.clone()) |
| 113 | + } else { |
| 114 | + Err(QueryError::ParseError(format!("No course found"))) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(test)] |
| 120 | +mod tests { |
| 121 | + use super::*; |
| 122 | + use futures::future::join_all; |
| 123 | + |
| 124 | + #[tokio::test] |
| 125 | + async fn new() { |
| 126 | + let _client = Q::new(); |
| 127 | + } |
| 128 | + |
| 129 | + #[tokio::test] |
| 130 | + async fn search() { |
| 131 | + let client = Q::new(); |
| 132 | + |
| 133 | + let mut options = SearchOptions::new("1131", Language::Zh); |
| 134 | + |
| 135 | + options.course_no = "cs".to_string(); |
| 136 | + |
| 137 | + let _details = client |
| 138 | + .search(&options, true) |
| 139 | + .await |
| 140 | + .expect("failed to search courses"); |
| 141 | + |
| 142 | + println!("{:#?}", _details); |
| 143 | + } |
| 144 | + |
| 145 | + #[tokio::test] |
| 146 | + async fn query() { |
| 147 | + let client = Q::new(); |
| 148 | + |
| 149 | + let _details = client |
| 150 | + .query("1122", "AT2005701", Language::Zh) |
| 151 | + .await |
| 152 | + .expect("Failed to query"); |
| 153 | + |
| 154 | + println!("{:#?}", _details) |
| 155 | + } |
| 156 | + |
| 157 | + #[tokio::test] |
| 158 | + async fn query_all_cs() { |
| 159 | + let client = Q::new(); |
| 160 | + |
| 161 | + let mut options = SearchOptions::new("1131", Language::Zh); |
| 162 | + |
| 163 | + options.course_no = "cs".to_string(); |
| 164 | + |
| 165 | + let search_results = client |
| 166 | + .search(&options, true) |
| 167 | + .await |
| 168 | + .expect("failed to search courses"); |
| 169 | + |
| 170 | + let futures = search_results.into_iter().map(|c| async move { |
| 171 | + let query_client = Q::new(); |
| 172 | + let details = query_client |
| 173 | + .query(&c.semester, &c.course_no, Language::Zh) |
| 174 | + .await |
| 175 | + .expect("Failed to query"); |
| 176 | + |
| 177 | + println!("{:#?}", details); |
| 178 | + }); |
| 179 | + |
| 180 | + join_all(futures).await; |
| 181 | + } |
| 182 | +} |
0 commit comments