#![warn(clippy::unwrap_used)]
use std::future::{self, Future};
use std::pin::Pin;
use serde::de::DeserializeOwned;
use surf::{Body, Url};
use crate::error::{ErrorResponse, StripeError};
use crate::params::{AppInfo, Headers};
use crate::resources::ApiVersion;
pub type Response<T> = Pin<Box<dyn Future<Output = Result<T, StripeError>> + Send>>;
#[allow(dead_code)]
#[inline(always)]
pub(crate) fn ok<T: Send + 'static>(ok: T) -> Response<T> {
Box::pin(future::ready(Ok(ok)))
}
#[allow(dead_code)]
#[inline(always)]
pub(crate) fn err<T: Send + 'static>(err: StripeError) -> Response<T> {
Box::pin(future::ready(Err(err)))
}
#[derive(Clone)]
pub struct Client {
host: Url,
api_root: String,
client: surf::Client,
secret_key: String,
headers: Headers,
app_info: Option<AppInfo>,
}
impl Client {
pub fn new(secret_key: impl Into<String>) -> Client {
Client::from_url("https://api.stripe.com/", secret_key)
}
pub fn from_url<'a>(scheme_host: impl Into<&'a str>, secret_key: impl Into<String>) -> Client {
let host = Url::parse(scheme_host.into()).expect("invalid url");
let client = surf::Client::new();
let headers =
Headers { stripe_version: Some(ApiVersion::V2020_08_27), ..Default::default() };
Client {
host,
api_root: "v1".to_string(),
client,
secret_key: secret_key.into(),
headers,
app_info: Some(AppInfo::default()),
}
}
pub fn with_headers(&self, headers: Headers) -> Client {
let mut client = self.clone();
client.headers = headers;
client
}
pub fn set_app_info(&mut self, name: String, version: Option<String>, url: Option<String>) {
self.app_info = Some(AppInfo { name, url, version });
}
pub fn set_stripe_account<S: Into<String>>(&mut self, account_id: S) {
self.headers.stripe_account = Some(account_id.into());
}
pub fn get<T: DeserializeOwned + Send + 'static>(&self, path: &str) -> Response<T> {
let url = self.url(path);
let req = surf::Request::builder(surf::http::Method::Get, url).build();
send(&self.client, self.set_headers(req))
}
pub fn get_query<T: DeserializeOwned + Send + 'static, P: serde::Serialize>(
&self,
path: &str,
params: P,
) -> Response<T> {
let url = match self.url_with_params(path, params) {
Err(err) => return Box::pin(future::ready(Err(err))),
Ok(ok) => ok,
};
let req = surf::Request::builder(surf::http::Method::Get, url).build();
send(&self.client, self.set_headers(req))
}
pub fn delete<T: DeserializeOwned + Send + 'static>(&self, path: &str) -> Response<T> {
let url = self.url(path);
let req = surf::Request::builder(surf::http::Method::Delete, url).build();
send(&self.client, self.set_headers(req))
}
pub fn delete_query<T: DeserializeOwned + Send + 'static, P: serde::Serialize>(
&self,
path: &str,
params: P,
) -> Response<T> {
let url = match self.url_with_params(path, params) {
Err(err) => return Box::pin(future::ready(Err(err))),
Ok(ok) => ok,
};
let req = surf::Request::builder(surf::http::Method::Delete, url).build();
send(&self.client, self.set_headers(req))
}
pub fn post<T: DeserializeOwned + Send + 'static>(&self, path: &str) -> Response<T> {
let url = self.url(path);
let req = surf::Request::builder(surf::http::Method::Post, url).build();
send(&self.client, self.set_headers(req))
}
pub fn post_form<T: DeserializeOwned + Send + 'static, F: serde::Serialize>(
&self,
path: &str,
form: F,
) -> Response<T> {
let url = self.url(path);
let mut req = surf::Request::builder(surf::http::Method::Post, url)
.body(match serde_qs::to_string(&form) {
Err(err) => {
return Box::pin(future::ready(Err(StripeError::QueryStringSerialize(err))))
}
Ok(body) => Body::from_string(body),
})
.build();
req.set_header("content-type", "application/x-www-form-urlencoded");
send(&self.client, self.set_headers(req))
}
fn url(&self, path: &str) -> Url {
let mut url = self.host.clone();
url.set_path(&format!("{}/{}", self.api_root, path.trim_start_matches('/')));
url
}
fn url_with_params<P: serde::Serialize>(
&self,
path: &str,
params: P,
) -> Result<Url, StripeError> {
let mut url = self.url(path);
let params = serde_qs::to_string(¶ms).map_err(StripeError::from)?;
url.set_query(Some(¶ms));
Ok(url)
}
fn set_headers(&self, mut req: surf::Request) -> surf::Request {
req.set_header("authorization", &format!("Bearer {}", self.secret_key));
if let Some(account) = &self.headers.stripe_account {
req.set_header("stripe-account", account);
}
if let Some(client_id) = &self.headers.client_id {
req.set_header("client-id", client_id);
}
if let Some(stripe_version) = &self.headers.stripe_version {
req.set_header("stripe-version", stripe_version.as_str());
}
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
let user_agent: String = format!("Stripe/v3 RustBindings/{}", CRATE_VERSION);
if let Some(app_info) = &self.app_info {
let formatted: String = format_app_info(app_info);
let user_agent_app_info: String =
format!("{} {}", user_agent, formatted).trim().to_owned();
req.set_header("user-agent", user_agent_app_info.as_str());
} else {
req.set_header("user-agent", user_agent.as_str());
};
req
}
}
fn send<T: DeserializeOwned + Send + 'static>(
client: &surf::Client,
request: surf::Request,
) -> Response<T> {
let client = client.clone(); Box::pin(async move {
let bytes = send_inner(&client, request).await?;
serde_json::from_slice(&bytes).map_err(StripeError::from)
})
}
async fn send_inner(client: &surf::Client, request: surf::Request) -> Result<Vec<u8>, StripeError> {
let mut response = client.send(request).await?;
let status = response.status();
let bytes = response.body_bytes().await?;
if !status.is_success() {
Err(serde_json::from_slice(&bytes)
.map(|mut e: ErrorResponse| {
e.error.http_status = status.into();
StripeError::from(e.error)
})
.unwrap_or_else(StripeError::from))
} else {
Ok(bytes)
}
}
fn format_app_info(info: &AppInfo) -> String {
let formatted: String = match (&info.version, &info.url) {
(Some(a), Some(b)) => format!("{}/{} ({})", &info.name, a, b),
(Some(a), None) => format!("{}/{}", &info.name, a),
(None, Some(b)) => format!("{}/{}", &info.name, b),
_ => info.name.to_string(),
};
formatted
}