diff options
Diffstat (limited to 'src/micropub.rs')
-rw-r--r-- | src/micropub.rs | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/src/micropub.rs b/src/micropub.rs index b2f1e73..f87feb7 100644 --- a/src/micropub.rs +++ b/src/micropub.rs @@ -1,5 +1,5 @@ +pub use kittybox_util::micropub::{Config, Error as MicropubError, QueryType}; use soup::prelude::*; -pub use kittybox_util::micropub::{Error as MicropubError, Config, QueryType}; #[derive(Debug)] pub struct Client { @@ -19,7 +19,7 @@ pub enum Error { #[error("micropub error: {0}")] Micropub(#[from] MicropubError), #[error("micropub server did not return a location: header")] - NoLocationHeader + NoLocationHeader, } impl Client { @@ -34,18 +34,21 @@ impl Client { pub async fn config(&self) -> Result<Config, Error> { let uri = glib::Uri::parse(&self.micropub, glib::UriFlags::NONE).unwrap(); - let uri = super::util::append_query( - &uri, [("q".to_string(), "config".to_string())] - ); - + let uri = super::util::append_query(&uri, [("q".to_string(), "config".to_string())]); + let exch = soup::Message::from_uri("GET", &uri); let headers = exch.request_headers().expect("SoupMessage with no headers"); // TODO: create a SoupAuth subclass that allows pasting in a token headers.append("Authorization", &format!("Bearer {}", self.access_token)); - let body = self.http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?; + let body = self + .http + .send_and_read_future(&exch, glib::Priority::DEFAULT) + .await?; if exch.status() == soup::Status::Unauthorized { - return Err(MicropubError::from(kittybox_util::micropub::ErrorKind::NotAuthorized).into()) + return Err( + MicropubError::from(kittybox_util::micropub::ErrorKind::NotAuthorized).into(), + ); } Ok(serde_json::from_slice(&body)?) @@ -57,22 +60,32 @@ impl Client { let headers = exch.request_headers().expect("SoupMessage with no headers"); headers.append("Authorization", &format!("Bearer {}", self.access_token)); - exch.set_request_body_from_bytes(Some("application/json"), - Some(&glib::Bytes::from_owned(serde_json::to_vec(&post).unwrap())) + exch.set_request_body_from_bytes( + Some("application/json"), + Some(&glib::Bytes::from_owned(serde_json::to_vec(&post).unwrap())), ); - let body = self.http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?; + let body = self + .http + .send_and_read_future(&exch, glib::Priority::DEFAULT) + .await?; match exch.status() { soup::Status::Created | soup::Status::Accepted => { - let response_headers = exch.response_headers().expect("Successful SoupMessage with no response headers"); - let location = response_headers.one("Location").ok_or(Error::NoLocationHeader)?; + let response_headers = exch + .response_headers() + .expect("Successful SoupMessage with no response headers"); + let location = response_headers + .one("Location") + .ok_or(Error::NoLocationHeader)?; Ok(glib::Uri::parse(&location, glib::UriFlags::NONE)?) - }, - soup::Status::InternalServerError | soup::Status::BadGateway | soup::Status::ServiceUnavailable => { + } + soup::Status::InternalServerError + | soup::Status::BadGateway + | soup::Status::ServiceUnavailable => { todo!("micropub server is down") - }, + } soup::Status::Unauthorized => { Err(MicropubError::from(kittybox_util::micropub::ErrorKind::NotAuthorized).into()) } @@ -80,7 +93,10 @@ impl Client { let error = match serde_json::from_slice::<MicropubError>(&body) { Ok(error) => error, Err(err) => { - tracing::debug!("Error serializing body: {}", String::from_utf8_lossy(&body)); + tracing::debug!( + "Error serializing body: {}", + String::from_utf8_lossy(&body) + ); Err(err)? } }; |