client: Handle unexpected responses as errors

This commit is contained in:
Elias Projahn 2021-04-15 17:00:37 +02:00
parent bb33b30f6e
commit f4254cc431

View file

@ -110,37 +110,37 @@ impl Client {
.send_async()
.await?;
let body = response.text().await?;
Ok(body)
match response.status() {
StatusCode::OK => Ok(response.text().await?),
status_code => Err(Error::UnexpectedResponse(status_code)),
}
}
/// Make an authenticated post request to the server.
async fn post(&self, url: &str, body: String) -> Result<String> {
let body = if self.token.borrow().is_some() {
// Try to do the request using a cached login token.
if self.token.borrow().is_some() {
let mut response = self.post_priv(url, body.clone()).await?;
// Try one more time (maybe the token was expired)
if response.status() == StatusCode::UNAUTHORIZED {
if self.login().await? {
response = self.post_priv(url, body).await?;
} else {
Err(Error::LoginFailed)?;
}
// If authorization failed, try again below. Else, return early.
match response.status() {
StatusCode::UNAUTHORIZED => (),
StatusCode::OK => return Ok(response.text().await?),
status_code => return Err(Error::UnexpectedResponse(status_code)),
}
}
response.text().await?
if self.login().await? {
let mut response = self.post_priv(url, body).await?;
match response.status() {
StatusCode::OK => Ok(response.text().await?),
StatusCode::UNAUTHORIZED => Err(Error::Unauthorized),
status_code => Err(Error::UnexpectedResponse(status_code)),
}
} else {
let mut response = if self.login().await? {
self.post_priv(url, body).await?
} else {
Err(Error::LoginFailed)?
};
response.text().await?
};
Ok(body)
Err(Error::LoginFailed)
}
}
/// Post something to the server assuming there is a valid login token.