Skip to content

Commit a429ffa

Browse files
authored
Add cassette record/replay/passthrough modes in bdd runner (#4)
1 parent aa03ff8 commit a429ffa

17 files changed

+170
-175
lines changed

.generator/src/generator/templates/api_mod.j2

-96
This file was deleted.

.generator/src/generator/templates/common_mod.j2

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct ResponseContent<T> {
1111
#[derive(Debug)]
1212
pub enum Error<T> {
1313
Reqwest(reqwest::Error),
14+
ReqwestMiddleware(reqwest_middleware::Error),
1415
Serde(serde_json::Error),
1516
Io(std::io::Error),
1617
ResponseError(ResponseContent<T>),
@@ -20,6 +21,7 @@ impl <T> fmt::Display for Error<T> {
2021
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2122
let (module, e) = match self {
2223
Error::Reqwest(e) => ("reqwest", e.to_string()),
24+
Error::ReqwestMiddleware(e) => ("reqwest_middleware", e.to_string()),
2325
Error::Serde(e) => ("serde", e.to_string()),
2426
Error::Io(e) => ("IO", e.to_string()),
2527
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
@@ -32,6 +34,7 @@ impl <T: fmt::Debug> error::Error for Error<T> {
3234
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
3335
Some(match self {
3436
Error::Reqwest(e) => e,
37+
Error::ReqwestMiddleware(e) => e,
3538
Error::Serde(e) => e,
3639
Error::Io(e) => e,
3740
Error::ResponseError(_) => return None,
@@ -45,6 +48,12 @@ impl <T> From<reqwest::Error> for Error<T> {
4548
}
4649
}
4750

51+
impl<T> From<reqwest_middleware::Error> for Error<T> {
52+
fn from(e: reqwest_middleware::Error) -> Self {
53+
Error::ReqwestMiddleware(e)
54+
}
55+
}
56+
4857
impl <T> From<serde_json::Error> for Error<T> {
4958
fn from(e: serde_json::Error) -> Self {
5059
Error::Serde(e)

.generator/src/generator/templates/configuration.j2

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::env;
66
pub struct Configuration {
77
pub base_path: String,
88
pub user_agent: Option<String>,
9-
pub client: reqwest::Client,
9+
pub client: reqwest_middleware::ClientWithMiddleware,
1010
{%- set authMethods = openapi.security %}
1111
{%- if authMethods %}
1212
{%- for authMethod in authMethods %}
@@ -29,6 +29,7 @@ impl Configuration {
2929

3030
impl Default for Configuration {
3131
fn default() -> Self {
32+
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
3233
Configuration {
3334
base_path: "https://api.datadoghq.com".to_owned(),
3435
user_agent: Some(format!(
@@ -38,7 +39,7 @@ impl Default for Configuration {
3839
env::consts::OS,
3940
env::consts::ARCH,
4041
)),
41-
client: reqwest::Client::new(),
42+
client: http_client.build(),
4243
{%- set authMethods = openapi.security %}
4344
{%- if authMethods %}
4445
{%- for authMethod in authMethods %}

.generator/src/generator/templates/function_mappings.j2

+1-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ fn test_{{ operation['operationId'] | snake_case }}(world: &mut DatadogWorld, _p
7575
Ok(response) => response,
7676
Err(error) => {
7777
return match error {
78-
Error::Reqwest(e) => panic!("reqwest error: {}", e),
79-
Error::Serde(e) => panic!("serde error: {}", e),
80-
Error::Io(e) => panic!("io error: {}", e),
8178
Error::ResponseError(e) => world.response.code = e.status.as_u16(),
79+
_ => panic!("error parsing response: {}", error),
8280
};
8381
}
8482
};

Cargo.toml

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@ name = "datadog-api-client"
33
version = "1.0.0"
44
authors = ["[email protected]"]
55
description = "Collection of all Datadog Public endpoints."
6-
# Override this license by providing a License Object in the OpenAPI.
76
license = "Apache-2.0"
87
edition = "2021"
98

109
[dependencies]
10+
log = "0.4.20"
11+
reqwest = { version = "^0.11", features = ["json", "multipart"] }
12+
reqwest-middleware = "0.1.6"
13+
rustc_version = "0.4.0"
1114
serde = "^1.0"
1215
serde_derive = "^1.0"
13-
serde_with = "^2.0"
1416
serde_json = "^1.0"
17+
serde_with = "^2.0"
1518
url = "^2.2"
16-
uuid = { version = "^1.0", features = ["serde"] }
17-
rustc_version = "0.4.0"
18-
log = "0.4.20"
19-
[dependencies.reqwest]
20-
version = "^0.11"
21-
features = ["json", "multipart"]
2219

2320
[dev-dependencies]
21+
chrono = "0.4.31"
2422
cucumber = "0.19.1"
2523
env_logger = "0.10.0"
26-
tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] }
24+
futures = "0.3.28"
2725
handlebars = "4.4.0"
2826
regex = "1.9.5"
27+
rvcr = { git = "https://github.com/nkzou/rvcr.git", rev = "b8f84bc0dfacd539fdc6f7446637c6276dcbb57c" }
2928
sha256 = "1.4.0"
30-
futures = "0.3.28"
29+
tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] }
3130

3231
[[test]]
33-
name = "main"
3432
harness = false # allows Cucumber to print output instead of libtest
33+
name = "main"

src/datadog/configuration.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::env;
88
pub struct Configuration {
99
pub base_path: String,
1010
pub user_agent: Option<String>,
11-
pub client: reqwest::Client,
11+
pub client: reqwest_middleware::ClientWithMiddleware,
1212
pub api_key_auth: Option<String>,
1313
pub app_key_auth: Option<String>,
1414
}
@@ -21,6 +21,7 @@ impl Configuration {
2121

2222
impl Default for Configuration {
2323
fn default() -> Self {
24+
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
2425
Configuration {
2526
base_path: "https://api.datadoghq.com".to_owned(),
2627
user_agent: Some(format!(
@@ -30,7 +31,7 @@ impl Default for Configuration {
3031
env::consts::OS,
3132
env::consts::ARCH,
3233
)),
33-
client: reqwest::Client::new(),
34+
client: http_client.build(),
3435
api_key_auth: env::var("DD_API_KEY").ok(),
3536
app_key_auth: env::var("DD_APP_KEY").ok(),
3637
}

src/datadog/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct ResponseContent<T> {
1111
#[derive(Debug)]
1212
pub enum Error<T> {
1313
Reqwest(reqwest::Error),
14+
ReqwestMiddleware(reqwest_middleware::Error),
1415
Serde(serde_json::Error),
1516
Io(std::io::Error),
1617
ResponseError(ResponseContent<T>),
@@ -20,6 +21,7 @@ impl<T> fmt::Display for Error<T> {
2021
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2122
let (module, e) = match self {
2223
Error::Reqwest(e) => ("reqwest", e.to_string()),
24+
Error::ReqwestMiddleware(e) => ("reqwest_middleware", e.to_string()),
2325
Error::Serde(e) => ("serde", e.to_string()),
2426
Error::Io(e) => ("IO", e.to_string()),
2527
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
@@ -32,6 +34,7 @@ impl<T: fmt::Debug> error::Error for Error<T> {
3234
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
3335
Some(match self {
3436
Error::Reqwest(e) => e,
37+
Error::ReqwestMiddleware(e) => e,
3538
Error::Serde(e) => e,
3639
Error::Io(e) => e,
3740
Error::ResponseError(_) => return None,
@@ -45,6 +48,12 @@ impl<T> From<reqwest::Error> for Error<T> {
4548
}
4649
}
4750

51+
impl<T> From<reqwest_middleware::Error> for Error<T> {
52+
fn from(e: reqwest_middleware::Error) -> Self {
53+
Error::ReqwestMiddleware(e)
54+
}
55+
}
56+
4857
impl<T> From<serde_json::Error> for Error<T> {
4958
fn from(e: serde_json::Error) -> Self {
5059
Error::Serde(e)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2023-01-19T15:15:56.412Z
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"request": {"body": {"string": "{\"data\":{\"attributes\":{\"api_key\":\"TestAddFastlyaccountreturnsCREATEDresponse1674141356\",\"name\":\"Test-Add_Fastly_account_returns_CREATED_response-1674141356\",\"services\":[]},\"type\":\"fastly-accounts\"}}", "encoding": null}, "headers": {"Accept": ["application/json"], "Content-Type": ["application/json"]}, "method": "post", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts"}, "response": {"body": {"string": "{\"data\":{\"attributes\":{\"services\":[],\"name\":\"Test-Add_Fastly_account_returns_CREATED_response-1674141356\"},\"type\":\"fastly-accounts\",\"id\":\"0427b05b6f56f454ca1477aa8df5e75d\"}}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 201, "message": "Created"}}, "recorded_at": "Thu, 19 Jan 2023 15:15:56 GMT"}, {"request": {"body": "", "headers": {"Accept": ["*/*"]}, "method": "delete", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts/0427b05b6f56f454ca1477aa8df5e75d"}, "response": {"body": {"string": "", "encoding": null}, "headers": {"Content-Type": ["text/html; charset=utf-8"]}, "status": {"code": 204, "message": "No Content"}}, "recorded_at": "Thu, 19 Jan 2023 15:15:56 GMT"}], "recorded_with": "VCR 6.0.0"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2023-03-13T10:11:14.475Z
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"request": {"body": {"string": "{\"data\":{\"attributes\":{\"api_key\":\"TestGetFastlyaccountreturnsOKresponse1678702274\",\"name\":\"Test-Get_Fastly_account_returns_OK_response-1678702274\",\"services\":[]},\"type\":\"fastly-accounts\"}}", "encoding": null}, "headers": {"Accept": ["application/json"], "Content-Type": ["application/json"]}, "method": "post", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts"}, "response": {"body": {"string": "{\"data\":{\"type\":\"fastly-accounts\",\"id\":\"6d8f2860f9f3e953fb46d554b9a19627\",\"attributes\":{\"services\":[],\"name\":\"Test-Get_Fastly_account_returns_OK_response-1678702274\"}}}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 201, "message": "Created"}}, "recorded_at": "Mon, 13 Mar 2023 10:11:14 GMT"}, {"request": {"body": "", "headers": {"Accept": ["application/json"]}, "method": "get", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts/6d8f2860f9f3e953fb46d554b9a19627"}, "response": {"body": {"string": "{\"data\":{\"type\":\"fastly-accounts\",\"id\":\"6d8f2860f9f3e953fb46d554b9a19627\",\"attributes\":{\"name\":\"Test-Get_Fastly_account_returns_OK_response-1678702274\",\"services\":[]}}}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 200, "message": "OK"}}, "recorded_at": "Mon, 13 Mar 2023 10:11:14 GMT"}, {"request": {"body": "", "headers": {"Accept": ["*/*"]}, "method": "delete", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts/6d8f2860f9f3e953fb46d554b9a19627"}, "response": {"body": {"string": "", "encoding": null}, "headers": {"Content-Type": ["text/html; charset=utf-8"]}, "status": {"code": 204, "message": "No Content"}}, "recorded_at": "Mon, 13 Mar 2023 10:11:14 GMT"}], "recorded_with": "VCR 6.0.0"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2023-03-13T10:10:50.453Z
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"request": {"body": {"string": "{\"data\":{\"attributes\":{\"api_key\":\"TestListFastlyaccountsreturnsOKresponse1678702250\",\"name\":\"Test-List_Fastly_accounts_returns_OK_response-1678702250\",\"services\":[]},\"type\":\"fastly-accounts\"}}", "encoding": null}, "headers": {"Accept": ["application/json"], "Content-Type": ["application/json"]}, "method": "post", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts"}, "response": {"body": {"string": "{\"data\":{\"type\":\"fastly-accounts\",\"attributes\":{\"name\":\"Test-List_Fastly_accounts_returns_OK_response-1678702250\",\"services\":[]},\"id\":\"07ec97dd43cd794c847ecf15cb25eb1c\"}}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 201, "message": "Created"}}, "recorded_at": "Mon, 13 Mar 2023 10:10:50 GMT"}, {"request": {"body": "", "headers": {"Accept": ["application/json"]}, "method": "get", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts"}, "response": {"body": {"string": "{\"data\":[{\"type\":\"fastly-accounts\",\"attributes\":{\"name\":\"Test-List_Fastly_accounts_returns_OK_response-1678702250\",\"services\":[]},\"id\":\"07ec97dd43cd794c847ecf15cb25eb1c\"}]}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 200, "message": "OK"}}, "recorded_at": "Mon, 13 Mar 2023 10:10:50 GMT"}, {"request": {"body": "", "headers": {"Accept": ["*/*"]}, "method": "delete", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts/07ec97dd43cd794c847ecf15cb25eb1c"}, "response": {"body": {"string": "", "encoding": null}, "headers": {"Content-Type": ["text/html; charset=utf-8"]}, "status": {"code": 204, "message": "No Content"}}, "recorded_at": "Mon, 13 Mar 2023 10:10:50 GMT"}], "recorded_with": "VCR 6.0.0"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2023-03-13T10:10:17.626Z
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"request": {"body": {"string": "{\"data\":{\"attributes\":{\"api_key\":\"TestUpdateFastlyaccountreturnsOKresponse1678702217\",\"name\":\"Test-Update_Fastly_account_returns_OK_response-1678702217\",\"services\":[]},\"type\":\"fastly-accounts\"}}", "encoding": null}, "headers": {"Accept": ["application/json"], "Content-Type": ["application/json"]}, "method": "post", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts"}, "response": {"body": {"string": "{\"data\":{\"type\":\"fastly-accounts\",\"id\":\"e37e834ae856fa24a2924973fdc7c276\",\"attributes\":{\"services\":[],\"name\":\"Test-Update_Fastly_account_returns_OK_response-1678702217\"}}}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 201, "message": "Created"}}, "recorded_at": "Mon, 13 Mar 2023 10:10:17 GMT"}, {"request": {"body": {"string": "{\"data\":{\"attributes\":{\"api_key\":\"update-secret\"},\"type\":\"fastly-accounts\"}}", "encoding": null}, "headers": {"Accept": ["application/json"], "Content-Type": ["application/json"]}, "method": "patch", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts/e37e834ae856fa24a2924973fdc7c276"}, "response": {"body": {"string": "{\"data\":{\"type\":\"fastly-accounts\",\"id\":\"e37e834ae856fa24a2924973fdc7c276\",\"attributes\":{\"services\":[],\"name\":\"Test-Update_Fastly_account_returns_OK_response-1678702217\"}}}\n", "encoding": null}, "headers": {"Content-Type": ["application/json"]}, "status": {"code": 200, "message": "OK"}}, "recorded_at": "Mon, 13 Mar 2023 10:10:17 GMT"}, {"request": {"body": "", "headers": {"Accept": ["*/*"]}, "method": "delete", "uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts/e37e834ae856fa24a2924973fdc7c276"}, "response": {"body": {"string": "", "encoding": null}, "headers": {"Content-Type": ["text/html; charset=utf-8"]}, "status": {"code": 204, "message": "No Content"}}, "recorded_at": "Mon, 13 Mar 2023 10:10:17 GMT"}], "recorded_with": "VCR 6.0.0"}

0 commit comments

Comments
 (0)