dora_ssr/dora/http_client.rs
1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10 fn httpclient_post_async(url: i64, json: i64, timeout: f32, func0: i32, stack0: i64);
11 fn httpclient_post_with_headers_async(url: i64, headers: i64, json: i64, timeout: f32, func0: i32, stack0: i64);
12 fn httpclient_post_with_headers_part_async(url: i64, headers: i64, json: i64, timeout: f32, func0: i32, stack0: i64, func1: i32, stack1: i64);
13 fn httpclient_get_async(url: i64, timeout: f32, func0: i32, stack0: i64);
14 fn httpclient_download_async(url: i64, full_path: i64, timeout: f32, func0: i32, stack0: i64);
15}
16/// An HTTP client interface.
17pub struct HttpClient { }
18impl HttpClient {
19 /// Sends a POST request to the specified URL and returns the response body.
20 ///
21 /// # Arguments
22 ///
23 /// * `url` - The URL to send the request to.
24 /// * `json` - The JSON data to send in the request body.
25 /// * `timeout` - The timeout in seconds for the request.
26 /// * `callback` - A callback function that is called when the request is complete. The function receives the response body as a parameter.
27 pub fn post_async(url: &str, json: &str, timeout: f32, mut callback: Box<dyn FnMut(Option<String>)>) {
28 let mut stack0 = crate::dora::CallStack::new();
29 let stack_raw0 = stack0.raw();
30 let func_id0 = crate::dora::push_function(Box::new(move || {
31 callback(stack0.pop_str())
32 }));
33 unsafe { httpclient_post_async(crate::dora::from_string(url), crate::dora::from_string(json), timeout, func_id0, stack_raw0); }
34 }
35 /// Sends a POST request to the specified URL with custom headers and returns the response body.
36 ///
37 /// # Arguments
38 ///
39 /// * `url` - The URL to send the request to.
40 /// * `headers` - A vector of headers to include in the request. Each header should be in the format `key: value`.
41 /// * `json` - The JSON data to send in the request body.
42 /// * `timeout` - The timeout in seconds for the request.
43 /// * `callback` - A callback function that is called when the request is complete. The function receives the response body as a parameter.
44 pub fn post_with_headers_async(url: &str, headers: &Vec<&str>, json: &str, timeout: f32, mut callback: Box<dyn FnMut(Option<String>)>) {
45 let mut stack0 = crate::dora::CallStack::new();
46 let stack_raw0 = stack0.raw();
47 let func_id0 = crate::dora::push_function(Box::new(move || {
48 callback(stack0.pop_str())
49 }));
50 unsafe { httpclient_post_with_headers_async(crate::dora::from_string(url), crate::dora::Vector::from_str(headers), crate::dora::from_string(json), timeout, func_id0, stack_raw0); }
51 }
52 /// Sends a POST request to the specified URL with custom headers and returns the response body.
53 ///
54 /// # Arguments
55 ///
56 /// * `url` - The URL to send the request to.
57 /// * `headers` - A vector of headers to include in the request. Each header should be in the format `key: value`.
58 /// * `json` - The JSON data to send in the request body.
59 /// * `timeout` - The timeout in seconds for the request.
60 /// * `part_callback` - A callback function that is called periodically to get part of the response content. Returns `true` to stop the request.
61 /// * `callback` - A callback function that is called when the request is complete. The function receives the response body as a parameter.
62 pub fn post_with_headers_part_async(url: &str, headers: &Vec<&str>, json: &str, timeout: f32, mut part_callback: Box<dyn FnMut(&str) -> bool>, mut callback: Box<dyn FnMut(Option<String>)>) {
63 let mut stack0 = crate::dora::CallStack::new();
64 let stack_raw0 = stack0.raw();
65 let func_id0 = crate::dora::push_function(Box::new(move || {
66 let result = part_callback(stack0.pop_str().unwrap().as_str());
67 stack0.push_bool(result);
68 }));
69 let mut stack1 = crate::dora::CallStack::new();
70 let stack_raw1 = stack1.raw();
71 let func_id1 = crate::dora::push_function(Box::new(move || {
72 callback(stack1.pop_str())
73 }));
74 unsafe { httpclient_post_with_headers_part_async(crate::dora::from_string(url), crate::dora::Vector::from_str(headers), crate::dora::from_string(json), timeout, func_id0, stack_raw0, func_id1, stack_raw1); }
75 }
76 /// Sends a GET request to the specified URL and returns the response body.
77 ///
78 /// # Arguments
79 ///
80 /// * `url` - The URL to send the request to.
81 /// * `timeout` - The timeout in seconds for the request.
82 /// * `callback` - A callback function that is called when the request is complete. The function receives the response body as a parameter.
83 pub fn get_async(url: &str, timeout: f32, mut callback: Box<dyn FnMut(Option<String>)>) {
84 let mut stack0 = crate::dora::CallStack::new();
85 let stack_raw0 = stack0.raw();
86 let func_id0 = crate::dora::push_function(Box::new(move || {
87 callback(stack0.pop_str())
88 }));
89 unsafe { httpclient_get_async(crate::dora::from_string(url), timeout, func_id0, stack_raw0); }
90 }
91 /// Downloads a file asynchronously from the specified URL and saves it to the specified path.
92 ///
93 /// # Arguments
94 ///
95 /// * `url` - The URL of the file to download.
96 /// * `full_path` - The full path where the downloaded file should be saved.
97 /// * `timeout` - The timeout in seconds for the request.
98 /// * `progress` - A callback function that is called periodically to report the download progress.
99 /// The function receives three parameters: `interrupted` (a boolean value indicating whether the download was interrupted), `current` (the number of bytes downloaded so far) and `total` (the total number of bytes to be downloaded).
100 pub fn download_async(url: &str, full_path: &str, timeout: f32, mut progress: Box<dyn FnMut(bool, i64, i64) -> bool>) {
101 let mut stack0 = crate::dora::CallStack::new();
102 let stack_raw0 = stack0.raw();
103 let func_id0 = crate::dora::push_function(Box::new(move || {
104 let result = progress(stack0.pop_bool().unwrap(), stack0.pop_i64().unwrap(), stack0.pop_i64().unwrap());
105 stack0.push_bool(result);
106 }));
107 unsafe { httpclient_download_async(crate::dora::from_string(url), crate::dora::from_string(full_path), timeout, func_id0, stack_raw0); }
108 }
109}