Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeitlin11 committed Apr 5, 2024
1 parent d295678 commit d8adc6e
Show file tree
Hide file tree
Showing 1,096 changed files with 119,509 additions and 32,269 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/async-stripe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ jobs:
- name: Run clippy
run: cargo clippy --features "runtime-${{ matrix.runtime }} full"

- name: Run clippy with min-ser
run: cargo clippy --features "runtime-${{ matrix.runtime }} full min-ser"

test:
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -129,6 +132,9 @@ jobs:

- uses: Swatinem/rust-cache@v2

- name: Test with min-ser
run: cargo test --features runtime-${{ matrix.runtime }} min-ser

- uses: taiki-e/install-action@cargo-llvm-cov
- name: Test and gather coverage
run: cargo llvm-cov --lcov --output-path lcov.info --features runtime-${{ matrix.runtime }}
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ edition = "2021"
serde = { version = ">=1.0.79", features = ["derive"] } # we use `serde(other)` which was introduced in 1.0.79
http-types = { version = "2.12.0", default-features = false }
smol_str = { version = "0.2.0", features = ["serde"] }
serde_json = "1.0"
serde_json = "1.0"
miniserde = "0.1.34"
11 changes: 6 additions & 5 deletions async-stripe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ hyper-rustls-native = ["hyper-rustls", "hyper-rustls/native-tokio"]
hyper-rustls-webpki = ["hyper-rustls", "hyper-rustls/webpki-tokio"]

[dependencies]
stripe_types = {path = "../stripe_types"}
stripe_shared = {path = "../generated/stripe_shared"}
async-std = {version = "1.8,<1.11", optional = true}
stripe_types = { path = "../stripe_types" }
stripe_shared = { path = "../generated/stripe_shared" }
async-std = { version = "1.8,<1.11", optional = true }

thiserror = "1.0.24"
hyper = { version = "0.14", default-features = false, features = ["http1", "http2", "client", "tcp"], optional = true }
Expand All @@ -91,7 +91,7 @@ serde_qs = "0.12.0"
serde_path_to_error = "0.1.8"
surf = { version = "2.1", optional = true }
tokio = { version = "1.2", optional = true }
uuid = { version = "1.6.1", optional=true, features=["v4"] }
uuid = { version = "1.6.1", optional = true, features = ["v4"] }

serde.workspace = true
http-types.workspace = true
Expand All @@ -103,4 +103,5 @@ futures-util = { version = "0.3.21", optional = true }
[dev-dependencies]
httpmock = "0.6.7"
async-std = { version = "1.10.0", features = ["attributes"] }
tokio = { version = "1.24.1", features = ["rt", "macros"] }
tokio = { version = "1.24.1", features = ["rt", "macros"] }
miniserde.workspace = true
21 changes: 14 additions & 7 deletions async-stripe/src/client/base/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::pin::Pin;

use async_std::task::sleep;
use http_types::{Request, StatusCode};
use serde::de::DeserializeOwned;
use stripe_types::StripeDeserialize;

use crate::client::request_strategy::{Outcome, RequestStrategy};
use crate::error::StripeError;
Expand All @@ -26,7 +26,7 @@ impl AsyncStdClient {
Self { client: surf::Client::new() }
}

pub fn execute<T: DeserializeOwned + Send + 'static>(
pub fn execute<T: StripeDeserialize + Send + 'static>(
&self,
request: Request,
strategy: &RequestStrategy,
Expand All @@ -38,8 +38,9 @@ impl AsyncStdClient {

Box::pin(async move {
let bytes = send_inner(&client, request, &strategy).await?;
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
serde_path_to_error::deserialize(json_deserializer).map_err(StripeError::from)
let str = std::str::from_utf8(bytes.as_ref())
.map_err(|_| StripeError::JSONDeserialize("Response was not valid UTF-8".into()))?;
T::deserialize(str).map_err(StripeError::JSONDeserialize)
})
}
}
Expand Down Expand Up @@ -100,10 +101,16 @@ async fn send_inner(

if !status.is_success() {
tries += 1;
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
last_error = serde_path_to_error::deserialize(json_deserializer)
let str = std::str::from_utf8(bytes.as_ref()).map_err(|_| {
StripeError::JSONDeserialize("Response was not valid UTF-8".into())

Check warning on line 105 in async-stripe/src/client/base/async_std.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/base/async_std.rs#L105

Added line #L105 was not covered by tests
})?;
last_error = stripe_shared::Error::deserialize(str)
.map(|e: stripe_shared::Error| StripeError::Stripe(*e.error, status.into()))
.unwrap_or_else(StripeError::from);
.unwrap_or_else(|_| {
StripeError::JSONDeserialize(
"Could not deserialize Stripe error".into(),
)
});
last_status = Some(status);
last_retry_header = retry;

Expand Down
28 changes: 17 additions & 11 deletions async-stripe/src/client/base/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::pin::Pin;
use http_types::{Request, StatusCode};
use hyper::http;
use hyper::{client::HttpConnector, Body};
use serde::de::DeserializeOwned;
use stripe_types::StripeDeserialize;
use tokio::time::sleep;

use crate::client::request_strategy::{Outcome, RequestStrategy};
Expand Down Expand Up @@ -77,7 +77,7 @@ impl TokioClient {
}
}

pub fn execute<T: DeserializeOwned + Send + 'static>(
pub fn execute<T: StripeDeserialize + Send + 'static>(
&self,
request: Request,
strategy: &RequestStrategy,
Expand All @@ -89,8 +89,9 @@ impl TokioClient {

Box::pin(async move {
let bytes = send_inner(&client, request, &strategy).await?;
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
serde_path_to_error::deserialize(json_deserializer).map_err(StripeError::from)
let str = std::str::from_utf8(bytes.as_ref())
.map_err(|_| StripeError::JSONDeserialize("Response was not valid UTF-8".into()))?;
T::deserialize(str).map_err(StripeError::JSONDeserialize)
})
}
}
Expand Down Expand Up @@ -153,12 +154,18 @@ async fn send_inner(

if !status.is_success() {
tries += 1;
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
last_error = serde_path_to_error::deserialize(json_deserializer)
let str = std::str::from_utf8(bytes.as_ref()).map_err(|_| {
StripeError::JSONDeserialize("Response was not valid UTF-8".into())

Check warning on line 158 in async-stripe/src/client/base/tokio.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/base/tokio.rs#L158

Added line #L158 was not covered by tests
})?;
last_error = stripe_shared::Error::deserialize(str)
.map(|e: stripe_shared::Error| {
StripeError::Stripe(*e.error, status.as_u16())
})
.unwrap_or_else(StripeError::from);
.unwrap_or_else(|_| {
StripeError::JSONDeserialize(
"Could not deserialize Stripe error".into(),
)
});
last_status = Some(
// NOTE: StatusCode::from can panic here, so fall back to InternalServerError
// see https://github.com/http-rs/http-types/blob/ac5d645ce5294554b86ebd49233d3ec01665d1d7/src/hyperium_http.rs#L20-L24
Expand Down Expand Up @@ -302,7 +309,7 @@ mod tests {
async fn nice_serde_error() {
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, miniserde::Deserialize)]
struct DataType {
// Allowing dead code since used for deserialization
#[allow(dead_code)]
Expand Down Expand Up @@ -333,9 +340,8 @@ mod tests {
mock.assert_hits_async(1).await;

match res {
Err(StripeError::JSONSerialize(err)) => {
println!("Error: {:?} Path: {:?}", err.inner(), err.path().to_string())
}
Err(StripeError::JSONSerialize(_)) => {}

Check warning on line 343 in async-stripe/src/client/base/tokio.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/base/tokio.rs#L343

Added line #L343 was not covered by tests
Err(StripeError::JSONDeserialize(_)) => {}
_ => panic!("Expected stripe error {:?}", res),
}
}
Expand Down
4 changes: 2 additions & 2 deletions async-stripe/src/client/base/tokio_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{sync::Arc, time::Duration};

use http_types::Request;
use serde::de::DeserializeOwned;
use stripe_types::StripeDeserialize;

use crate::client::base::tokio::TokioClient;
use crate::client::request_strategy::RequestStrategy;
Expand Down Expand Up @@ -39,7 +39,7 @@ impl TokioBlockingClient {
TokioBlockingClient { inner, runtime: Arc::new(runtime) }
}

pub fn execute<T: DeserializeOwned + Send + 'static>(
pub fn execute<T: StripeDeserialize + Send + 'static>(
&self,
request: Request,
strategy: &RequestStrategy,
Expand Down
18 changes: 9 additions & 9 deletions async-stripe/src/client/stripe.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Necessary under tokio-blocking since `Response` is a type alias to a `Result`
#![allow(clippy::missing_errors_doc)]
use http_types::{Body, Method, Request, Url};
use serde::de::DeserializeOwned;
use serde::Serialize;
use stripe_shared::version::VERSION;
use stripe_types::StripeDeserialize;

use crate::client::headers::{AppInfo, Headers};
use crate::{
Expand Down Expand Up @@ -86,12 +86,12 @@ impl Client {
}

/// Make a `GET` http request with just a path
pub fn get<T: DeserializeOwned + Send + 'static>(&self, path: &str) -> Response<T> {
pub fn get<T: StripeDeserialize + Send + 'static>(&self, path: &str) -> Response<T> {

Check warning on line 89 in async-stripe/src/client/stripe.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/stripe.rs#L89

Added line #L89 was not covered by tests
self.send(path, Method::Get)
}

/// Make a `GET` http request with url query parameters
pub fn get_query<T: DeserializeOwned + Send + 'static, P: Serialize>(
pub fn get_query<T: StripeDeserialize + Send + 'static, P: Serialize>(
&self,
path: &str,
params: P,
Expand All @@ -103,7 +103,7 @@ impl Client {
self.client.execute::<T>(self.create_request(Method::Get, url), &self.strategy)
}

pub fn send<T: DeserializeOwned + Send + 'static>(
pub fn send<T: StripeDeserialize + Send + 'static>(

Check warning on line 106 in async-stripe/src/client/stripe.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/stripe.rs#L106

Added line #L106 was not covered by tests
&self,
path: &str,
method: Method,
Expand All @@ -113,17 +113,17 @@ impl Client {
}

/// Make a `DELETE` http request with just a path
pub fn delete<T: DeserializeOwned + Send + 'static>(&self, path: &str) -> Response<T> {
pub fn delete<T: StripeDeserialize + Send + 'static>(&self, path: &str) -> Response<T> {

Check warning on line 116 in async-stripe/src/client/stripe.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/stripe.rs#L116

Added line #L116 was not covered by tests
self.send(path, Method::Delete)
}

/// Make a `POST` http request with just a path
pub fn post<T: DeserializeOwned + Send + 'static>(&self, path: &str) -> Response<T> {
pub fn post<T: StripeDeserialize + Send + 'static>(&self, path: &str) -> Response<T> {

Check warning on line 121 in async-stripe/src/client/stripe.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/stripe.rs#L121

Added line #L121 was not covered by tests
self.send(path, Method::Post)
}

/// Make a `POST` http request with urlencoded body
pub fn post_form<T: DeserializeOwned + Send + 'static, F: Serialize>(
pub fn post_form<T: StripeDeserialize + Send + 'static, F: Serialize>(

Check warning on line 126 in async-stripe/src/client/stripe.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/stripe.rs#L126

Added line #L126 was not covered by tests
&self,
path: &str,
form: F,
Expand All @@ -132,7 +132,7 @@ impl Client {
}

/// Make a `DELETE` http request with urlencoded body
pub fn delete_form<T: DeserializeOwned + Send + 'static, F: Serialize>(
pub fn delete_form<T: StripeDeserialize + Send + 'static, F: Serialize>(

Check warning on line 135 in async-stripe/src/client/stripe.rs

View check run for this annotation

Codecov / codecov/patch

async-stripe/src/client/stripe.rs#L135

Added line #L135 was not covered by tests
&self,
path: &str,
form: F,
Expand All @@ -141,7 +141,7 @@ impl Client {
}

/// Make an http request with urlencoded body
pub fn send_form<T: DeserializeOwned + Send + 'static, F: Serialize>(
pub fn send_form<T: StripeDeserialize + Send + 'static, F: Serialize>(
&self,
path: &str,
form: F,
Expand Down
2 changes: 2 additions & 0 deletions async-stripe/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum StripeError {
QueryStringSerialize(#[from] serde_path_to_error::Error<serde_qs::Error>),
#[error("error serializing or deserializing a request")]
JSONSerialize(#[from] serde_path_to_error::Error<serde_json::Error>),
#[error("error serializing a request")]
JSONDeserialize(String),
#[error("attempted to access an unsupported version of the api")]
UnsupportedVersion,
#[error("error communicating with stripe: {0}")]
Expand Down
5 changes: 0 additions & 5 deletions crate_info.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
| [Topup](https://stripe.com/docs/api/topups/object) | stripe_connect | topup |
| [TransferReversal](https://stripe.com/docs/api/transfer_reversals/object) | stripe_connect | transfer_reversal |
| [Transfer](https://stripe.com/docs/api/transfers/object) | stripe_connect | transfer |
| CustomerSession | stripe_core | customer_session |
| PaymentSource | stripe_core | payment_source |
| [BalanceTransaction](https://stripe.com/docs/api/balance_transactions/object) | stripe_core | balance_transaction |
| [Balance](https://stripe.com/docs/api/balance/balance_object) | stripe_core | balance |
Expand Down Expand Up @@ -62,12 +61,8 @@
| [IssuingToken](https://stripe.com/docs/api/issuing/tokens/object) | stripe_issuing | issuing_token |
| [IssuingTransaction](https://stripe.com/docs/api/issuing/transactions/object) | stripe_issuing | issuing_transaction |
| ApplePayDomain | stripe_misc | apple_pay_domain |
| ClimateOrder | stripe_misc | climate_order |
| ClimateProduct | stripe_misc | climate_product |
| ClimateSupplier | stripe_misc | climate_supplier |
| EphemeralKey | stripe_misc | ephemeral_key |
| ExchangeRate | stripe_misc | exchange_rate |
| FinancialConnectionsTransaction | stripe_misc | financial_connections_transaction |
| TaxRegistration | stripe_misc | tax_registration |
| [FinancialConnectionsAccount](https://stripe.com/docs/api/financial_connections/accounts/object) | stripe_misc | financial_connections_account |
| [FinancialConnectionsSession](https://stripe.com/docs/api/financial_connections/sessions/object) | stripe_misc | financial_connections_session |
Expand Down
Loading

0 comments on commit d8adc6e

Please sign in to comment.