(&mut self, username: &str, password: &str)
| 171 | } |
| 172 | |
| 173 | async fn login(&mut self, username: &str, password: &str) -> io::Result<LoginResponse> { |
| 174 | // TODO(https://github.com/seanmonstar/reqwest/pull/1096): Replace with a basic_auth() |
| 175 | // call on the RequestBuilder once it is supported in WASM. |
| 176 | let basic_auth = |
| 177 | format!("Basic {}", BASE64_STANDARD.encode(format!("{}:{}", username, password))); |
| 178 | |
| 179 | let response = self |
| 180 | .client |
| 181 | .post(self.make_url("api/login")) |
| 182 | .headers(self.default_headers()) |
| 183 | .header("Authorization", basic_auth) |
| 184 | .header("Content-Length", 0) |
| 185 | .send() |
| 186 | .await |
| 187 | .map_err(reqwest_error_to_io_error)?; |
| 188 | match response.status() { |
| 189 | StatusCode::OK => { |
| 190 | let bytes = response.bytes().await.map_err(reqwest_error_to_io_error)?; |
| 191 | let response: LoginResponse = serde_json::from_reader(bytes.reader())?; |
| 192 | let auth_data = AuthData { |
| 193 | username: username.to_owned(), |
| 194 | access_token: response.access_token.clone(), |
| 195 | }; |
| 196 | *(self.auth_data.borrow_mut()) = Some(auth_data); |
| 197 | Ok(response) |
| 198 | } |
| 199 | _ => Err(http_response_to_io_error(response).await), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | async fn logout(&mut self) -> io::Result<()> { |
| 204 | let mut auth_data = self.auth_data.borrow_mut(); |
no test coverage detected