pub struct FWClientBuilder { /* private fields */ }
Expand description
Builder for the FWClient
.
This builder allows you to configure the client with an API key, client name, client version.
For further control you can also add custom middleware and request initializers
using the FWClientBuilder::with
and FWClientBuilder::with_init
methods.
For more information on how to write a Middleware or Request Initialiser, see the
reqwest_middleware::Middleware
and reqwest_middleware::RequestInitialiser
traits.
Example:
use fw_client::{FWClientBuilder, FWClient};
let client = FWClientBuilder::from("scitran-user fw_instance:test_api_key")
.client_name("MyClient".to_string())
.client_version("1.0.0".to_string())
.build();
Implementations§
Source§impl FWClientBuilder
impl FWClientBuilder
Sourcepub fn new(api_key: ApiKey) -> Self
pub fn new(api_key: ApiKey) -> Self
Create a new FWClientBuilder
with the provided API key.
See the ApiKey
documentation for the expected format.
You can also use the From
trait to create a builder from a string:
use fw_client::FWClientBuilder;
let builder = FWClientBuilder::from("scitran-user fw_instance:test_api_key");
// or
let builder = FWClientBuilder::new("scitran-user fw_instance:test_api_key".parse().unwrap());;
Sourcepub fn client_name(self, name: String) -> Self
pub fn client_name(self, name: String) -> Self
Set the client name. Used in “User-Agent” header.
Sourcepub fn client_version(self, version: String) -> Self
pub fn client_version(self, version: String) -> Self
Set client version. Used in “User-Agent” header.
Sourcepub fn tracing(self, tracing: bool) -> Self
pub fn tracing(self, tracing: bool) -> Self
Sets whether the client should use tracing middleware, defaults to true
.
Sourcepub fn with_retry<T: RetryPolicy + Send + Sync + 'static, R: RetryableStrategy + Send + Sync + 'static>(
self,
retry: Option<RetryTransientMiddleware<T, R>>,
) -> Self
pub fn with_retry<T: RetryPolicy + Send + Sync + 'static, R: RetryableStrategy + Send + Sync + 'static>( self, retry: Option<RetryTransientMiddleware<T, R>>, ) -> Self
Set the retry policy and strategy for the client,
By default an exponential backoff policy with 3 retries is used.
For example you can create a simple retry policy that retries 5 times with 1 second delay between retries:
use reqwest_retry::{RetryPolicy, RetryTransientMiddleware, RetryDecision, RetryableStrategy};
use fw_client::{FWClientBuilder, FWClient};
use std::time::{Duration, SystemTime};
struct SimpleRetryPolicy(u32);
impl RetryPolicy for SimpleRetryPolicy {
fn should_retry(&self, _: SystemTime, attempt: u32) -> RetryDecision {
if attempt >= self.0 {
RetryDecision::DoNotRetry
} else {
RetryDecision::Retry{
execute_after: SystemTime::now() + Duration::from_secs(1)
}
}
}
}
let client = FWClientBuilder::from("fw_instance:test_api_key")
.with_retry(Some(RetryTransientMiddleware::new_with_policy(
SimpleRetryPolicy(3)
)))
.build()
.expect("Failed to create FWClient");
Sourcepub fn with<M>(self, middleware: M) -> Selfwhere
M: Middleware,
pub fn with<M>(self, middleware: M) -> Selfwhere
M: Middleware,
Convenience method to attach middleware.
If you need to keep a reference to the middleware after attaching, use with_arc
.
Sourcepub fn with_arc(self, middleware: Arc<dyn Middleware>) -> Self
pub fn with_arc(self, middleware: Arc<dyn Middleware>) -> Self
Add middleware to the chain. with
is more ergonomic if you don’t need the Arc
.
Sourcepub fn with_init<I>(self, initialiser: I) -> Selfwhere
I: RequestInitialiser,
pub fn with_init<I>(self, initialiser: I) -> Selfwhere
I: RequestInitialiser,
Convenience method to attach a request initialiser.
If you need to keep a reference to the initialiser after attaching, use with_arc_init
.
Sourcepub fn with_arc_init(self, initialiser: Arc<dyn RequestInitialiser>) -> Self
pub fn with_arc_init(self, initialiser: Arc<dyn RequestInitialiser>) -> Self
Add a request initialiser to the chain. with_init
is more ergonomic if you don’t need the Arc
.
Sourcepub fn build(self) -> Result<FWClient, FWClientError>
pub fn build(self) -> Result<FWClient, FWClientError>
Build the FWClient
with the provided configuration
Uses default configuration for the underlying reqwest
client.
- Timeouts: 60 seconds for read and write, 10 seconds for connect
- Redirect policy: limited to 10 redirects
If you need to customise the reqwest::Client
, use FWClientBuilder::build_with_client
.
Sourcepub fn build_with_client(
self,
client: Client,
) -> Result<FWClient, FWClientError>
pub fn build_with_client( self, client: Client, ) -> Result<FWClient, FWClientError>
Build the FWClient
with the provided reqwest::Client
.
use reqwest::Client;
use fw_client::FWClientBuilder;
use reqwest::Certificate;
let client = reqwest::Client::builder()
// Override default timeouts
.timeout(std::time::Duration::from_secs(120))
.connect_timeout(std::time::Duration::from_secs(30))
// Add custom root certificate
.add_root_certificate(Certificate::from_pem(include_bytes!("path/to/cert.pem")).unwrap())
.build()
.unwrap();
let fw_client = FWClientBuilder::new("scitran-user fw_instance:test_api_key".parse().unwrap())
.build_with_client(client)
.unwrap();
Trait Implementations§
Source§impl Default for FWClientBuilder
impl Default for FWClientBuilder
Source§fn default() -> Self
fn default() -> Self
Create a new FWClientBuilder
with default values.
This also includes a default retry policy with an exponential backoff strategy and a tracing middleware.
The default values are:
api_key
: Empty stringclient_name
: Noneclient_version
: Nonetimeout
: 60 secondsconnect_timeout
: 10 secondsread_timeout
: 60 seconds