Struct FWClientBuilder

Source
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

Source

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());;
Source

pub fn client_name(self, name: String) -> Self

Set the client name. Used in “User-Agent” header.

Source

pub fn client_version(self, version: String) -> Self

Set client version. Used in “User-Agent” header.

Source

pub fn tracing(self, tracing: bool) -> Self

Sets whether the client should use tracing middleware, defaults to true.

Source

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");
Source

pub fn with<M>(self, middleware: M) -> Self
where M: Middleware,

Convenience method to attach middleware.

If you need to keep a reference to the middleware after attaching, use with_arc.

Source

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.

Source

pub fn with_init<I>(self, initialiser: I) -> Self

Convenience method to attach a request initialiser.

If you need to keep a reference to the initialiser after attaching, use with_arc_init.

Source

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.

Source

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.

Source

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

Source§

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 string
  • client_name: None
  • client_version: None
  • timeout: 60 seconds
  • connect_timeout: 10 seconds
  • read_timeout: 60 seconds
Source§

impl<T: AsRef<str>> From<T> for FWClientBuilder

Source§

fn from(api_key: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,