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