async_curl/
lib.rs

1//! async-curl: An asynchronous implementation to perform curl operations with tokio.
2//! This requires a dependency with the [curl](https://crates.io/crates/curl) and [tokio](https://crates.io/crates/tokio) crates
3//!
4//! ## perform Curl Easy2 asynchronously
5//! ```rust
6//! pub mod actor;
7//! pub mod error;
8//!
9//! use async_curl::{Actor, CurlActor};
10//! use curl::easy::{Easy2, Handler, WriteError};
11//!
12//! #[derive(Debug, Clone, Default)]
13//! pub struct ResponseHandler {
14//!     data: Vec<u8>,
15//! }
16//!
17//! impl Handler for ResponseHandler {
18//!     /// This will store the response from the server
19//!     /// to the data vector.
20//!     fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
21//!         self.data.extend_from_slice(data);
22//!         Ok(data.len())
23//!     }
24//! }
25//!
26//! impl ResponseHandler {
27//!     /// Instantiation of the ResponseHandler
28//!     /// and initialize the data vector.
29//!     pub fn new() -> Self {
30//!         Self::default()
31//!     }
32//!
33//!     /// This will consumed the object and
34//!     /// give the data to the caller
35//!     pub fn get_data(self) -> Vec<u8> {
36//!         self.data
37//!     }
38//! }
39//!
40//! #[tokio::main(flavor = "current_thread")]
41//! async fn main() {
42//!     let actor = CurlActor::new();
43//!
44//!     let mut easy2 = Easy2::new(ResponseHandler::new());
45//!     easy2.url("https://www.rust-lang.org/").unwrap();
46//!     easy2.get(true).unwrap();
47//!
48//!     let actor1 = actor.clone();
49//!     let spawn1 = tokio::spawn(async move {
50//!     let mut result = actor1.send_request(easy2).await.unwrap();
51//!
52//!         let response = result.get_ref().to_owned().get_data();
53//!         let status = result.response_code().unwrap();
54//!
55//!         println!("Response: {:?}", response);
56//!         println!("Status: {:?}", status);
57//!     });
58//!
59//!     let mut easy2 = Easy2::new(ResponseHandler::new());
60//!     easy2.url("https://www.rust-lang.org/").unwrap();
61//!     easy2.get(true).unwrap();
62//!
63//!     let spawn2 = tokio::spawn(async move {
64//!         let mut result = actor.send_request(easy2).await.unwrap();
65//!
66//!         let response = result.get_ref().to_owned().get_data();
67//!         let status = result.response_code().unwrap();
68//!
69//!         println!("Response: {:?}", response);
70//!         println!("Status: {:?}", status);
71//!     });
72//!
73//!     let (_, _) = tokio::join!(spawn1, spawn2);
74//! }
75//! ```
76pub mod actor;
77pub mod curl;
78pub mod error;
79#[cfg(test)]
80mod tests;
81
82pub use actor::*;
83pub use curl::*;
84pub use error::*;