speakeasy_rust_sdk/
sdk.rs

1use crate::{
2    transport::{GrpcClient, Transport},
3    Config, Error, Masking, RequestConfig, SpeakeasySdk,
4};
5
6/// Speakeasy SDK instance
7#[doc(hidden)]
8#[derive(Debug, Clone)]
9pub struct GenericSpeakeasySdk<GrpcClient> {
10    pub(crate) masking: Masking,
11    pub(crate) config: RequestConfig,
12    pub(crate) transport: GrpcClient,
13}
14
15impl<T: Transport + Send + Clone + 'static> GenericSpeakeasySdk<T> {
16    pub fn new_with_transport(config: Config, transport: T) -> Self {
17        let config = RequestConfig::from(config);
18        let masking = Masking::default();
19
20        Self {
21            masking,
22            config,
23            transport,
24        }
25    }
26
27    #[cfg(feature = "custom_transport")]
28    pub fn into_sdk(self) -> crate::SpeakeasySdk<T> {
29        crate::SpeakeasySdk::CustomTransport(self)
30    }
31}
32
33#[cfg(feature = "mock")]
34impl GenericSpeakeasySdk<crate::transport::mock::GrpcMock> {
35    pub fn into_sdk(self) -> crate::SpeakeasySdk {
36        crate::SpeakeasySdk::Mock(Box::new(self))
37    }
38}
39
40impl GenericSpeakeasySdk<GrpcClient> {
41    /// Create a new Speakeasy SDK instance
42    ///
43    /// # Examples:
44    /// ```rust
45    /// use speakeasy_rust_sdk::{SpeakeasySdk, Config, masking::StringMaskingOption};
46    ///
47    /// let config = Config{
48    ///     api_key: "YOUR API KEY HERE".to_string(),       // retrieve from Speakeasy API dashboard.
49    ///     api_id: "YOUR API ID HERE".to_string(),         // enter a name that you'd like to associate captured requests with.
50    ///     // This name will show up in the Speakeasy dashboard. e.g. "PetStore" might be a good ApiID for a Pet Store's API.
51    ///     // No spaces allowed.
52    ///     version_id: "YOUR VERSION ID HERE".to_string(), // enter a version that you would like to associate captured requests with.
53    ///     // The combination of ApiID (name) and VersionID will uniquely identify your requests in the Speakeasy Dashboard.
54    ///     // e.g. "v1.0.0". You can have multiple versions for the same ApiID (if running multiple versions of your API)
55    /// };
56    ///
57    /// // Create a new Speakeasy SDK instance
58    /// let mut sdk = SpeakeasySdk::try_new(config).expect("valid API key");
59    ///
60    /// // Configure masking for query
61    /// // see [Masking::with_query_string_mask] for more options
62    /// sdk.masking().with_query_string_mask("secret", "********");
63    /// sdk.masking().with_query_string_mask("password", StringMaskingOption::default());
64    ///
65    /// // Configure other masks
66    /// // see [Masking] for more options
67    /// ```
68    pub fn try_new(config: Config) -> Result<Self, Error> {
69        Ok(Self {
70            transport: GrpcClient::new(config.api_key.clone())?,
71            config: config.into(),
72            masking: Default::default(),
73        })
74    }
75}
76
77impl SpeakeasySdk {
78    pub fn try_new(config: Config) -> Result<Self, Error> {
79        let generic_sdk = GenericSpeakeasySdk::try_new(config)?;
80        Ok(Self::Grpc(generic_sdk))
81    }
82}