pub struct Context { /* private fields */ }Expand description
Context provides the context for the request signing.
§Important
reqsign provides NO default implementations. Users MAY configure components they need. Any unconfigured component will use a no-op implementation that returns errors or empty values when called.
§Example
use reqsign_core::{Context, OsEnv};
// Create a context with explicit implementations
let ctx = Context::new()
.with_env(OsEnv); // Optionally configure environment implementationImplementations§
Source§impl Context
impl Context
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new Context with no-op implementations.
All components use no-op implementations by default.
Use the with_* methods to configure the components you need.
use reqsign_core::Context;
let ctx = Context::new();
// All components use no-op implementations by default
// You can configure specific components as needed:
// ctx.with_file_read(my_file_reader)
// .with_http_send(my_http_client)
// .with_env(my_env_provider);Sourcepub fn with_file_read(self, fs: impl FileRead) -> Self
pub fn with_file_read(self, fs: impl FileRead) -> Self
Replace the file reader implementation.
Sourcepub fn with_http_send(self, http: impl HttpSend) -> Self
pub fn with_http_send(self, http: impl HttpSend) -> Self
Replace the HTTP client implementation.
Sourcepub fn with_command_execute(self, cmd: impl CommandExecute) -> Self
pub fn with_command_execute(self, cmd: impl CommandExecute) -> Self
Replace the command executor implementation.
Sourcepub async fn file_read(&self, path: &str) -> Result<Vec<u8>>
pub async fn file_read(&self, path: &str) -> Result<Vec<u8>>
Read the file content entirely in Vec<u8>.
Sourcepub async fn file_read_as_string(&self, path: &str) -> Result<String>
pub async fn file_read_as_string(&self, path: &str) -> Result<String>
Read the file content entirely in String.
Sourcepub async fn http_send(&self, req: Request<Bytes>) -> Result<Response<Bytes>>
pub async fn http_send(&self, req: Request<Bytes>) -> Result<Response<Bytes>>
Send http request and return the response.
Sourcepub async fn http_send_as_string(
&self,
req: Request<Bytes>,
) -> Result<Response<String>>
pub async fn http_send_as_string( &self, req: Request<Bytes>, ) -> Result<Response<String>>
Send http request and return the response as string.
Sourcepub fn expand_home_dir(&self, path: &str) -> Option<String>
pub fn expand_home_dir(&self, path: &str) -> Option<String>
Expand ~ in input path.
- If path not starts with
~/or~\\, returnsSome(path)directly. - Otherwise, replace
~with home dir instead. - If home_dir is not found, returns
None.
Sourcepub fn env_var(&self, key: &str) -> Option<String>
pub fn env_var(&self, key: &str) -> Option<String>
Get the environment variable.
- Returns
Some(v)if the environment variable is found and is valid utf-8. - Returns
Noneif the environment variable is not found or value is invalid.
Sourcepub fn env_vars(&self) -> HashMap<String, String>
pub fn env_vars(&self) -> HashMap<String, String>
Returns a hashmap of (variable, value) pairs of strings, for all the environment variables of the current process.
Sourcepub async fn command_execute(
&self,
program: &str,
args: &[&str],
) -> Result<CommandOutput>
pub async fn command_execute( &self, program: &str, args: &[&str], ) -> Result<CommandOutput>
Execute an external command with the given program and arguments.
Returns the command output including exit status, stdout, and stderr.