mirror of
https://github.com/astro/buzzrelay.git
synced 2024-11-23 20:40:59 +00:00
send: move SendError to error::Error
This commit is contained in:
parent
e8b2b807b2
commit
f782197b93
4 changed files with 31 additions and 32 deletions
17
src/error.rs
Normal file
17
src/error.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("HTTP Digest generation error")]
|
||||
Digest,
|
||||
#[error("JSON encoding error")]
|
||||
Json(#[from] serde_json::Error),
|
||||
#[error("Signature error")]
|
||||
Signature(#[from] sigh::Error),
|
||||
#[error("HTTP request error")]
|
||||
HttpReq(#[from] http::Error),
|
||||
#[error("HTTP client error")]
|
||||
Http(#[from] reqwest::Error),
|
||||
#[error("Invalid URI")]
|
||||
InvalidUri,
|
||||
#[error("Error response from remote")]
|
||||
Response(String),
|
||||
}
|
11
src/fetch.rs
11
src/fetch.rs
|
@ -1,8 +1,7 @@
|
|||
use http::StatusCode;
|
||||
use serde::de::DeserializeOwned;
|
||||
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
|
||||
use crate::digest;
|
||||
use crate::send::SendError;
|
||||
use crate::{digest, error::Error};
|
||||
|
||||
pub async fn fetch<T>(client: &reqwest::Client, url: &str) -> Result<T, reqwest::Error>
|
||||
where
|
||||
|
@ -21,13 +20,13 @@ pub async fn authorized_fetch<T>(
|
|||
uri: &str,
|
||||
key_id: &str,
|
||||
private_key: &PrivateKey,
|
||||
) -> Result<T, SendError>
|
||||
) -> Result<T, Error>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let url = reqwest::Url::parse(uri)
|
||||
.map_err(|_| SendError::InvalidUri)?;
|
||||
let host = format!("{}", url.host().ok_or(SendError::InvalidUri)?);
|
||||
.map_err(|_| Error::InvalidUri)?;
|
||||
let host = format!("{}", url.host().ok_or(Error::InvalidUri)?);
|
||||
let digest_header = digest::generate_header(&[])
|
||||
.expect("digest::generate_header");
|
||||
let mut req = http::Request::builder()
|
||||
|
@ -47,6 +46,6 @@ where
|
|||
if res.status() >= StatusCode::OK && res.status() < StatusCode::MULTIPLE_CHOICES {
|
||||
Ok(res.json().await?)
|
||||
} else {
|
||||
Err(SendError::Response(format!("{}", res.text().await?)))
|
||||
Err(Error::Response(format!("{}", res.text().await?)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ use std::{net::SocketAddr, sync::Arc, time::Duration, collections::HashMap};
|
|||
use std::{panic, process};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod error;
|
||||
mod config;
|
||||
mod actor;
|
||||
mod db;
|
||||
|
|
34
src/send.rs
34
src/send.rs
|
@ -3,28 +3,10 @@ use std::{
|
|||
time::Instant,
|
||||
};
|
||||
use http::StatusCode;
|
||||
use http_digest_headers::{DigestHeader, DigestMethod};
|
||||
use metrics::histogram;
|
||||
use serde::Serialize;
|
||||
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SendError {
|
||||
#[error("HTTP Digest generation error")]
|
||||
Digest,
|
||||
#[error("JSON encoding error")]
|
||||
Json(#[from] serde_json::Error),
|
||||
#[error("Signature error")]
|
||||
Signature(#[from] sigh::Error),
|
||||
#[error("HTTP request error")]
|
||||
HttpReq(#[from] http::Error),
|
||||
#[error("HTTP client error")]
|
||||
Http(#[from] reqwest::Error),
|
||||
#[error("Invalid URI")]
|
||||
InvalidUri,
|
||||
#[error("Error response from remote")]
|
||||
Response(String),
|
||||
}
|
||||
use crate::{digest, error::Error};
|
||||
|
||||
pub async fn send<T: Serialize>(
|
||||
client: &reqwest::Client,
|
||||
|
@ -32,10 +14,10 @@ pub async fn send<T: Serialize>(
|
|||
key_id: &str,
|
||||
private_key: &PrivateKey,
|
||||
body: &T,
|
||||
) -> Result<(), SendError> {
|
||||
) -> Result<(), Error> {
|
||||
let body = Arc::new(
|
||||
serde_json::to_vec(body)
|
||||
.map_err(SendError::Json)?
|
||||
.map_err(Error::Json)?
|
||||
);
|
||||
send_raw(client, uri, key_id, private_key, body).await
|
||||
}
|
||||
|
@ -46,12 +28,12 @@ pub async fn send_raw(
|
|||
key_id: &str,
|
||||
private_key: &PrivateKey,
|
||||
body: Arc<Vec<u8>>,
|
||||
) -> Result<(), SendError> {
|
||||
) -> Result<(), Error> {
|
||||
let url = reqwest::Url::parse(uri)
|
||||
.map_err(|_| Error::InvalidUri)?;
|
||||
let host = format!("{}", url.host().ok_or(SendError::InvalidUri)?);
|
||||
let host = format!("{}", url.host().ok_or(Error::InvalidUri)?);
|
||||
let digest_header = digest::generate_header(&body)
|
||||
.map_err(|()| SendError::Digest)?;
|
||||
.map_err(|()| Error::Digest)?;
|
||||
let mut req = http::Request::builder()
|
||||
.method("POST")
|
||||
.uri(uri)
|
||||
|
@ -61,7 +43,7 @@ pub async fn send_raw(
|
|||
.replace("+0000", "GMT"))
|
||||
.header("digest", digest_header)
|
||||
.body(body.as_ref().clone())
|
||||
.map_err(SendError::HttpReq)?;
|
||||
.map_err(Error::HttpReq)?;
|
||||
let t1 = Instant::now();
|
||||
SigningConfig::new(RsaSha256, private_key, key_id)
|
||||
.sign(&mut req)?;
|
||||
|
@ -79,6 +61,6 @@ pub async fn send_raw(
|
|||
tracing::error!("send_raw {} response HTTP {}", url, res.status());
|
||||
let response = res.text().await?;
|
||||
tracing::error!("send_raw {} response body: {:?}", url, response);
|
||||
Err(SendError::Response(response))
|
||||
Err(Error::Response(response))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue