Skip to content

Commit

Permalink
updated Mpesa struct to be thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
phydy committed Sep 24, 2024
1 parent a590bcd commit cf648be
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/release-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ jobs:
uses: MarcoIeni/[email protected]
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 '<meta http-equiv=refresh content=0;url=mpesa/index.html>' > 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 '<meta http-equiv=refresh content=0;url=mpesa/index.html>' > target/doc/index.html
ghp-import -n target/doc
git push -qf https://github.com/collinsmuriuki/mpesa-rust.git gh-pages
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Cargo.lock
.DS_Store

.vscode
act.sh
18 changes: 10 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::borrow::BorrowMut;
use std::sync::{Arc, RwLock};
use std::time::Duration;

use cached::Cached;
Expand Down Expand Up @@ -31,7 +32,7 @@ const CARGO_PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
pub struct Mpesa {
consumer_key: String,
consumer_secret: Secret<String>,
initiator_password: RefCell<Option<Secret<String>>>,
initiator_password: Arc<RwLock<Option<Secret<String>>>>,
pub(crate) base_url: String,
certificate: String,
pub(crate) http_client: HttpClient,
Expand Down Expand Up @@ -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,
Expand All @@ -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())
Expand Down Expand Up @@ -132,8 +134,8 @@ impl Mpesa {
/// assert!(client.is_connected().await);

Check failure on line 134 in src/client.rs

View workflow job for this annotation

GitHub Actions / Test

cannot borrow `client` as mutable, as it is not declared as mutable
/// }
/// ```
pub fn set_initiator_password<S: Into<String>>(&self, initiator_password: S) {
*self.initiator_password.borrow_mut() = Some(Secret::new(initiator_password.into()));
pub fn set_initiator_password<S: Into<String>>(&mut self, initiator_password: S) {
*self.initiator_password.write().unwrap() = Some(Secret::new(initiator_password.into()));
}

/// Checks if the client can be authenticated
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit cf648be

Please sign in to comment.