http_client/
http_client.rs

1use blockless_sdk::http::{get, post, HttpClient, MultipartField};
2use std::collections::HashMap;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    println!("====================================");
6    println!("HTTP v2 Client Demo");
7    println!("====================================");
8
9    println!("\n1. GET request:");
10    match get("https://httpbin.org/get").send() {
11        Ok(response) => {
12            println!("GET Status: {}", response.status());
13            println!("GET Success: {}", response.is_success());
14        }
15        Err(e) => println!("GET Error: {}", e),
16    }
17
18    println!("\n2. POST with JSON:");
19    let json_data = serde_json::json!({
20        "name": "Blockless SDK",
21        "version": "2.0",
22        "api_style": "reqwest-like"
23    });
24    match post("https://httpbin.org/post").json(&json_data)?.send() {
25        Ok(response) => {
26            println!("POST JSON Status: {}", response.status());
27            if let Ok(response_json) = response.json::<serde_json::Value>() {
28                if let Some(received_json) = response_json.get("json") {
29                    println!("Received JSON: {}", received_json);
30                }
31            }
32        }
33        Err(e) => println!("POST JSON Error: {}", e),
34    }
35
36    println!("\n3. Client instance with default configuration:");
37    let mut default_headers = HashMap::new();
38    default_headers.insert("User-Agent".to_string(), "Blockless-SDK/2.0".to_string());
39    default_headers.insert("Accept".to_string(), "application/json".to_string());
40    let client = HttpClient::builder()
41        .default_headers(default_headers)
42        .timeout(10000)
43        .build();
44    match client
45        .get("https://httpbin.org/get")
46        .query("search", "blockless")
47        .query("limit", "10")
48        .query("format", "json")
49        .send()
50    {
51        Ok(response) => {
52            println!("Client GET Status: {}", response.status());
53            if let Ok(json_data) = response.json::<serde_json::Value>() {
54                if let Some(args) = json_data.get("args") {
55                    println!("Query params: {}", args);
56                }
57            }
58        }
59        Err(e) => println!("Client GET Error: {}", e),
60    }
61
62    println!("\n4. Authentication examples:");
63    match client
64        .get("https://httpbin.org/basic-auth/user/pass")
65        .basic_auth("user", "pass")
66        .send()
67    {
68        Ok(response) => {
69            println!("Basic auth status: {}", response.status());
70            if let Ok(json_data) = response.json::<serde_json::Value>() {
71                println!("Authenticated: {:?}", json_data.get("authenticated"));
72            }
73        }
74        Err(e) => println!("Basic auth error: {}", e),
75    }
76
77    match client
78        .get("https://httpbin.org/bearer")
79        .bearer_auth("test-token-12345")
80        .send()
81    {
82        Ok(response) => {
83            println!("Bearer auth status: {}", response.status());
84            if let Ok(json_data) = response.json::<serde_json::Value>() {
85                println!("Token received: {:?}", json_data.get("token"));
86            }
87        }
88        Err(e) => println!("Bearer auth error: {}", e),
89    }
90
91    println!("\n5. Different request body types:");
92    let mut form_data = HashMap::new();
93    form_data.insert("name".to_string(), "Blockless".to_string());
94    form_data.insert("type".to_string(), "distributed computing".to_string());
95    match client
96        .post("https://httpbin.org/post")
97        .form(form_data)
98        .send()
99    {
100        Ok(response) => {
101            println!("Form POST Status: {}", response.status());
102            if let Ok(json_data) = response.json::<serde_json::Value>() {
103                if let Some(form) = json_data.get("form") {
104                    println!("Form data received: {}", form);
105                }
106            }
107        }
108        Err(e) => println!("Form POST Error: {}", e),
109    }
110
111    println!("\n6. Multipart form with file upload:");
112    let multipart_fields = vec![
113        MultipartField::text("description", "SDK test file"),
114        MultipartField::file(
115            "upload",
116            b"Hello from Blockless SDK v2!".to_vec(),
117            "hello.txt",
118            Some("text/plain".to_string()),
119        ),
120    ];
121    match client
122        .post("https://httpbin.org/post")
123        .multipart(multipart_fields)
124        .send()
125    {
126        Ok(response) => {
127            println!("Multipart POST Status: {}", response.status());
128            if let Ok(json_data) = response.json::<serde_json::Value>() {
129                if let Some(files) = json_data.get("files") {
130                    println!("Files uploaded: {}", files);
131                }
132            }
133        }
134        Err(e) => println!("Multipart POST Error: {}", e),
135    }
136
137    println!("\n7. Binary data:");
138    let binary_data = vec![0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello" in bytes
139    match client
140        .post("https://httpbin.org/post")
141        .header("Content-Type", "application/octet-stream")
142        .body_bytes(binary_data)
143        .send()
144    {
145        Ok(response) => {
146            println!("Binary POST Status: {}", response.status());
147        }
148        Err(e) => println!("Binary POST Error: {}", e),
149    }
150
151    println!("\n8. Advanced request building:");
152    match client
153        .put("https://httpbin.org/put")
154        .header("X-Custom-Header", "custom-value")
155        .header("X-API-Version", "2.0")
156        .query("action", "update")
157        .query("id", "12345")
158        .timeout(5000)
159        .body("Updated data")
160        .send()
161    {
162        Ok(response) => {
163            println!("PUT Status: {}", response.status());
164            if let Ok(json_data) = response.json::<serde_json::Value>() {
165                if let Some(headers) = json_data.get("headers") {
166                    println!("Custom headers received: {}", headers);
167                }
168            }
169        }
170        Err(e) => println!("PUT Error: {}", e),
171    }
172
173    println!("\nDemo completed 🚀");
174    Ok(())
175}