Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth response deserializion errors #84

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_repr = "0.1"
thiserror = "1.0.37"
wiremock = "0.5"
secrecy = "0.8.0"
serde-aux = "4.2.0"

[dev-dependencies]
dotenv = "0.15"
Expand Down
8 changes: 5 additions & 3 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cached::proc_macro::cached;
use serde::{Deserialize, Serialize};
use serde_aux::field_attributes::deserialize_number_from_string;

use crate::{ApiEnvironment, ApiError, Mpesa, MpesaError, MpesaResult};

Expand Down Expand Up @@ -39,15 +40,16 @@ pub struct AuthenticationResponse {
/// Access token which is used as the Bearer-Auth-Token
pub access_token: String,
/// Expiry time in seconds
pub expiry_in: u64,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub expires_in: u64,
}

impl std::fmt::Display for AuthenticationResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"token :{} expires in: {}",
self.access_token, self.expiry_in
self.access_token, self.expires_in
)
}
}
Expand Down Expand Up @@ -97,7 +99,7 @@ mod tests {
.respond_with(wiremock::ResponseTemplate::new(200).set_body_json(
AuthenticationResponse {
access_token: "test_token".to_string(),
expiry_in: 3600,
expires_in: 3600,
},
))
.expect(1)
Expand Down
4 changes: 2 additions & 2 deletions tests/mpesa-rust/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! get_mpesa_client {
.and(query_param("grant_type", "client_credentials"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"access_token": "dummy_access_token",
"expiry_in": 3600
"expires_in": "3600"
})))
.mount(&server)
.await;
Expand All @@ -73,7 +73,7 @@ macro_rules! get_mpesa_client {
.and(query_param("grant_type", "client_credentials"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"access_token": "dummy_access_token",
"expiry_in": 3600
"expires_in": "3600"
})))
.mount(&server)
.await;
Expand Down
Loading