Crates.io | curl-http-client |
lib.rs | curl-http-client |
version | 2.3.3 |
source | src |
created_at | 2023-10-22 13:27:31.712227 |
updated_at | 2024-04-08 23:46:12.965287 |
description | This is a wrapper for Easy2 from curl-rust crate for ergonomic use and can perform synchronously and asynchronously using async-curl crate that uses an actor model (Message passing) to achieve a non-blocking I/O. |
homepage | https://github.com/LorenzoLeonardo/curl-http-client |
repository | https://github.com/LorenzoLeonardo/curl-http-client |
max_upload_size | |
id | 1010588 |
size | 7,428,427 |
This is a wrapper for Easy2 from curl-rust crate for ergonomic use and is able to perform synchronously and asynchronously using async-curl crate that uses an actor model (Message passing) to achieve a non-blocking I/O.
use async_curl::CurlActor;
use curl_http_client::*;
use http::{Method, Request};
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let actor = CurlActor::new();
let collector = Collector::Ram(Vec::new());
let request = Request::builder()
.uri("<SOURCE URL>")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.request(request).unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
}
use async_curl::CurlActor;
use curl_http_client::*;
use http::{Method, Request};
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let actor = CurlActor::new();
let collector = Collector::Ram(Vec::new());
let request = Request::builder()
.uri("<TARGET URL>")
.method(Method::POST)
.body(Some("test body".as_bytes().to_vec()))
.unwrap();
let response = HttpClient::new(collector)
.request(request).unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
}
use std::path::PathBuf;
use async_curl::CurlActor;
use curl_http_client::*;
use http::{Method, Request};
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let actor = CurlActor::new();
let collector = Collector::File(FileInfo::path(PathBuf::from("<FILE PATH TO SAVE>")));
let request = Request::builder()
.uri("<SOURCE URL>")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.request(request)
.unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
Ok(())
}
use std::{fs, path::PathBuf};
use async_curl::CurlActor;
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let file_to_be_uploaded = PathBuf::from("<FILE PATH TO BE UPLOADED>");
let file_size = fs::metadata(file_to_be_uploaded.as_path()).unwrap().len() as usize;
let actor = CurlActor::new();
let collector = Collector::File(FileInfo::path(file_to_be_uploaded));
let request = Request::builder()
.uri("<TARGET URL>")
.method(Method::PUT)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.upload_file_size(FileSize::from(file_size)).unwrap()
.request(request).unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
}
use async_curl::CurlActor;
use curl_http_client::*;
use futures::future;
use http::{HeaderMap, Method, Request};
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
const NUM_CONCURRENT: usize = 5;
let actor = CurlActor::new();
let mut handles = Vec::new();
for _n in 0..NUM_CONCURRENT {
let actor = actor.clone();
let handle = tokio::spawn(async move {
let collector = Collector::Ram(Vec::new());
let request = Request::builder()
.uri("https://www.rust-lang.org/")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.request(request)
.unwrap()
.nonblocking(actor)
.perform()
.await
.unwrap();
println!("Response: {:?}", response);
});
handles.push(handle);
}
let results: Vec<Result<_, _>> = future::join_all(handles).await;
for (_i, result) in results.into_iter().enumerate() {
result.unwrap();
}
}
use std::fs;
use std::path::PathBuf;
use async_curl::CurlActor;
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let actor = CurlActor::new();
let save_to = PathBuf::from("<FILE PATH TO SAVE>");
let collector = Collector::File(FileInfo::path(save_to.clone()));
let partial_download_file_size = fs::metadata(save_to.as_path()).unwrap().len() as usize;
let request = Request::builder()
.uri("<SOURCE URL>")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.resume_from(BytesOffset::from(partial_download_file_size)).unwrap()
.request(request).unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
}
use std::path::PathBuf;
use async_curl::CurlActor;
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use tokio::sync::mpsc::channel;
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let (tx, mut rx) = channel(1);
let actor = CurlActor::new();
let file_info = FileInfo::path(PathBuf::from("<FILE PATH TO SAVE>")).with_transfer_speed_sender(tx);
let collector = Collector::File(file_info);
let handle = tokio::spawn(async move {
while let Some(speed) = rx.recv().await {
println!("Download Speed: {} kB/s", speed.as_bytes_per_sec());
}
});
let request = Request::builder()
.uri("<SOURCE URL>")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.request(request).unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
handle.abort();
}
use std::{fs, path::PathBuf};
use async_curl::CurlActor;
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use tokio::sync::mpsc::channel;
use url::Url;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let (tx, mut rx) = channel(1);
let file_to_be_uploaded = PathBuf::from("<FILE PATH TO BE UPLOADED>");
let file_size = fs::metadata(file_to_be_uploaded.as_path()).unwrap().len() as usize;
let actor = CurlActor::new();
let file_info = FileInfo::path(file_to_be_uploaded).with_transfer_speed_sender(tx);
let collector = Collector::File(file_info);
let handle = tokio::spawn(async move {
while let Some(speed) = rx.recv().await {
println!("Upload Speed: {} kB/s", speed.as_bytes_per_sec());
}
});
let request = Request::builder()
.uri("<TARGET URL>")
.method(Method::PUT)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.upload_file_size(FileSize::from(file_size)).unwrap()
.request(request).unwrap()
.nonblocking(actor)
.perform()
.await.unwrap();
println!("Response: {:?}", response);
handle.abort();
}
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use url::Url;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let collector = Collector::Ram(Vec::new());
let request = Request::builder()
.uri("<SOURCE URL>")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.request(request)?
.blocking()
.perform()?;
println!("Response: {:?}", response);
Ok(())
}
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use url::Url;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let collector = Collector::Ram(Vec::new());
let request = Request::builder()
.uri("<TARGET URL>")
.method(Method::POST)
.body(Some("test body".as_bytes().to_vec()))
.unwrap();
let response = HttpClient::new(collector)
.request(request)?
.blocking()
.perform()?;
println!("Response: {:?}", response);
Ok(())
}
use std::path::PathBuf;
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use url::Url;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let collector = Collector::File(FileInfo::path(PathBuf::from("<FILE PATH TO SAVE>")));
let request = Request::builder()
.uri("<SOURCE URL>")
.method(Method::GET)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.request(request)?
.blocking()
.perform()?;
println!("Response: {:?}", response);
Ok(())
}
use std::{fs, path::PathBuf};
use curl_http_client::*;
use http::{HeaderMap, Method, Request};
use url::Url;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file_to_be_uploaded = PathBuf::from("<FILE PATH TO BE UPLOADED>");
let file_size = fs::metadata(file_to_be_uploaded.as_path()).unwrap().len() as usize;
let collector = Collector::File(FileInfo::path(file_to_be_uploaded));
let request = Request::builder()
.uri("<TARGET URL>")
.method(Method::PUT)
.body(None)
.unwrap();
let response = HttpClient::new(collector)
.upload_file_size(FileSize::from(file_size))?
.request(request)?
.blocking()
.perform()?;
println!("Response: {:?}", response);
Ok(())
}