Skip to main content

gateio_rs/http/
request.rs

1use crate::http::{Credentials, Method};
2
3/// HTTP request representation for Gate.io API calls
4#[derive(PartialEq, Eq, Debug)]
5pub struct Request {
6    pub(crate) method: Method,
7    pub(crate) path: String,
8    pub(crate) params: Vec<(String, String)>,
9    pub(crate) payload: String,
10    pub(crate) x_gate_exp_time: Option<u128>,
11    pub(crate) credentials: Option<Credentials>,
12    pub(crate) sign: bool,
13}
14
15impl Request {
16    /// Returns the HTTP method for this request
17    pub fn method(&self) -> &Method {
18        &self.method
19    }
20    /// Returns the API path for this request
21    pub fn path(&self) -> &str {
22        &self.path
23    }
24    /// Returns the query parameters for this request
25    pub fn params(&self) -> &[(String, String)] {
26        &self.params
27    }
28    /// Returns the request body payload
29    pub fn payload(&self) -> &str {
30        &self.payload
31    }
32    /// Returns the Gate expiration time if set
33    pub fn x_gate_exp_time(&self) -> &Option<u128> {
34        &self.x_gate_exp_time
35    }
36    /// Returns the API credentials if set
37    pub fn credentials(&self) -> &Option<Credentials> {
38        &self.credentials
39    }
40    /// Returns whether this request should be signed
41    pub fn sign(&self) -> &bool {
42        &self.sign
43    }
44}
45
46/// API HTTP Request
47///
48/// A low-level request builder for API integration
49/// decoupled from any specific underlying HTTP library.
50pub struct RequestBuilder {
51    method: Method,
52    path: String,
53    params: Vec<(String, String)>,
54    payload: String,
55    credentials: Option<Credentials>,
56    x_gate_exp_time: Option<u128>,
57    sign: bool,
58}
59
60impl RequestBuilder {
61    /// Creates a new request builder with method and path
62    pub fn new(method: Method, path: &str) -> Self {
63        Self {
64            method,
65            path: path.to_owned(),
66            params: vec![],
67            payload: "".to_owned(),
68            x_gate_exp_time: None,
69            credentials: None,
70            sign: false,
71        }
72    }
73
74    /// Append `params` to the request's query string. Parameters may
75    /// share the same key, and will result in a query string with one or
76    /// more duplicated query parameter keys.
77    pub fn params<'a>(mut self, params: impl IntoIterator<Item = (&'a str, &'a str)>) -> Self {
78        self.params.extend(
79            params
80                .into_iter()
81                .map(|param| (param.0.to_owned(), param.1.to_owned())),
82        );
83        self
84    }
85    /// Sets the request body payload
86    pub fn payload(mut self, payload: &str) -> Self {
87        self.payload = payload.to_owned();
88        self
89    }
90
91    /// Sets the API credentials for authentication
92    pub fn credentials(mut self, credentials: Credentials) -> Self {
93        self.credentials = Some(credentials);
94        self
95    }
96
97    /// Sets the Gate expiration time
98    pub fn x_gate_exp_time(mut self, x_gate_exp_time: u128) -> Self {
99        self.x_gate_exp_time = Some(x_gate_exp_time);
100        self
101    }
102
103    /// Marks this request to be signed with HMAC
104    pub fn sign(mut self) -> Self {
105        self.sign = true;
106        self
107    }
108}
109
110impl From<RequestBuilder> for Request {
111    fn from(builder: RequestBuilder) -> Request {
112        Request {
113            method: builder.method,
114            path: builder.path,
115            params: builder.params,
116            payload: builder.payload,
117            x_gate_exp_time: builder.x_gate_exp_time,
118            credentials: builder.credentials,
119            sign: builder.sign,
120        }
121    }
122}