aptos_client_icp/http/
mod.rs1use serde::Serialize;
2
3use crate::{
4 apis::{self, transactions_api::EncodeSubmissionError},
5 client::{CallOptions, HttpProvider},
6};
7
8#[derive(Debug, Clone)]
9pub enum HttpMethod {
10 GET,
11 POST,
12 PUT,
13 DELETE,
14 PATCH,
15 HEAD,
16 OPTIONS,
17 TRACE,
18}
19
20#[derive(Debug, Clone)]
21pub struct Client {}
22pub struct Response {
23 status: u16,
24 body: String,
25}
26
27#[derive(Debug, Clone)]
28pub struct StatusCode(u16);
29impl StatusCode {
30 pub fn is_server_error(&self) -> bool {
31 self.0 >= 500 && self.0 < 600
32 }
33 pub fn is_client_error(&self) -> bool {
34 self.0 >= 400 && self.0 < 500
35 }
36}
37impl Response {
38 pub fn new(status: u16, body: String) -> Self {
39 Self { status, body }
40 }
41 pub fn status(&self) -> StatusCode {
42 StatusCode(self.status)
43 }
44 pub async fn text<E>(&self) -> Result<String, apis::Error<E>> {
45 Ok(self.body.clone())
46 }
47}
48
49impl Client {
50 pub fn request(&self, method: HttpMethod, url: &str) -> RequestBuilder {
51 RequestBuilder::new(method, url)
52 }
53 pub async fn execute<E>(&self, request: Request) -> Result<Response, apis::Error<E>> {
54 Ok(HttpProvider::new()
55 .send(request, CallOptions::default())
56 .await)
57 }
58}
59
60pub struct RequestBuilder {
61 method: HttpMethod,
62 url: String,
63 queries: Vec<(String, String)>,
64 headers: Vec<(String, String)>,
65 body: Option<Vec<u8>>,
66}
67
68#[derive(Debug, Clone)]
69pub struct Request {
70 pub url: String,
71 body: Option<Vec<u8>>,
72 query: Vec<(String, String)>,
73 headers: Vec<(String, String)>,
74 method: HttpMethod,
75}
76
77impl Request {
78 pub fn body(&self) -> Option<Vec<u8>> {
79 self.body.clone()
80 }
81 pub fn query(&self) -> Vec<(String, String)> {
82 self.query.clone()
83 }
84 pub fn headers(&self) -> Vec<(String, String)> {
85 self.headers.clone()
86 }
87 pub fn method(&self) -> HttpMethod {
88 self.method.clone()
89 }
90}
91
92impl RequestBuilder {
93 pub fn new(method: HttpMethod, url: &str) -> Self {
94 Self {
95 method,
96 url: url.to_string(),
97 queries: Vec::new(),
98 headers: Vec::new(),
99 body: None,
100 }
101 }
102
103 pub fn query(self, params: &[(&str, &str)]) -> Self {
104 let mut queries = self.queries;
105 for (key, value) in params {
106 queries.push((key.to_string(), value.to_string()));
107 }
108 Self { queries, ..self }
109 }
110
111 pub fn header(self, key: String, value: String) -> Self {
112 let mut headers = self.headers;
113 headers.push((key, value.to_string()));
114 Self { headers, ..self }
115 }
116
117 pub fn json<T: Serialize + ?Sized>(self, json: &T) -> Self {
118 let body = serde_json::to_string(json).unwrap();
119 Self {
120 body: Some(body.as_bytes().to_vec()),
121 ..self
122 }
123 }
124
125 pub fn build<E>(&self) -> Result<Request, apis::Error<E>> {
126 Ok(Request {
127 body: self.body.clone(),
128 query: self
129 .queries
130 .iter()
131 .map(|(k, v)| (k.clone(), v.clone()))
132 .collect(),
133 headers: self.headers.clone(),
134 method: self.method.clone(),
135 url: self.url.clone(),
136 })
137 }
138}