client: prepare to get rid of Error trait
First rename it so it's clear what "Error" refers to in the following patches. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
0c45d51406
commit
e0b102d932
@ -13,13 +13,13 @@ use serde_json::Value;
|
|||||||
use proxmox_login::{Login, TicketResult};
|
use proxmox_login::{Login, TicketResult};
|
||||||
|
|
||||||
use crate::auth::AuthenticationKind;
|
use crate::auth::AuthenticationKind;
|
||||||
use crate::{Authentication, Environment, Error, Token};
|
use crate::{Authentication, Environment, ErrorTrait, Token};
|
||||||
|
|
||||||
/// HTTP client backend trait.
|
/// HTTP client backend trait.
|
||||||
///
|
///
|
||||||
/// An async [`Client`] requires some kind of async HTTP client implementation.
|
/// An async [`Client`] requires some kind of async HTTP client implementation.
|
||||||
pub trait HttpClient: Send + Sync {
|
pub trait HttpClient: Send + Sync {
|
||||||
type Error: Error;
|
type Error: ErrorTrait;
|
||||||
type ResponseFuture: Future<Output = Result<Response<Vec<u8>>, Self::Error>>;
|
type ResponseFuture: Future<Output = Result<Response<Vec<u8>>, Self::Error>>;
|
||||||
|
|
||||||
fn request(&self, request: Request<Vec<u8>>) -> Self::ResponseFuture;
|
fn request(&self, request: Request<Vec<u8>>) -> Self::ResponseFuture;
|
||||||
@ -58,7 +58,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_request<E: Error>(request: proxmox_login::Request) -> Result<http::Request<Vec<u8>>, E> {
|
fn to_request<E: ErrorTrait>(request: proxmox_login::Request) -> Result<http::Request<Vec<u8>>, E> {
|
||||||
http::Request::builder()
|
http::Request::builder()
|
||||||
.method(http::Method::POST)
|
.method(http::Method::POST)
|
||||||
.uri(request.url)
|
.uri(request.url)
|
||||||
@ -309,7 +309,7 @@ where
|
|||||||
.set_auth_headers(Request::get(self.build_uri(uri)?))
|
.set_auth_headers(Request::get(self.build_uri(uri)?))
|
||||||
.await?
|
.await?
|
||||||
.body(Vec::new())
|
.body(Vec::new())
|
||||||
.map_err(Error::internal)?;
|
.map_err(E::Error::internal)?;
|
||||||
|
|
||||||
Self::handle_response(self.client.request(request).await?)
|
Self::handle_response(self.client.request(request).await?)
|
||||||
}
|
}
|
||||||
@ -364,7 +364,7 @@ where
|
|||||||
.set_auth_headers(Request::delete(self.build_uri(uri)?))
|
.set_auth_headers(Request::delete(self.build_uri(uri)?))
|
||||||
.await?
|
.await?
|
||||||
.body(Vec::new())
|
.body(Vec::new())
|
||||||
.map_err(Error::internal)?;
|
.map_err(E::Error::internal)?;
|
||||||
|
|
||||||
Self::handle_response(self.client.request(request).await?)
|
Self::handle_response(self.client.request(request).await?)
|
||||||
}
|
}
|
||||||
@ -437,7 +437,7 @@ where
|
|||||||
let request = auth
|
let request = auth
|
||||||
.set_auth_headers(request)
|
.set_auth_headers(request)
|
||||||
.body(body.clone())
|
.body(body.clone())
|
||||||
.map_err(Error::internal)?;
|
.map_err(E::Error::internal)?;
|
||||||
|
|
||||||
Ok(self.client.request(request).await?)
|
Ok(self.client.request(request).await?)
|
||||||
}
|
}
|
||||||
@ -459,12 +459,12 @@ where
|
|||||||
// Ok(value) =>
|
// Ok(value) =>
|
||||||
// if value["error"]
|
// if value["error"]
|
||||||
let (response, body) = response.into_parts();
|
let (response, body) = response.into_parts();
|
||||||
let body = String::from_utf8(body).map_err(Error::bad_api)?;
|
let body = String::from_utf8(body).map_err(E::Error::bad_api)?;
|
||||||
return Err(E::Error::api_error(response.status, body));
|
return Err(E::Error::api_error(response.status, body));
|
||||||
}
|
}
|
||||||
|
|
||||||
let data: RawApiResponse<R> =
|
let data: RawApiResponse<R> =
|
||||||
serde_json::from_slice(&response.into_body()).map_err(Error::bad_api)?;
|
serde_json::from_slice(&response.into_body()).map_err(E::Error::bad_api)?;
|
||||||
|
|
||||||
data.check()
|
data.check()
|
||||||
}
|
}
|
||||||
@ -528,7 +528,7 @@ struct RawApiResponse<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> RawApiResponse<T> {
|
impl<T> RawApiResponse<T> {
|
||||||
pub fn check<E: Error>(mut self) -> Result<ApiResponse<T>, E> {
|
pub fn check<E: ErrorTrait>(mut self) -> Result<ApiResponse<T>, E> {
|
||||||
if !self.success.unwrap_or(false) {
|
if !self.success.unwrap_or(false) {
|
||||||
let status = http::StatusCode::from_u16(self.status.unwrap_or(400))
|
let status = http::StatusCode::from_u16(self.status.unwrap_or(400))
|
||||||
.unwrap_or(http::StatusCode::BAD_REQUEST);
|
.unwrap_or(http::StatusCode::BAD_REQUEST);
|
||||||
|
@ -6,12 +6,12 @@ use http::Uri;
|
|||||||
|
|
||||||
use proxmox_login::tfa::TfaChallenge;
|
use proxmox_login::tfa::TfaChallenge;
|
||||||
|
|
||||||
use crate::Error;
|
use crate::ErrorTrait;
|
||||||
|
|
||||||
/// Provide input from the environment for storing/loading tickets or tokens and querying the user
|
/// Provide input from the environment for storing/loading tickets or tokens and querying the user
|
||||||
/// for passwords or 2nd factors.
|
/// for passwords or 2nd factors.
|
||||||
pub trait Environment: Send + Sync {
|
pub trait Environment: Send + Sync {
|
||||||
type Error: Error;
|
type Error: ErrorTrait;
|
||||||
|
|
||||||
/// Store a ticket belonging to a user of an API.
|
/// Store a ticket belonging to a user of an API.
|
||||||
///
|
///
|
||||||
|
@ -2,7 +2,7 @@ use std::any::Any;
|
|||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
/// For error types provided by the user of this crate.
|
/// For error types provided by the user of this crate.
|
||||||
pub trait Error: Sized + Display + fmt::Debug + Any + Send + Sync + 'static {
|
pub trait ErrorTrait: Sized + Display + fmt::Debug + Any + Send + Sync + 'static {
|
||||||
/// An arbitrary error message.
|
/// An arbitrary error message.
|
||||||
fn custom<T: Display>(msg: T) -> Self;
|
fn custom<T: Display>(msg: T) -> Self;
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ pub trait Error: Sized + Display + fmt::Debug + Any + Send + Sync + 'static {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for anyhow::Error {
|
impl ErrorTrait for anyhow::Error {
|
||||||
fn custom<T: Display>(msg: T) -> Self {
|
fn custom<T: Display>(msg: T) -> Self {
|
||||||
anyhow::format_err!("{msg}")
|
anyhow::format_err!("{msg}")
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ mod environment;
|
|||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
pub use environment::Environment;
|
pub use environment::Environment;
|
||||||
pub use error::Error;
|
pub use error::ErrorTrait;
|
||||||
|
|
||||||
pub use proxmox_login::tfa::TfaChallenge;
|
pub use proxmox_login::tfa::TfaChallenge;
|
||||||
pub use proxmox_login::{Authentication, Ticket};
|
pub use proxmox_login::{Authentication, Ticket};
|
||||||
|
Loading…
Reference in New Issue
Block a user