| 230 | #[new] |
| 231 | #[pyo3(signature = (status_code, content=None, headers=None, url=None, default_encoding="utf-8"))] |
| 232 | fn new( |
| 233 | status_code: u16, |
| 234 | content: Option<Vec<u8>>, |
| 235 | headers: Option<HashMap<String, String>>, |
| 236 | url: Option<String>, |
| 237 | default_encoding: Option<&str>, |
| 238 | ) -> Self { |
| 239 | let headers = headers.unwrap_or_default(); |
| 240 | |
| 241 | let encoding = match headers |
| 242 | .iter() |
| 243 | .find(|(k, _)| k.to_lowercase() == "content-type") |
| 244 | { |
| 245 | Some((_, ct)) => ContentType::from(ct) |
| 246 | .ok() |
| 247 | .map(|ct| ct.charset) |
| 248 | .unwrap_or_else(|| default_encoding.unwrap_or("utf-8").to_string()), |
| 249 | None => default_encoding.unwrap_or("utf-8").to_string(), |
| 250 | }; |
| 251 | |
| 252 | let reason_phrase = StatusCode::from_u16(status_code) |
| 253 | .map(|s| s.canonical_reason().unwrap_or("Unknown").to_string()) |
| 254 | .unwrap_or_else(|_| "Unknown".to_string()); |
| 255 | |
| 256 | Self { |
| 257 | status_code, |
| 258 | reason_phrase, |
| 259 | http_version: "HTTP/1.1".to_string(), |
| 260 | headers, |
| 261 | encoding, |
| 262 | is_redirect: false, |
| 263 | url: url.unwrap_or_default(), |
| 264 | is_closed: true, |
| 265 | is_stream_consumed: true, |
| 266 | text: None, |
| 267 | content: Some(content.unwrap_or_default()), |
| 268 | inner: None, |
| 269 | inner_state: InnerResponseState::Read, |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | fn __repr__(&self) -> String { |
| 274 | format!("<Response [{} {}]>", self.status_code, self.reason_phrase) |