#curl #actor #tokio #perform #asynchronous #data #easy2

async-curl

An asynchronous implementation to perform curl operations with tokio

28 releases

0.4.3 Apr 5, 2024
0.4.1 Mar 6, 2024
0.2.4 Oct 29, 2023
0.1.6 May 15, 2023
0.1.3 Mar 26, 2023

#111 in HTTP client

Download history 28/week @ 2024-01-08 18/week @ 2024-01-15 63/week @ 2024-01-22 148/week @ 2024-01-29 344/week @ 2024-02-05 251/week @ 2024-02-12 323/week @ 2024-02-19 405/week @ 2024-02-26 225/week @ 2024-03-04 101/week @ 2024-03-11 19/week @ 2024-03-18 281/week @ 2024-03-25 464/week @ 2024-04-01 111/week @ 2024-04-08 8/week @ 2024-04-15

866 downloads per month
Used in 2 crates

MIT license

125KB
1.5K SLoC

async-curl

This will perform curl Easy2 asynchronously for rust-lang via an Actor using tokio

Latest Version License Documentation Build Status Downloads

How to use with multiple async request

use async_curl::{Actor, CurlActor};
use curl::easy::{Easy2, Handler, WriteError};

#[derive(Debug, Clone, Default)]
pub struct ResponseHandler {
    data: Vec<u8>,
}

impl Handler for ResponseHandler {
    /// This will store the response from the server
    /// to the data vector.
    fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
        self.data.extend_from_slice(data);
        Ok(data.len())
    }
}

impl ResponseHandler {
    /// Instantiation of the ResponseHandler
    /// and initialize the data vector.
    pub fn new() -> Self {
        Self::default()
    }

    /// This will consumed the object and
    /// give the data to the caller
    pub fn get_data(self) -> Vec<u8> {
        self.data
    }
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let actor = CurlActor::new();

    let mut easy2 = Easy2::new(ResponseHandler::new());
    easy2.url("https://www.rust-lang.org/").unwrap();
    easy2.get(true).unwrap();

    let actor1 = actor.clone();
    let spawn1 = tokio::spawn(async move {
        let mut result = actor1.send_request(easy2).await.unwrap();

        let response = result.get_ref().to_owned().get_data();
        let status = result.response_code().unwrap();

        println!("Response: {:?}", response);
        println!("Status: {:?}", status);
    });

    let mut easy2 = Easy2::new(ResponseHandler::new());
    easy2.url("https://www.rust-lang.org/").unwrap();
    easy2.get(true).unwrap();

    let spawn2 = tokio::spawn(async move {
        let mut result = actor.send_request(easy2).await.unwrap();

        let response = result.get_ref().to_owned().get_data();
        let status = result.response_code().unwrap();

        println!("Response: {:?}", response);
        println!("Status: {:?}", status);
    });

    let (_, _) = tokio::join!(spawn1, spawn2);
}

Dependencies

~16–26MB
~378K SLoC