-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add context in Load and Build (#487)
Signed-off-by: Xuanwo <[email protected]>
- Loading branch information
Showing
6 changed files
with
52 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::{FileRead, HttpSend}; | ||
use anyhow::Result; | ||
use bytes::Bytes; | ||
use std::sync::Arc; | ||
|
||
/// Context provides the context for the request signing. | ||
#[derive(Debug, Clone)] | ||
pub struct Context { | ||
fs: Arc<dyn FileRead>, | ||
http: Arc<dyn HttpSend>, | ||
} | ||
|
||
impl Context { | ||
/// Create a new context. | ||
pub fn new(fs: impl FileRead, http: impl HttpSend) -> Self { | ||
Self { | ||
fs: Arc::new(fs), | ||
http: Arc::new(http), | ||
} | ||
} | ||
|
||
/// Read the file content entirely in `Vec<u8>`. | ||
#[inline] | ||
pub async fn file_read(&self, path: &str) -> Result<Vec<u8>> { | ||
self.fs.file_read(path).await | ||
} | ||
|
||
/// Send http request and return the response. | ||
#[inline] | ||
pub async fn http_send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>> { | ||
self.http.http_send(req).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use anyhow::Result; | ||
use std::fmt::Debug; | ||
|
||
/// FileRead is used to read the file content entirely in `Vec<u8>`. | ||
/// | ||
/// This could be used by `Load` to load the credential from the file. | ||
#[async_trait::async_trait] | ||
pub trait FileRead { | ||
pub trait FileRead: Debug + 'static { | ||
/// Read the file content entirely in `Vec<u8>`. | ||
async fn file_read(&self, path: &str) -> Result<Vec<u8>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
use anyhow::Result; | ||
use bytes::Bytes; | ||
use std::fmt::Debug; | ||
|
||
/// HttpSend is used to send http request during the signing process. | ||
/// | ||
/// For example, fetch IMDS token from AWS or OAuth2 refresh token. This trait is designed | ||
/// especially for the signer, please don't use it as a general http client. | ||
#[async_trait::async_trait] | ||
pub trait HttpSend { | ||
pub trait HttpSend: Debug + 'static { | ||
/// Send http request and return the response. | ||
async fn http_send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ mod fs; | |
pub use fs::FileRead; | ||
mod http; | ||
pub use http::HttpSend; | ||
mod context; | ||
pub use context::Context; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters