Struct simplist::HttpClient
[−]
[src]
pub struct HttpClient { /* fields omitted */ }
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\"> ..."
Methods
impl HttpClient
[src]
fn new(remote: Remote) -> HttpClient
[src]
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);
fn request(
&self,
request: HttpRequest
) -> Box<Future<Item = HttpResponse, Error = HttpError>>
[src]
&self,
request: HttpRequest
) -> Box<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);
impl HttpClient
[src]
fn options<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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, ...]);
fn options_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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\"> ...");
impl HttpClient
[src]
fn get<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
sends an asynchronous http request, returning the response as a
Vec<u8>
.
examples
use simplist::HttpClient; let http = HttpClient::new(handle); assert_eq!( &await http.get("https://hinaria.com")?, &[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);
fn get_string<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
impl HttpClient
[src]
fn post<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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, ...]);
fn post_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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\"> ...");
impl HttpClient
[src]
fn put<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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, ...]);
fn put_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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\"> ...");
impl HttpClient
[src]
fn delete<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
sends an asynchronous http request, returning the response as a
Vec<u8>
.
examples
use simplist::HttpClient; let http = HttpClient::new(handle); assert_eq!( &await http.get("https://hinaria.com")?, &[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);
fn delete_string<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
impl HttpClient
[src]
fn head<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
sends an asynchronous http request, returning the response as a
Vec<u8>
.
examples
use simplist::HttpClient; let http = HttpClient::new(handle); assert_eq!( &await http.get("https://hinaria.com")?, &[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);
fn head_string<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
impl HttpClient
[src]
fn trace<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
sends an asynchronous http request, returning the response as a
Vec<u8>
.
examples
use simplist::HttpClient; let http = HttpClient::new(handle); assert_eq!( &await http.get("https://hinaria.com")?, &[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);
fn trace_string<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
impl HttpClient
[src]
fn connect<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
sends an asynchronous http request, returning the response as a
Vec<u8>
.
examples
use simplist::HttpClient; let http = HttpClient::new(handle); assert_eq!( &await http.get("https://hinaria.com")?, &[60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, ...]);
fn connect_string<TUrl>(
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
[src]
&self,
url: TUrl
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
impl HttpClient
[src]
fn patch<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = Vec<u8>, Error = HttpError>>, FutureResult<Vec<u8>, HttpError>, Vec<u8>, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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, ...]);
fn patch_string<TUrl, TContent>(
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
[src]
&self,
url: TUrl,
content: Option<TContent>
) -> OneOfFuture<Box<Future<Item = String, Error = HttpError>>, FutureResult<String, HttpError>, String, HttpError> where
TUrl: IntoUrl,
TContent: Into<HttpContent>,
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
impl Clone for HttpClient
[src]
fn clone(&self) -> HttpClient
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more