ezstripe/
payout.rs

1use std::collections::HashMap;
2
3use serde::{ Serialize, Deserialize };
4
5include!("split/structs/payout/response.rs");
6
7include!("split/structs/payout/response_list.rs");
8
9#[doc(hidden)]
10#[derive(PartialEq)]
11pub enum Types {
12  CREATE(String),
13  RETRIEVE(String),
14  UPDATE(String, String),
15  LIST(String),
16  CANCEL(String),
17  REVERSE(String, String)
18}
19
20#[doc(hidden)]
21const PAYOUT_URL: &str = "https://api.stripe.com/v1/payouts";
22
23#[doc(hidden)]
24impl Types {
25  pub fn create_send_request(&self, client: &reqwest::Client, secret: &str)-> reqwest::RequestBuilder {
26    let mut result = client
27      .post(self._get_url())
28      .basic_auth(secret, None::<&str>)
29      .header("Content-Type", "application/x-www-form-urlencoded");
30    
31    if let Some(r) = self._get_body() {
32      result = result.body(r);
33    }
34
35    result
36  }
37
38  pub fn create_get_request(&self, client: &reqwest::Client, secret: &str)-> reqwest::RequestBuilder {
39    let mut result = client
40      .get(self._get_url())
41      .basic_auth(secret, None::<&str>)
42      .header("Content-Type", "application/x-www-form-urlencoded");
43    
44    if let Some(r) = self._get_body() {
45      result = result.body(r);
46    }
47
48    result
49  }
50
51  fn _get_url(&self) -> String {
52    match self {
53      Self::CREATE(_) => format!("{}", PAYOUT_URL),
54      Self::RETRIEVE(id) => format!("{}/{}", PAYOUT_URL, id),
55      Self::UPDATE(id, _) => format!("{}/{}", PAYOUT_URL, id),
56      Self::LIST(_) => format!("{}", PAYOUT_URL),
57      Self::CANCEL(id) => format!("{}/{}/cancel", PAYOUT_URL, id),
58      Self::REVERSE(id, _) => format!("{}/{}/reverse", PAYOUT_URL, id)
59    }
60  }
61
62  fn _get_body(&self) -> Option<String> {
63    let body = match self {
64      Self::CREATE(body) => body,
65      Self::UPDATE(_, body) => body,
66      Self::LIST(body) => body,
67      Self::REVERSE(_, body) => body,
68      _ => ""
69    };
70
71    if body.is_empty() {
72      None
73    } else {
74      Some(body.to_string())
75    }
76  }
77}
78
79#[doc(hidden)]
80pub struct Info<'a> {
81  pub r#type: Types,
82  pub secret_key: String,
83  pub reqwest_client: &'a reqwest::Client
84}
85
86impl Info<'_> {
87  /// Sends a "POST" request to Stripe's API.
88  pub async fn send(&self) -> Result<Response, (String, Option<crate::error::Info>)> {
89    match self.r#type {
90      Types::RETRIEVE(_) => {
91        if log::log_enabled!(log::Level::Error) {
92          log::error!("The selected type is not compatible with `send()`. Please use the `get()` function");
93        }
94        return Err(("This function is not compatible with the selected type".to_string(), None));
95      },
96      Types::LIST(_) => {
97        if log::log_enabled!(log::Level::Error) {
98          log::error!("The selected type is not compatible with `send()`. Please use the `get_list()` function");
99        }
100        return Err(("This function is not compatible with the selected type".to_string(), None));
101      },
102      _ => ()
103    };
104
105    crate::helper::make_reqwest::<Response>(self.r#type.create_send_request(self.reqwest_client, &self.secret_key)).await
106  }
107  
108  /// Sends a "GET" request to Stripe's API.
109  pub async fn get(&self) -> Result<Response, (String, Option<crate::error::Info>)> {
110    match self.r#type {
111      Types::RETRIEVE(_) => (),
112      Types::LIST(_) => {
113        if log::log_enabled!(log::Level::Error) {
114          log::error!("The selected type is not compatible with `get()`. Please use the `get_list()` function");
115        }
116        return Err(("This function is not compatible with the selected type".to_string(), None));
117      },
118      _ => {
119        if log::log_enabled!(log::Level::Error) {
120          log::error!("The selected type is not compatible with `get()`. Please use the `send()` function");
121        }
122        return Err(("This function is not compatible with the selected type".to_string(), None));
123      }
124    };
125    
126    crate::helper::make_reqwest::<Response>(self.r#type.create_get_request(self.reqwest_client, &self.secret_key)).await
127  }
128
129  /// Sends a "GET" request to Stripe's API.
130  pub async fn get_list(&self) -> Result<ResponseList, (String, Option<crate::error::Info>)> {
131    match self.r#type {
132      Types::LIST(_) => (),
133      Types::RETRIEVE(_) => {
134        if log::log_enabled!(log::Level::Error) {
135          log::error!("The selected type is not compatible with `get_list()`. Please use the `get()` function");
136        }
137        return Err(("This function is not compatible with the selected type".to_string(), None));
138      },
139      _ => {
140        if log::log_enabled!(log::Level::Error) {
141          log::error!("The selected type is not compatible with `get_list()`. Please use the `send()` function");
142        }
143        return Err(("This function is not compatible with the selected type".to_string(), None));
144      }
145    };
146    
147    crate::helper::make_reqwest::<ResponseList>(self.r#type.create_get_request(self.reqwest_client, &self.secret_key)).await
148  }
149}