Skip to content

Commit

Permalink
record/replay/passthrough functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzou committed Oct 24, 2023
1 parent 4aebe1a commit 0b509cd
Show file tree
Hide file tree
Showing 17 changed files with 493 additions and 153 deletions.
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
rustc_version = "0.4.0"
log = "0.4.20"
reqwest-middleware = "0.1.6"
[dependencies.reqwest]
version = "^0.11"
features = ["json", "multipart"]
Expand All @@ -28,6 +29,8 @@ handlebars = "4.4.0"
regex = "1.9.5"
sha256 = "1.4.0"
futures = "0.3.28"
rvcr = { git = "https://github.com/nkzou/rvcr.git", rev = "a3fc79e" }
chrono = "0.4.31"

[[test]]
name = "main"
Expand Down
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,67 @@
{
"http_interactions": [
{
"recorded_at": "Thu, 19 Jan 2023 15:15:56 GMT",
"request": {
"body": {
"encoding": null,
"string": "{\"data\":{\"attributes\":{\"api_key\":\"TestAddFastlyaccountreturnsCREATEDresponse1674141356\",\"name\":\"Test-Add_Fastly_account_returns_CREATED_response-1674141356\",\"services\":[]},\"type\":\"fastly-accounts\"}}"
},
"headers": {
"Accept": [
"application/json"
],
"Content-Type": [
"application/json"
]
},
"method": "post",
"uri": "https://api.datadoghq.com/api/v2/integrations/fastly/accounts"
},
"response": {
"body": {
"encoding": null,
"string": "{\"data\":{\"attributes\":{\"services\":[],\"name\":\"Test-Add_Fastly_account_returns_CREATED_response-1674141356\"},\"type\":\"fastly-accounts\",\"id\":\"0427b05b6f56f454ca1477aa8df5e75d\"}}\n"
},
"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": {
"encoding": null,
"string": ""
},
"headers": {
"Content-Type": [
"text/html; charset=utf-8"
]
},
"status": {
"code": 204,
"message": "No Content"
}
}
}
],
"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
Loading

0 comments on commit 0b509cd

Please sign in to comment.