Checking HTTP response codes

This commit is contained in:
daladim 2021-04-19 23:58:23 +02:00
parent 2dbd3726a5
commit 65791806bc
2 changed files with 31 additions and 5 deletions

View file

@ -51,7 +51,7 @@ impl BaseCalendar for RemoteCalendar {
async fn add_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>> { async fn add_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>> {
let ical_text = crate::ical::build_from(&item)?; let ical_text = crate::ical::build_from(&item)?;
let request = reqwest::Client::new() let response = reqwest::Client::new()
.put(item.id().as_url().clone()) .put(item.id().as_url().clone())
.header("If-None-Match", "*") .header("If-None-Match", "*")
.header(CONTENT_TYPE, "text/calendar") .header(CONTENT_TYPE, "text/calendar")
@ -61,7 +61,11 @@ impl BaseCalendar for RemoteCalendar {
.send() .send()
.await?; .await?;
let reply_hdrs = request.headers(); if response.status().is_success() == false {
return Err(format!("Unexpected HTTP status code {:?}", response.status()).into());
}
let reply_hdrs = response.headers();
match reply_hdrs.get("ETag") { match reply_hdrs.get("ETag") {
None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()), None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()),
Some(etag) => { Some(etag) => {
@ -91,6 +95,10 @@ impl BaseCalendar for RemoteCalendar {
.send() .send()
.await?; .await?;
if request.status().is_success() == false {
return Err(format!("Unexpected HTTP status code {:?}", request.status()).into());
}
let reply_hdrs = request.headers(); let reply_hdrs = request.headers();
match reply_hdrs.get("ETag") { match reply_hdrs.get("ETag") {
None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()), None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()),
@ -161,6 +169,10 @@ impl DavCalendar for RemoteCalendar {
.send() .send()
.await?; .await?;
if res.status().is_success() == false {
return Err(format!("Unexpected HTTP status code {:?}", res.status()).into());
}
let text = res.text().await?; let text = res.text().await?;
// This is supposed to be cached // This is supposed to be cached
@ -175,11 +187,16 @@ impl DavCalendar for RemoteCalendar {
} }
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>> { async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>> {
reqwest::Client::new() let del_response = reqwest::Client::new()
.delete(item_id.as_url().clone()) .delete(item_id.as_url().clone())
.basic_auth(self.resource.username(), Some(self.resource.password())) .basic_auth(self.resource.username(), Some(self.resource.password()))
.send() .send()
.await?; .await?;
if del_response.status().is_success() == false {
return Err(format!("Unexpected HTTP status code {:?}", del_response.status()).into());
}
Ok(()) Ok(())
} }
} }

View file

@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use async_trait::async_trait; use async_trait::async_trait;
use reqwest::Method; use reqwest::{Method, StatusCode};
use reqwest::header::CONTENT_TYPE; use reqwest::header::CONTENT_TYPE;
use minidom::Element; use minidom::Element;
use url::Url; use url::Url;
@ -63,6 +63,10 @@ pub async fn sub_request(resource: &Resource, method: &str, body: String, depth:
.send() .send()
.await?; .await?;
if res.status().is_success() == false {
return Err(format!("Unexpected HTTP status code {:?}", res.status()).into());
}
let text = res.text().await?; let text = res.text().await?;
Ok(text) Ok(text)
} }
@ -252,7 +256,7 @@ impl CalDavSource<RemoteCalendar> for Client {
let creation_body = calendar_body(name, supported_components); let creation_body = calendar_body(name, supported_components);
reqwest::Client::new() let response = reqwest::Client::new()
.request(Method::from_bytes(b"MKCALENDAR").unwrap(), id.clone()) .request(Method::from_bytes(b"MKCALENDAR").unwrap(), id.clone())
.header(CONTENT_TYPE, "application/xml") .header(CONTENT_TYPE, "application/xml")
.basic_auth(self.resource.username(), Some(self.resource.password())) .basic_auth(self.resource.username(), Some(self.resource.password()))
@ -260,6 +264,11 @@ impl CalDavSource<RemoteCalendar> for Client {
.send() .send()
.await?; .await?;
let status = response.status();
if status != StatusCode::CREATED {
return Err(format!("Unexpected HTTP status code. Expected CREATED, got {}", status.as_u16()).into());
}
self.get_calendar(&id).await.ok_or(format!("Unable to insert calendar {:?}", id).into()) self.get_calendar(&id).await.ok_or(format!("Unable to insert calendar {:?}", id).into())
} }
} }