mirror of
https://github.com/astro/buzzrelay.git
synced 2024-11-27 14:21:01 +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 http::StatusCode;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
|
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
|
||||||
use crate::digest;
|
use crate::{digest, error::Error};
|
||||||
use crate::send::SendError;
|
|
||||||
|
|
||||||
pub async fn fetch<T>(client: &reqwest::Client, url: &str) -> Result<T, reqwest::Error>
|
pub async fn fetch<T>(client: &reqwest::Client, url: &str) -> Result<T, reqwest::Error>
|
||||||
where
|
where
|
||||||
|
@ -21,13 +20,13 @@ pub async fn authorized_fetch<T>(
|
||||||
uri: &str,
|
uri: &str,
|
||||||
key_id: &str,
|
key_id: &str,
|
||||||
private_key: &PrivateKey,
|
private_key: &PrivateKey,
|
||||||
) -> Result<T, SendError>
|
) -> Result<T, Error>
|
||||||
where
|
where
|
||||||
T: DeserializeOwned,
|
T: DeserializeOwned,
|
||||||
{
|
{
|
||||||
let url = reqwest::Url::parse(uri)
|
let url = reqwest::Url::parse(uri)
|
||||||
.map_err(|_| SendError::InvalidUri)?;
|
.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(&[])
|
let digest_header = digest::generate_header(&[])
|
||||||
.expect("digest::generate_header");
|
.expect("digest::generate_header");
|
||||||
let mut req = http::Request::builder()
|
let mut req = http::Request::builder()
|
||||||
|
@ -47,6 +46,6 @@ where
|
||||||
if res.status() >= StatusCode::OK && res.status() < StatusCode::MULTIPLE_CHOICES {
|
if res.status() >= StatusCode::OK && res.status() < StatusCode::MULTIPLE_CHOICES {
|
||||||
Ok(res.json().await?)
|
Ok(res.json().await?)
|
||||||
} else {
|
} 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 std::{panic, process};
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
|
mod error;
|
||||||
mod config;
|
mod config;
|
||||||
mod actor;
|
mod actor;
|
||||||
mod db;
|
mod db;
|
||||||
|
|
34
src/send.rs
34
src/send.rs
|
@ -3,28 +3,10 @@ use std::{
|
||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use http_digest_headers::{DigestHeader, DigestMethod};
|
|
||||||
use metrics::histogram;
|
use metrics::histogram;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
|
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
|
||||||
|
use crate::{digest, error::Error};
|
||||||
#[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),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn send<T: Serialize>(
|
pub async fn send<T: Serialize>(
|
||||||
client: &reqwest::Client,
|
client: &reqwest::Client,
|
||||||
|
@ -32,10 +14,10 @@ pub async fn send<T: Serialize>(
|
||||||
key_id: &str,
|
key_id: &str,
|
||||||
private_key: &PrivateKey,
|
private_key: &PrivateKey,
|
||||||
body: &T,
|
body: &T,
|
||||||
) -> Result<(), SendError> {
|
) -> Result<(), Error> {
|
||||||
let body = Arc::new(
|
let body = Arc::new(
|
||||||
serde_json::to_vec(body)
|
serde_json::to_vec(body)
|
||||||
.map_err(SendError::Json)?
|
.map_err(Error::Json)?
|
||||||
);
|
);
|
||||||
send_raw(client, uri, key_id, private_key, body).await
|
send_raw(client, uri, key_id, private_key, body).await
|
||||||
}
|
}
|
||||||
|
@ -46,12 +28,12 @@ pub async fn send_raw(
|
||||||
key_id: &str,
|
key_id: &str,
|
||||||
private_key: &PrivateKey,
|
private_key: &PrivateKey,
|
||||||
body: Arc<Vec<u8>>,
|
body: Arc<Vec<u8>>,
|
||||||
) -> Result<(), SendError> {
|
) -> Result<(), Error> {
|
||||||
let url = reqwest::Url::parse(uri)
|
let url = reqwest::Url::parse(uri)
|
||||||
.map_err(|_| Error::InvalidUri)?;
|
.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)
|
let digest_header = digest::generate_header(&body)
|
||||||
.map_err(|()| SendError::Digest)?;
|
.map_err(|()| Error::Digest)?;
|
||||||
let mut req = http::Request::builder()
|
let mut req = http::Request::builder()
|
||||||
.method("POST")
|
.method("POST")
|
||||||
.uri(uri)
|
.uri(uri)
|
||||||
|
@ -61,7 +43,7 @@ pub async fn send_raw(
|
||||||
.replace("+0000", "GMT"))
|
.replace("+0000", "GMT"))
|
||||||
.header("digest", digest_header)
|
.header("digest", digest_header)
|
||||||
.body(body.as_ref().clone())
|
.body(body.as_ref().clone())
|
||||||
.map_err(SendError::HttpReq)?;
|
.map_err(Error::HttpReq)?;
|
||||||
let t1 = Instant::now();
|
let t1 = Instant::now();
|
||||||
SigningConfig::new(RsaSha256, private_key, key_id)
|
SigningConfig::new(RsaSha256, private_key, key_id)
|
||||||
.sign(&mut req)?;
|
.sign(&mut req)?;
|
||||||
|
@ -79,6 +61,6 @@ pub async fn send_raw(
|
||||||
tracing::error!("send_raw {} response HTTP {}", url, res.status());
|
tracing::error!("send_raw {} response HTTP {}", url, res.status());
|
||||||
let response = res.text().await?;
|
let response = res.text().await?;
|
||||||
tracing::error!("send_raw {} response body: {:?}", url, response);
|
tracing::error!("send_raw {} response body: {:?}", url, response);
|
||||||
Err(SendError::Response(response))
|
Err(Error::Response(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue