From cf648bee333d59798d29ff2f37f5985ec29caf4b Mon Sep 17 00:00:00 2001 From: Phidel Musungu Date: Wed, 25 Sep 2024 01:35:34 +0300 Subject: [PATCH] updated Mpesa struct to be thread safe --- .github/workflows/release-core.yml | 32 +++++++++++++++--------------- .gitignore | 1 + src/client.rs | 18 +++++++++-------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/.github/workflows/release-core.yml b/.github/workflows/release-core.yml index 8a121520a..e6c32694e 100644 --- a/.github/workflows/release-core.yml +++ b/.github/workflows/release-core.yml @@ -23,24 +23,24 @@ jobs: uses: MarcoIeni/release-plz-action@v0.5 env: GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }} - CARGO_REGISTRY_TOKEN: ${{ CARGO_REGISTRY_TOKEN }} + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} sync-cargo-docs: name: Sync cargo docs to gh-pages runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - name: Sync Docs - run: | - cargo doc - pip install ghp-import - echo '' > target/doc/index.html - ghp-import -n target/doc - git push -qf https://github.com/collinsmuriuki/mpesa-rust.git gh-pages + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: "3.10" + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + - name: Sync Docs + run: | + cargo doc + pip install ghp-import + echo '' > target/doc/index.html + ghp-import -n target/doc + git push -qf https://github.com/collinsmuriuki/mpesa-rust.git gh-pages diff --git a/.gitignore b/.gitignore index 56c16255d..92bc9c15d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ Cargo.lock .DS_Store .vscode +act.sh \ No newline at end of file diff --git a/src/client.rs b/src/client.rs index 5b804b635..35216db81 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,5 @@ -use std::cell::RefCell; +use std::borrow::BorrowMut; +use std::sync::{Arc, RwLock}; use std::time::Duration; use cached::Cached; @@ -31,7 +32,7 @@ const CARGO_PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION"); pub struct Mpesa { consumer_key: String, consumer_secret: Secret, - initiator_password: RefCell>>, + initiator_password: Arc>>>, pub(crate) base_url: String, certificate: String, pub(crate) http_client: HttpClient, @@ -77,7 +78,7 @@ impl Mpesa { Self { consumer_key: consumer_key.into(), consumer_secret: Secret::new(consumer_secret.into()), - initiator_password: RefCell::new(None), + initiator_password: Arc::new(RwLock::new(None)), base_url, certificate, http_client, @@ -87,8 +88,9 @@ impl Mpesa { /// Gets the initiator password /// If `None`, the default password is `"Safcom496!"` pub(crate) fn initiator_password(&self) -> String { - self.initiator_password - .borrow() + let mut pass = self.initiator_password.read().unwrap(); + + pass.borrow_mut() .as_ref() .map(|password| password.expose_secret().into()) .unwrap_or(DEFAULT_INITIATOR_PASSWORD.to_owned()) @@ -132,8 +134,8 @@ impl Mpesa { /// assert!(client.is_connected().await); /// } /// ``` - pub fn set_initiator_password>(&self, initiator_password: S) { - *self.initiator_password.borrow_mut() = Some(Secret::new(initiator_password.into())); + pub fn set_initiator_password>(&mut self, initiator_password: S) { + *self.initiator_password.write().unwrap() = Some(Secret::new(initiator_password.into())); } /// Checks if the client can be authenticated @@ -332,7 +334,7 @@ mod tests { #[test] fn test_setting_initator_password() { - let client = Mpesa::new("consumer_key", "consumer_secret", Sandbox); + let mut client = Mpesa::new("consumer_key", "consumer_secret", Sandbox); assert_eq!(client.initiator_password(), DEFAULT_INITIATOR_PASSWORD); client.set_initiator_password("foo_bar"); assert_eq!(client.initiator_password(), "foo_bar".to_string());