pix_api_client/
oauth.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{ApiRequest, PixClient};
5
6pub struct OauthTokenEndpoint<'a> {
7    inner: &'a PixClient,
8}
9
10#[derive(Debug, Serialize, Deserialize)]
11struct OauthTokenPayload {
12    grant_type: String,
13}
14
15impl Default for OauthTokenPayload {
16    fn default() -> Self {
17        Self {
18            grant_type: "client_credentials".to_string(),
19        }
20    }
21}
22
23impl PixClient {
24    pub fn oauth(&self) -> OauthTokenEndpoint {
25        OauthTokenEndpoint { inner: self }
26    }
27}
28
29#[derive(Debug, Serialize, Deserialize)]
30pub struct OauthTokenResponse {
31    pub access_token: String,
32    pub token_type: String,
33    pub expires_in: i32,
34    pub scope: String,
35}
36
37impl OauthTokenEndpoint<'_> {
38    pub fn autenticar(&self, full_custom_endpoint: Option<String>) -> ApiRequest<OauthTokenResponse> {
39        let endpoint = full_custom_endpoint.unwrap_or_else(|| format!("{}/oauth/token", self.inner.base_endpoint));
40
41        self.inner
42            .request_with_headers(Method::POST, &*endpoint, OauthTokenPayload::default())
43    }
44}