Skip to content

Commit

Permalink
Only have core apis in doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c12i committed Nov 20, 2023
1 parent e7eeb89 commit fedf9cd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
8 changes: 0 additions & 8 deletions docs/client/auth.md

This file was deleted.

19 changes: 0 additions & 19 deletions docs/client/new.md

This file was deleted.

17 changes: 0 additions & 17 deletions docs/client/set_initiator_password.md

This file was deleted.

58 changes: 55 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,28 @@ pub struct Mpesa<Env: ApiEnvironment> {
}

impl<'mpesa, Env: ApiEnvironment> Mpesa<Env> {
#[doc = include_str!("../docs/client/new.md")]
/// Constructs a new `Mpesa` client.
///
/// # Example
///
/// ```rust
/// use mpesa::{Mpesa, Environment};
///
/// #[tokio::main]
/// async fn main() {
/// dotenv::dotenv().ok();
///
/// let client = Mpesa::new(
/// env!("CLIENT_KEY"),
/// env!("CLIENT_SECRET"),
/// Environment::Sandbox,
/// );
///
/// assert!(client.is_connected().await);
/// }
/// ```
/// # Panics
/// This method can panic if a TLS backend cannot be initialized for the internal http_client
pub fn new<S: Into<String>>(client_key: S, client_secret: S, environment: Env) -> Self {
let http_client = HttpClient::builder()
.connect_timeout(std::time::Duration::from_millis(10_000))
Expand Down Expand Up @@ -70,7 +91,31 @@ impl<'mpesa, Env: ApiEnvironment> Mpesa<Env> {
self.client_secret.expose_secret()
}

#[doc = include_str!("../docs/client/set_initiator_password.md")]
/// Optional in development but required for production for the following apis:
/// - `account_balance`
/// - `b2b`
/// - `b2c`
/// - `transaction_reversal`
/// - `transaction_status`
///
/// You will need to call this method and set your production initiator password.
/// If in development, a default initiator password from the test credentials is already pre-set
///
/// # Example
/// ```rust
/// use mpesa::{Mpesa, Environment};
///
/// fn main() {
/// dotenv::dotenv().ok();
///
/// let client = Mpesa::new(
/// env!("CLIENT_KEY"),
/// env!("CLIENT_SECRET"),
/// Environment::Sandbox,
/// );
/// client.set_initiator_password("your_initiator_password");
/// }

Check failure on line 117 in src/client.rs

View workflow job for this annotation

GitHub Actions / clippy

needless `fn main` in doctest

error: needless `fn main` in doctest --> src/client.rs:106:9 | 106 | /// use mpesa::{Mpesa, Environment}; | _________^ 107 | | /// 108 | | /// fn main() { 109 | | /// dotenv::dotenv().ok(); ... | 116 | | /// client.set_initiator_password("your_initiator_password"); 117 | | /// } | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main = note: `-D clippy::needless-doctest-main` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_doctest_main)]`
/// ```
pub fn set_initiator_password<S: Into<String>>(&self, initiator_password: S) {
*self.initiator_password.borrow_mut() = Some(Secret::new(initiator_password.into()));
}
Expand All @@ -80,7 +125,14 @@ impl<'mpesa, Env: ApiEnvironment> Mpesa<Env> {
self.auth().await.is_ok()
}

#[doc = include_str!("../docs/client/auth.md")]
/// This API generates the tokens for authenticating your API calls. This is the first API you will engage with within the set of APIs available because all the other APIs require authentication information from this API to work.
///
/// Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/Authorization)
///
/// Returns auth token as a `String` that is ttl-cached in memory for subsequent requests.
///
/// # Errors
/// Returns a `MpesaError` on failure
pub(crate) async fn auth(&self) -> MpesaResult<String> {
if let Some(token) = AUTH.lock().await.cache_get(&self.client_key) {
return Ok(token.to_owned());
Expand Down

0 comments on commit fedf9cd

Please sign in to comment.