Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cassette record/replay/passthrough modes in bdd runner #4

Merged
merged 11 commits into from
Nov 2, 2023
96 changes: 0 additions & 96 deletions .generator/src/generator/templates/api_mod.j2

This file was deleted.

9 changes: 9 additions & 0 deletions .generator/src/generator/templates/common_mod.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct ResponseContent<T> {
#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
ReqwestMiddleware(reqwest_middleware::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
Expand All @@ -20,6 +21,7 @@ impl <T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::ReqwestMiddleware(e) => ("reqwest_middleware", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
Expand All @@ -32,6 +34,7 @@ impl <T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
Error::ReqwestMiddleware(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
Expand All @@ -45,6 +48,12 @@ impl <T> From<reqwest::Error> for Error<T> {
}
}

impl<T> From<reqwest_middleware::Error> for Error<T> {
fn from(e: reqwest_middleware::Error) -> Self {
Error::ReqwestMiddleware(e)
}
}

impl <T> From<serde_json::Error> for Error<T> {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
Expand Down
5 changes: 3 additions & 2 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::env;
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest::Client,
pub client: reqwest_middleware::ClientWithMiddleware,
{%- set authMethods = openapi.security %}
{%- if authMethods %}
{%- for authMethod in authMethods %}
Expand All @@ -29,6 +29,7 @@ impl Configuration {

impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
Configuration {
base_path: "https://api.datadoghq.com".to_owned(),
user_agent: Some(format!(
Expand All @@ -38,7 +39,7 @@ impl Default for Configuration {
env::consts::OS,
env::consts::ARCH,
)),
client: reqwest::Client::new(),
client: http_client.build(),
{%- set authMethods = openapi.security %}
{%- if authMethods %}
{%- for authMethod in authMethods %}
Expand Down
4 changes: 1 addition & 3 deletions .generator/src/generator/templates/function_mappings.j2
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ fn test_{{ operation['operationId'] | snake_case }}(world: &mut DatadogWorld, _p
Ok(response) => response,
Err(error) => {
return match error {
Error::Reqwest(e) => panic!("reqwest error: {}", e),
Error::Serde(e) => panic!("serde error: {}", e),
Error::Io(e) => panic!("io error: {}", e),
Error::ResponseError(e) => world.response.code = e.status.as_u16(),
_ => panic!("error parsing response: {}", error),
};
}
};
Expand Down
21 changes: 10 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@ name = "datadog-api-client"
version = "1.0.0"
authors = ["[email protected]"]
description = "Collection of all Datadog Public endpoints."
# Override this license by providing a License Object in the OpenAPI.
license = "Apache-2.0"
edition = "2021"

[dependencies]
log = "0.4.20"
reqwest = { version = "^0.11", features = ["json", "multipart"] }
reqwest-middleware = "0.1.6"
rustc_version = "0.4.0"
serde = "^1.0"
serde_derive = "^1.0"
serde_with = "^2.0"
serde_json = "^1.0"
serde_with = "^2.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
rustc_version = "0.4.0"
log = "0.4.20"
[dependencies.reqwest]
version = "^0.11"
features = ["json", "multipart"]

[dev-dependencies]
chrono = "0.4.31"
cucumber = "0.19.1"
env_logger = "0.10.0"
tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] }
futures = "0.3.28"
handlebars = "4.4.0"
regex = "1.9.5"
rvcr = { git = "https://github.com/nkzou/rvcr.git", rev = "b8f84bc0dfacd539fdc6f7446637c6276dcbb57c" }
sha256 = "1.4.0"
futures = "0.3.28"
tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] }

[[test]]
name = "main"
harness = false # allows Cucumber to print output instead of libtest
name = "main"
5 changes: 3 additions & 2 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::env;
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest::Client,
pub client: reqwest_middleware::ClientWithMiddleware,
pub api_key_auth: Option<String>,
pub app_key_auth: Option<String>,
}
Expand All @@ -21,6 +21,7 @@ impl Configuration {

impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
Configuration {
base_path: "https://api.datadoghq.com".to_owned(),
user_agent: Some(format!(
Expand All @@ -30,7 +31,7 @@ impl Default for Configuration {
env::consts::OS,
env::consts::ARCH,
)),
client: reqwest::Client::new(),
client: http_client.build(),
api_key_auth: env::var("DD_API_KEY").ok(),
app_key_auth: env::var("DD_APP_KEY").ok(),
}
Expand Down
9 changes: 9 additions & 0 deletions src/datadog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct ResponseContent<T> {
#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
ReqwestMiddleware(reqwest_middleware::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
Expand All @@ -20,6 +21,7 @@ impl<T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::ReqwestMiddleware(e) => ("reqwest_middleware", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
Expand All @@ -32,6 +34,7 @@ impl<T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
Error::ReqwestMiddleware(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
Expand All @@ -45,6 +48,12 @@ impl<T> From<reqwest::Error> for Error<T> {
}
}

impl<T> From<reqwest_middleware::Error> for Error<T> {
fn from(e: reqwest_middleware::Error) -> Self {
Error::ReqwestMiddleware(e)
}
}

impl<T> From<serde_json::Error> for Error<T> {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-01-19T15:15:56.412Z
Original file line number Diff line number Diff line change
@@ -0,0 +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 number Diff line number Diff line change
@@ -0,0 +1 @@
2023-03-13T10:11:14.475Z
Original file line number Diff line number Diff line change
@@ -0,0 +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 number Diff line number Diff line change
@@ -0,0 +1 @@
2023-03-13T10:10:50.453Z
Original file line number Diff line number Diff line change
@@ -0,0 +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 number Diff line number Diff line change
@@ -0,0 +1 @@
2023-03-13T10:10:17.626Z
Original file line number Diff line number Diff line change
@@ -0,0 +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"}
Loading