pub struct SmsRouter { /* private fields */ }Expand description
Routes SMS sends to a named provider without requiring the caller to know about individual provider crate types.
This is the unified dispatch client that eliminates boilerplate in
consumer code. Instead of matching on a provider enum and constructing
the right client, register each provider once and then call
send_via with a name.
SmsRouter also implements SmsClient itself, forwarding to a
configured default provider.
§Example
use sms_core::{SmsRouter, SendRequest};
let router = SmsRouter::new()
.with("plivo", plivo_client)
.with("aws-sns", sns_client)
.default_provider("plivo");
// Explicit dispatch:
router.send_via("aws-sns", SendRequest { .. }).await?;
// Or use the SmsClient impl (goes to the default):
router.send(SendRequest { .. }).await?;Implementations§
Source§impl SmsRouter
impl SmsRouter
Sourcepub fn with(
self,
name: impl Into<String>,
client: impl SmsClient + 'static,
) -> Self
pub fn with( self, name: impl Into<String>, client: impl SmsClient + 'static, ) -> Self
Register a provider under the given name.
If this is the first provider added it automatically becomes the
default (override with default_provider).
Sourcepub fn with_arc(
self,
name: impl Into<String>,
client: Arc<dyn SmsClient>,
) -> Self
pub fn with_arc( self, name: impl Into<String>, client: Arc<dyn SmsClient>, ) -> Self
Register a provider that is already behind an Arc.
Sourcepub fn default_provider(self, name: impl Into<String>) -> Self
pub fn default_provider(self, name: impl Into<String>) -> Self
Set which provider name is used when calling the SmsClient trait
impl directly (i.e. router.send(..)).
Sourcepub async fn send_via(
&self,
provider: &str,
req: SendRequest<'_>,
) -> Result<SendResponse, SmsError>
pub async fn send_via( &self, provider: &str, req: SendRequest<'_>, ) -> Result<SendResponse, SmsError>
Send a message through a specific named provider.
Sourcepub fn has_provider(&self, name: &str) -> bool
pub fn has_provider(&self, name: &str) -> bool
Returns true if a provider with the given name is registered.
Sourcepub fn default_provider_name(&self) -> Option<&str>
pub fn default_provider_name(&self) -> Option<&str>
Returns the name of the current default provider, if any.
Trait Implementations§
Source§impl SmsClient for SmsRouter
impl SmsClient for SmsRouter
Source§fn send<'life0, 'life1, 'async_trait>(
&'life0 self,
req: SendRequest<'life1>,
) -> Pin<Box<dyn Future<Output = Result<SendResponse, SmsError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn send<'life0, 'life1, 'async_trait>(
&'life0 self,
req: SendRequest<'life1>,
) -> Pin<Box<dyn Future<Output = Result<SendResponse, SmsError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Send through the default provider.
Returns SmsError::Invalid if no default has been set.