pub struct HttpClient { /* private fields */ }Expand description
an asynchronous, futures-based http client.
a class for sending http requests and receiving http responses from a resource identified by a uri. this class provides a simple, asynchronous, futures-based api.
if you’d like to use a synchronous, blocking api instead, HttpSyncClient may be
easier to use. alternatively, you can emulate a blocking api on this struct by synchronously waiting for future
results by calling wait() on all returned futures.
all examples here assume that you have an existing tokio reactor you can use. if you not already have a tokio core, a complete example demonstrating the initialization and usage of tokio together with this http client is available at https://github.com/hinaria/simplist/blob/master/examples/basic-async.rs.
§nightly
if you’re using a nightly compiler, you can significantly reduce the number of allocations that simplist performs by
enabling the nightly feature. refer to the module-level docs for more information.
§methods
this struct provides two sets of operations for performing http operations: one for returning the response as a
Vec<u8>, and another for returning the response as a
String.
finally, there is a separate method, fn request(...) that takes a
HttpRequest and allows you to manually set the http method, as well as set http
headers.
rustdoc currently generates some pretty unreadable documentation for this struct, so we’ll summarize the available methods here.
§methods returning a Vec<u8>:
.
fn options(url, Option<body>) -> Future<Item = Vec<u8>, Error = HttpError>;
fn get (url) -> Future<Item = Vec<u8>, Error = HttpError>;
fn post (url, Option<body>) -> Future<Item = Vec<u8>, Error = HttpError>;
fn put (url, Option<body>) -> Future<Item = Vec<u8>, Error = HttpError>;
fn delete (url) -> Future<Item = Vec<u8>, Error = HttpError>;
fn head (url) -> Future<Item = Vec<u8>, Error = HttpError>;
fn trace (url) -> Future<Item = Vec<u8>, Error = HttpError>;
fn connect(url) -> Future<Item = Vec<u8>, Error = HttpError>;
fn patch (url, Option<body>) -> Future<Item = Vec<u8>, Error = HttpError>;§methods returning a String<u8>:
.
fn options_string(url, Option<body>) -> Future<Item = String, Error = HttpError>;
fn get_string (url) -> Future<Item = String, Error = HttpError>;
fn post_string (url, Option<body>) -> Future<Item = String, Error = HttpError>;
fn put_string (url, Option<body>) -> Future<Item = String, Error = HttpError>;
fn delete_string (url) -> Future<Item = String, Error = HttpError>;
fn head_string (url) -> Future<Item = String, Error = HttpError>;
fn trace_string (url) -> Future<Item = String, Error = HttpError>;
fn connect_string(url) -> Future<Item = String, Error = HttpError>;
fn patch_string (url, Option<body>) -> Future<Item = String, Error = HttpError>;§examples
§asynchronous, with await notation.
use simplist::HttpClient;
let http = HttpClient::new(handle);
let html = await http.get_string("https://hinaria.com")?;
println!("{:?}", html);
// => "<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ..."§asynchronous, with future callbacks.
use simplist::HttpClient;
let http = HttpClient::new(handle);
let future = http.get_string("https://hinaria.com").and_then(|html| {
println!("{:?}", html);
Ok(())
}).map_err(|_| ());
handle.spawn(future);
// => "<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ..."§synchronous.
use simplist::HttpSyncClient;
let http = HttpSyncClient::new(handle);
let html = http.get_string("https://hinaria.com")?;
println!("{:?}", html);
// => "<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ..."Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new(remote: Remote) -> HttpClient
pub fn new(remote: Remote) -> HttpClient
creates a new http client.
this method is very cheap, and does not perform any kind of initialization.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);Sourcepub fn request(
&self,
request: HttpRequest,
) -> Box<dyn Future<Item = HttpResponse, Error = HttpError>>
pub fn request( &self, request: HttpRequest, ) -> Box<dyn Future<Item = HttpResponse, Error = HttpError>>
sends a http request as an asynchronous operation.
§examples
use simplist::HttpClient;
use simplist::HttpMethod;
let http = HttpClient::new(handle);
let url = "https://hinaria.com".parse()?;
let operation = HttpRequest::with(url, HttpMethod::Get);
let response = await http.request(operation)?;
assert_eq!(response.status(), StatusCode::Ok);Source§impl HttpClient
impl HttpClient
Sourcepub fn options<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
pub fn options<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
sends an asynchronous http request, returning the response as a
Vec<u8>.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post("https://hinaria.com/users/@me/database", body)?,
&[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);Sourcepub fn options_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
pub fn options_string<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
sends an asynchronous http request, returning the response as a
String.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post_string("https://hinaria.com/users/@me/database", body)?,
"<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ...");Source§impl HttpClient
impl HttpClient
Sourcepub fn get<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
pub fn get<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
Source§impl HttpClient
impl HttpClient
Sourcepub fn post<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
pub fn post<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
sends an asynchronous http request, returning the response as a
Vec<u8>.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post("https://hinaria.com/users/@me/database", body)?,
&[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);Sourcepub fn post_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
pub fn post_string<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
sends an asynchronous http request, returning the response as a
String.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post_string("https://hinaria.com/users/@me/database", body)?,
"<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ...");Source§impl HttpClient
impl HttpClient
Sourcepub fn put<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
pub fn put<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
sends an asynchronous http request, returning the response as a
Vec<u8>.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post("https://hinaria.com/users/@me/database", body)?,
&[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);Sourcepub fn put_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
pub fn put_string<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
sends an asynchronous http request, returning the response as a
String.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post_string("https://hinaria.com/users/@me/database", body)?,
"<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ...");Source§impl HttpClient
impl HttpClient
Sourcepub fn delete<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
pub fn delete<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
Source§impl HttpClient
impl HttpClient
Sourcepub fn head<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
pub fn head<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
Source§impl HttpClient
impl HttpClient
Sourcepub fn trace<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
pub fn trace<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
Source§impl HttpClient
impl HttpClient
Sourcepub fn connect<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
pub fn connect<TUrl>(
&self,
url: TUrl,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>where
TUrl: IntoUrl,
Source§impl HttpClient
impl HttpClient
Sourcepub fn patch<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
pub fn patch<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError>
sends an asynchronous http request, returning the response as a
Vec<u8>.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post("https://hinaria.com/users/@me/database", body)?,
&[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);Sourcepub fn patch_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>,
) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
pub fn patch_string<TUrl, TContent>( &self, url: TUrl, content: Option<TContent>, ) -> OneOfFuture<Box<dyn Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError>
sends an asynchronous http request, returning the response as a
String.
the request body can be any type that is convertible to a HttpContent.
refer to HttpContent to see a list of types that can be used.
§examples
use simplist::HttpClient;
let http = HttpClient::new(handle);
let body = Some("{ id: 1234 }");
assert_eq!(
&await http.post_string("https://hinaria.com/users/@me/database", body)?,
"<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ...");Trait Implementations§
Source§impl Clone for HttpClient
impl Clone for HttpClient
Source§fn clone(&self) -> HttpClient
fn clone(&self) -> HttpClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more