interveil_sdk/client/
client.rs1use crate::error::VeilError;
2use crate::intent::sign::SignedIntent;
3
4#[derive(Debug, Clone)]
6pub struct SubmitResponse {
7 pub tx_hash: String,
8 pub status: String,
9}
10
11#[derive(Debug, Clone)]
18pub struct Client {
19 base_url: String,
20}
21
22impl Client {
23 pub fn new(base_url: &str) -> Self {
28 let url = base_url.trim_end_matches('/');
29 Self {
30 base_url: url.to_string(),
31 }
32 }
33
34 pub fn submit(&self, signed_intent: &SignedIntent) -> Result<SubmitResponse, VeilError> {
43 let url = format!("{}/intents", self.base_url);
44 let body = signed_intent.to_json()?;
45
46 let response = reqwest::blocking::Client::new()
47 .post(&url)
48 .header("Content-Type", "application/json")
49 .body(body)
50 .send()
51 .map_err(|e| VeilError::Http(format!("request failed: {}", e)))?;
52
53 let status = response.status();
54 let response_text = response
55 .text()
56 .map_err(|e| VeilError::Http(format!("failed to read response: {}", e)))?;
57
58 if !status.is_success() {
59 return Err(VeilError::Http(format!(
60 "node returned status {}: {}",
61 status, response_text
62 )));
63 }
64
65 let parsed: serde_json::Value = serde_json::from_str(&response_text)
68 .map_err(|e| VeilError::Http(format!("invalid JSON response: {}", e)))?;
69
70 let tx_hash = parsed["tx_hash"]
71 .as_str()
72 .ok_or_else(|| VeilError::Http("missing tx_hash in response".to_string()))?
73 .to_string();
74
75 let status_str = parsed["status"]
76 .as_str()
77 .ok_or_else(|| VeilError::Http("missing status in response".to_string()))?
78 .to_string();
79
80 Ok(SubmitResponse {
81 tx_hash,
82 status: status_str,
83 })
84 }
85
86 pub fn base_url(&self) -> &str {
88 &self.base_url
89 }
90}