1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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
//! The client implementation for the reqwest HTTP client, which is async by
//! default.
use super::{BaseHttpClient, Form, Headers, Query};
use std::convert::TryInto;
use std::time::Duration;
use maybe_async::async_impl;
use reqwest::{Method, RequestBuilder};
use serde_json::Value;
/// Custom enum that contains all the possible errors that may occur when using
/// [`reqwest`].
///
/// Sample usage:
///
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// use rusty_box::http_client::{HttpError, HttpClient, BaseHttpClient};
///
/// let client = HttpClient::default();
/// let response = client.get("wrongurl", None, &Default::default()).await;
/// match response {
/// Ok(data) => println!("request succeeded: {:?}", data),
/// Err(HttpError::Client(e)) => eprintln!("request failed: {}", e),
/// Err(HttpError::StatusCode(response)) => {
/// let code = response.status().as_u16();
/// match response.json::<rusty_box::rest_api::api::models::client_error::ClientError>().await {
/// Ok(api_error) => eprintln!("status code {}: {:?}", code, api_error),
/// Err(_) => eprintln!("status code {}", code),
/// }
/// },
/// }
/// # }
/// ```
#[derive(thiserror::Error, Debug)]
pub enum ReqwestError {
/// The request couldn't be completed because there was an error when trying
/// to do so
#[error("request: {0}")]
Client(#[from] reqwest::Error),
/// The request was made, but the server returned an unsuccessful status
/// code, such as 404 or 503. In some cases, the response may contain a
/// custom message from Spotify with more information, which can be
/// serialized into `rusty-box::ApiError`.
#[error("status code {}", reqwest::Response::status(.0))]
StatusCode(reqwest::Response),
}
#[derive(Debug, Clone)]
pub struct ReqwestClient {
/// reqwest needs an instance of its client to perform requests.
client: reqwest::Client,
}
impl Default for ReqwestClient {
fn default() -> Self {
let client = reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(10))
.build()
// building with these options cannot fail
.unwrap();
Self { client }
}
}
impl ReqwestClient {
async fn request<D>(
&self,
method: Method,
url: &str,
headers: Option<&Headers>,
add_data: D,
) -> Result<String, ReqwestError>
where
D: Fn(RequestBuilder) -> RequestBuilder,
{
let mut request = self.client.request(method.clone(), url);
// Setting the headers, if any
if let Some(headers) = headers {
// The headers need to be converted into a `reqwest::HeaderMap`,
// which won't fail as long as its contents are ASCII. This is an
// internal function, so the condition cannot be broken by the user
// and will always be true.
//
// The content-type header will be set automatically.
let headers = headers.try_into().unwrap();
request = request.headers(headers);
}
// Configuring the request for the specific type (get/post/put/delete)
request = add_data(request);
// Finally performing the request and handling the response
log::info!("Making request {:?}", request);
let response = request.send().await?;
// Making sure that the status code is OK
if response.status().is_success() {
response.text().await.map_err(Into::into)
} else {
Err(ReqwestError::StatusCode(response))
}
}
}
#[async_impl]
impl BaseHttpClient for ReqwestClient {
type Error = ReqwestError;
#[inline]
async fn get(
&self,
url: &str,
headers: Option<&Headers>,
query: &Query,
) -> Result<String, Self::Error> {
self.request(Method::GET, url, headers, |req| req.query(query))
.await
}
#[inline]
async fn post(
&self,
url: &str,
headers: Option<&Headers>,
// query: &Query,
payload: &Value,
) -> Result<String, Self::Error> {
self.request(Method::POST, url, headers, |req| req.json(payload))
.await
}
#[inline]
async fn post_form(
&self,
url: &str,
headers: Option<&Headers>,
payload: &Form<'_>,
) -> Result<String, Self::Error> {
self.request(Method::POST, url, headers, |req| req.form(payload))
.await
}
#[inline]
async fn put(
&self,
url: &str,
headers: Option<&Headers>,
payload: &Value,
) -> Result<String, Self::Error> {
self.request(Method::PUT, url, headers, |req| req.json(payload))
.await
}
#[inline]
async fn delete(
&self,
url: &str,
headers: Option<&Headers>,
payload: &Value,
) -> Result<String, Self::Error> {
self.request(Method::DELETE, url, headers, |req| req.json(payload))
.await
}
// TODO: implement method OPTION for reqwest
}