Skip to content

Commit 5d89a4a

Browse files
authored
Merge pull request #84 from collinsmuriuki/fix-auth-response-deserialization-error
Fix auth response deserializion errors
2 parents c15ac9f + 84448a4 commit 5d89a4a

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ serde_repr = "0.1"
2424
thiserror = "1.0.37"
2525
wiremock = "0.5"
2626
secrecy = "0.8.0"
27+
serde-aux = "4.2.0"
2728

2829
[dev-dependencies]
2930
dotenv = "0.15"

src/auth.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cached::proc_macro::cached;
22
use serde::{Deserialize, Serialize};
3+
use serde_aux::field_attributes::deserialize_number_from_string;
34

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

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

4547
impl std::fmt::Display for AuthenticationResponse {
4648
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4749
write!(
4850
f,
4951
"token :{} expires in: {}",
50-
self.access_token, self.expiry_in
52+
self.access_token, self.expires_in
5153
)
5254
}
5355
}
@@ -97,7 +99,7 @@ mod tests {
9799
.respond_with(wiremock::ResponseTemplate::new(200).set_body_json(
98100
AuthenticationResponse {
99101
access_token: "test_token".to_string(),
100-
expiry_in: 3600,
102+
expires_in: 3600,
101103
},
102104
))
103105
.expect(1)

tests/mpesa-rust/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ macro_rules! get_mpesa_client {
4646
.and(query_param("grant_type", "client_credentials"))
4747
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
4848
"access_token": "dummy_access_token",
49-
"expiry_in": 3600
49+
"expires_in": "3600"
5050
})))
5151
.mount(&server)
5252
.await;
@@ -73,7 +73,7 @@ macro_rules! get_mpesa_client {
7373
.and(query_param("grant_type", "client_credentials"))
7474
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
7575
"access_token": "dummy_access_token",
76-
"expiry_in": 3600
76+
"expires_in": "3600"
7777
})))
7878
.mount(&server)
7979
.await;

0 commit comments

Comments
 (0)