Struct generic_api_client::http::Client
source · pub struct Client { /* private fields */ }
Expand description
Client for communicating with APIs through HTTP/HTTPS.
When making a HTTP request or starting a websocket connection with this client, a handler that implements RequestHandler is required.
Implementations§
source§impl Client
impl Client
sourcepub async fn request<Q, B, H>(
&self,
method: Method,
url: &str,
query: Option<&Q>,
body: Option<B>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
Q: Serialize + ?Sized,
H: RequestHandler<B>,
pub async fn request<Q, B, H>(
&self,
method: Method,
url: &str,
query: Option<&Q>,
body: Option<B>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
Q: Serialize + ?Sized,
H: RequestHandler<B>,
Makes an HTTP request with the given RequestHandler and returns the response.
It is recommended to use methods like get() because this method takes many type parameters and parameters.
The request is passed to handler
before being sent, and the response is passed to handler
before being returned.
Note, that as stated in the docs for RequestBuilder::query(), parameter query
only accepts a sequence of key-value pairs.
Examples found in repository?
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
pub async fn get<Q, H>(&self, url: &str, query: Option<&Q>, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
Q: Serialize + ?Sized,
H: RequestHandler<()>,
{
self.request::<Q, (), H>(Method::GET, url, query, None, handler).await
}
/// Makes an GET request with the given [RequestHandler], without queries.
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn get_no_query<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
H: RequestHandler<()>,
{
self.request::<&[(&str, &str)], (), H>(Method::GET, url, None, None, handler).await
}
/// Makes an POST request with the given [RequestHandler].
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn post<B, H>(&self, url: &str, body: Option<B>, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
H: RequestHandler<B>,
{
self.request::<(), B, H>(Method::POST, url, None, body, handler).await
}
/// Makes an POST request with the given [RequestHandler], without a body.
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn post_no_body<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
H: RequestHandler<()>,
{
self.request::<(), (), H>(Method::POST, url, None, None, handler).await
}
/// Makes an PUT request with the given [RequestHandler].
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn put<B, H>(&self, url: &str, body: Option<B>, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
H: RequestHandler<B>,
{
self.request::<(), B, H>(Method::PUT, url, None, body, handler).await
}
/// Makes an PUT request with the given [RequestHandler], without a body.
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn put_no_body<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
H: RequestHandler<()>,
{
self.request::<(), (), H>(Method::PUT, url, None, None, handler).await
}
/// Makes an DELETE request with the given [RequestHandler].
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn delete<Q, H>(&self, url: &str, query: Option<&Q>, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
Q: Serialize + ?Sized,
H: RequestHandler<()>,
{
self.request::<Q, (), H>(Method::DELETE, url, query, None, handler).await
}
/// Makes an DELETE request with the given [RequestHandler], without queries.
///
/// This method just calls [request()][Self::request()]. It requires less typing for type parameters and parameters.
/// This method requires that `handler` can handle a request with a body of type `()`. The actual body passed will be `None`.
///
/// For more information, see [request()][Self::request()].
#[inline(always)]
pub async fn delete_no_query<H>(&self, url: &str, handler: &H) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>
where
H: RequestHandler<()>,
{
self.request::<&[(&str, &str)], (), H>(Method::DELETE, url, None, None, handler).await
}
sourcepub async fn get<Q, H>(
&self,
url: &str,
query: Option<&Q>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
Q: Serialize + ?Sized,
H: RequestHandler<()>,
pub async fn get<Q, H>(
&self,
url: &str,
query: Option<&Q>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
Q: Serialize + ?Sized,
H: RequestHandler<()>,
Makes an GET request with the given RequestHandler.
This method just calls request(). It requires less typing for type parameters and parameters.
This method requires that handler
can handle a request with a body of type ()
. The actual body passed will be None
.
For more information, see request().
sourcepub async fn get_no_query<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
pub async fn get_no_query<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
Makes an GET request with the given RequestHandler, without queries.
This method just calls request(). It requires less typing for type parameters and parameters.
This method requires that handler
can handle a request with a body of type ()
. The actual body passed will be None
.
For more information, see request().
sourcepub async fn post<B, H>(
&self,
url: &str,
body: Option<B>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<B>,
pub async fn post<B, H>(
&self,
url: &str,
body: Option<B>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<B>,
Makes an POST request with the given RequestHandler.
This method just calls request(). It requires less typing for type parameters and parameters.
For more information, see request().
sourcepub async fn post_no_body<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
pub async fn post_no_body<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
Makes an POST request with the given RequestHandler, without a body.
This method just calls request(). It requires less typing for type parameters and parameters.
This method requires that handler
can handle a request with a body of type ()
. The actual body passed will be None
.
For more information, see request().
sourcepub async fn put<B, H>(
&self,
url: &str,
body: Option<B>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<B>,
pub async fn put<B, H>(
&self,
url: &str,
body: Option<B>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<B>,
Makes an PUT request with the given RequestHandler.
This method just calls request(). It requires less typing for type parameters and parameters.
For more information, see request().
sourcepub async fn put_no_body<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
pub async fn put_no_body<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
Makes an PUT request with the given RequestHandler, without a body.
This method just calls request(). It requires less typing for type parameters and parameters.
This method requires that handler
can handle a request with a body of type ()
. The actual body passed will be None
.
For more information, see request().
sourcepub async fn delete<Q, H>(
&self,
url: &str,
query: Option<&Q>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
Q: Serialize + ?Sized,
H: RequestHandler<()>,
pub async fn delete<Q, H>(
&self,
url: &str,
query: Option<&Q>,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
Q: Serialize + ?Sized,
H: RequestHandler<()>,
Makes an DELETE request with the given RequestHandler.
This method just calls request(). It requires less typing for type parameters and parameters.
This method requires that handler
can handle a request with a body of type ()
. The actual body passed will be None
.
For more information, see request().
sourcepub async fn delete_no_query<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
pub async fn delete_no_query<H>(
&self,
url: &str,
handler: &H
) -> Result<H::Successful, RequestError<H::BuildError, H::Unsuccessful>>where
H: RequestHandler<()>,
Makes an DELETE request with the given RequestHandler, without queries.
This method just calls request(). It requires less typing for type parameters and parameters.
This method requires that handler
can handle a request with a body of type ()
. The actual body passed will be None
.
For more information, see request().