1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Authentication endpoint
pub use refresh::{Request as Refresh, Response as RefreshResponse};
mod refresh {
use crate::{endpoints::handle_response, Result};
use reqwest::Client as HttpClient;
use serde::{Deserialize, Serialize};
/// The response received from the Monzo API after a successful request to
/// refresh the authentication.
#[derive(Deserialize, Debug)]
pub struct Response {
/// New access token for authorising requests against the Monzo API
pub access_token: String,
/// The id of the client
pub client_id: String,
/// time (in seconds) until the access token expires
pub expires_in: i64,
/// Refresh token. This token can be used to generate a new
/// access/refresh token pair
pub refresh_token: String,
/// The token type. currently the only supported token type is
/// "bearer_auth"
pub token_type: String,
/// The id of the current Monzo user
pub user_id: String,
}
/// A request for new authentication tokens.
pub struct Request {
reqwest_builder: reqwest::RequestBuilder,
}
#[derive(Serialize)]
struct Payload<'a> {
grant_type: &'static str,
client_id: &'a str,
client_secret: &'a str,
refresh_token: &'a str,
}
impl<'a> Payload<'a> {
fn new(client_id: &'a str, client_secret: &'a str, refresh_token: &'a str) -> Self {
Payload {
grant_type: "refresh_token",
client_id,
client_secret,
refresh_token,
}
}
}
impl Request {
pub(crate) fn new(
http_client: &HttpClient,
client_id: &str,
client_secret: &str,
refresh_token: &str,
) -> Self {
let payload = Payload::new(client_id, client_secret, refresh_token);
let reqwest_builder = http_client
.post("https://api.monzo.com/oauth2/token")
.form(&payload);
Self { reqwest_builder }
}
/// Send the response to the Monzo server.
///
/// This method consumes the request and produces a future which will
/// resolve to a `[RefreshResponse]`. This method is effectively an
/// alias for the `into_future` method.
pub async fn send(self) -> Result<Response> {
handle_response(self.reqwest_builder).await
}
}
}