Skip to content

Commit

Permalink
Merge branch 'collinsmuriuki:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kodesoul authored Jan 8, 2022
2 parents add32f7 + 0cd303a commit 6cdbf86
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --no-fail-fast

fmt:
name: Rustfmt
Expand Down Expand Up @@ -72,4 +73,4 @@ jobs:
- name: Run cargo-tarpaulin
uses: actions-rs/[email protected]
with:
args: '--ignore-tests'
args: '--ignore-tests --no-fail-fast'
12 changes: 10 additions & 2 deletions mpesa_core/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::environment::Environment;
use super::services::{
AccountBalanceBuilder, B2bBuilder, B2cBuilder, C2bRegisterBuilder, C2bSimulateBuilder,
MpesaExpressRequestBuilder,
};
use crate::services::MpesaExpressRequestBuilder;
use crate::MpesaSecurity;
use mpesa_derive::*;
use reqwest::blocking::Client;
Expand All @@ -19,6 +19,7 @@ pub struct Mpesa {
client_secret: String,
initiator_password: RefCell<Option<String>>,
environment: Environment,
pub(crate) http_client: Client,
}

impl<'a> Mpesa {
Expand All @@ -33,11 +34,17 @@ impl<'a> Mpesa {
/// );
/// ```
pub fn new(client_key: String, client_secret: String, environment: Environment) -> Self {
let http_client = Client::builder()
.connect_timeout(std::time::Duration::from_millis(10000))
.build()
// TODO: Potentialy return a `Result` enum from Mpesa::new?
.expect("Error building http client");
Self {
client_key,
client_secret,
initiator_password: RefCell::new(None),
environment,
http_client,
}
}

Expand Down Expand Up @@ -94,7 +101,8 @@ impl<'a> Mpesa {
"{}/oauth/v1/generate?grant_type=client_credentials",
self.environment.base_url()
);
let resp = Client::new()
let resp = self
.http_client
.get(&url)
.basic_auth(&self.client_key, Some(&self.client_secret))
.send()?;
Expand Down
8 changes: 5 additions & 3 deletions mpesa_core/src/services/account_balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::client::MpesaResult;
use crate::constants::{CommandId, IdentifierTypes};
use crate::{Mpesa, MpesaError, MpesaSecurity};
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -164,11 +163,14 @@ impl<'a> AccountBalanceBuilder<'a> {
security_credential: &credentials,
};

let response = Client::new()
let response = self
.client
.http_client
.post(&url)
.bearer_auth(self.client.auth()?)
.json(&payload)
.send()?;
.send()?
.error_for_status()?;

if response.status().is_success() {
let value: AccountBalanceResponse = response.json()?;
Expand Down
8 changes: 5 additions & 3 deletions mpesa_core/src/services/b2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::client::{Mpesa, MpesaResult};
use crate::constants::{CommandId, IdentifierTypes};
use crate::errors::MpesaError;
use crate::MpesaSecurity;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -231,11 +230,14 @@ impl<'a> B2bBuilder<'a> {
account_reference: self.account_ref.unwrap_or(""),
};

let response = Client::new()
let response = self
.client
.http_client
.post(&url)
.bearer_auth(self.client.auth()?)
.json(&payload)
.send()?;
.send()?
.error_for_status()?;

if response.status().is_success() {
let value: B2bResponse = response.json()?;
Expand Down
8 changes: 5 additions & 3 deletions mpesa_core/src/services/b2c.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::client::MpesaResult;
use crate::{CommandId, Mpesa, MpesaError, MpesaSecurity};
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -197,11 +196,14 @@ impl<'a> B2cBuilder<'a> {
occasion: self.occasion.unwrap_or("None"),
};

let response = Client::new()
let response = self
.client
.http_client
.post(&url)
.bearer_auth(self.client.auth()?)
.json(&payload)
.send()?;
.send()?
.error_for_status()?;

if response.status().is_success() {
let value: B2cResponse = response.json()?;
Expand Down
8 changes: 5 additions & 3 deletions mpesa_core/src/services/c2b_register.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::client::{Mpesa, MpesaResult};
use crate::constants::ResponseType;
use crate::errors::MpesaError;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -112,11 +111,14 @@ impl<'a> C2bRegisterBuilder<'a> {
short_code: self.short_code.unwrap_or("None"),
};

let response = Client::new()
let response = self
.client
.http_client
.post(&url)
.bearer_auth(self.client.auth()?)
.json(&payload)
.send()?;
.send()?
.error_for_status()?;

if response.status().is_success() {
let value: C2bRegisterResponse = response.json()?;
Expand Down
8 changes: 5 additions & 3 deletions mpesa_core/src/services/c2b_simulate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::client::{Mpesa, MpesaResult};
use crate::constants::CommandId;
use crate::errors::MpesaError;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -122,11 +121,14 @@ impl<'a> C2bSimulateBuilder<'a> {
short_code: self.short_code.unwrap_or("None"),
};

let response = Client::new()
let response = self
.client
.http_client
.post(&url)
.bearer_auth(self.client.auth()?)
.json(&payload)
.send()?;
.send()?
.error_for_status()?;

if response.status().is_success() {
let value: C2bSimulateResponse = response.json()?;
Expand Down
8 changes: 5 additions & 3 deletions mpesa_core/src/services/express_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::client::{Mpesa, MpesaResult};
use crate::constants::CommandId;
use crate::errors::MpesaError;
use chrono::prelude::Local;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -215,11 +214,14 @@ impl<'a> MpesaExpressRequestBuilder<'a> {
transaction_desc: self.transaction_desc.unwrap_or("None"),
};

let response = Client::new()
let response = self
.client
.http_client
.post(&url)
.bearer_auth(self.client.auth()?)
.json(&payload)
.send()?;
.send()?
.error_for_status()?;

if response.status().is_success() {
let value: MpesaExpressRequestResponse = response.json()?;
Expand Down

0 comments on commit 6cdbf86

Please sign in to comment.