Skip to content

Commit

Permalink
apply code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
skarimo committed Feb 15, 2024
1 parent b5e7ae9 commit b6b7249
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
39 changes: 20 additions & 19 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest_middleware::ClientWithMiddleware,
pub unstable_operations: Option<HashMap<String, bool>>,
{%- set authMethods = openapi.security %}
{%- if authMethods %}
{%- for authMethod in authMethods %}
Expand All @@ -20,7 +19,7 @@ pub struct Configuration {
{%- endfor %}
{%- endfor %}
{%- endif %}

unstable_operations: HashMap<String, bool>,
}

impl Configuration {
Expand All @@ -29,33 +28,35 @@ impl Configuration {
}

pub fn set_unstable_operation_enabled(&mut self, operation: &str, enabled: bool) -> bool {
if let Some(unstable_operations) = &mut self.unstable_operations {
if unstable_operations.contains_key(operation) {
unstable_operations.insert(operation.to_string(), enabled);
return true;
}
if self.unstable_operations.contains_key(operation) {
self.unstable_operations.insert(operation.to_string(), enabled);
return true;
}

warn!("Operation {} is not an unstable operation, can't enable/disable", operation);
warn!(
"Operation {} is not an unstable operation, can't enable/disable",
operation
);

false
}

pub fn is_unstable_operation_enabled(&self, operation: &str) -> bool {
if let Some(unstable_operations) = &self.unstable_operations {
if unstable_operations.contains_key(operation) {
return unstable_operations.get(operation).unwrap().clone();
}
if self.unstable_operations.contains_key(operation) {
return self.unstable_operations.get(operation).unwrap().clone();
}

warn!("Operation {} is not an unstable operation, is always enabled", operation);
warn!(
"Operation {} is not an unstable operation, is always enabled",
operation
);

false
}

pub fn is_unstable_operation(&self, operation: &str) -> bool {
if let Some(unstable_operations) = &self.unstable_operations {
if unstable_operations.contains_key(operation) {
return true;
}
if self.unstable_operations.contains_key(operation) {
return true;
}

false
Expand All @@ -65,7 +66,7 @@ impl Configuration {
impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let unstable_operations = Some(HashMap::from([
let unstable_operations = HashMap::from([
{%- for version, api in apis.items() %}
{%- for operations in api.values() %}
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
Expand All @@ -75,7 +76,7 @@ impl Default for Configuration {
{%- endfor %}
{%- endfor %}
{%- endfor %}
]));
]);

Configuration {
base_path: "https://api.datadoghq.com".to_owned(),
Expand Down
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/target/
**/*.rs.bk
Cargo.lock
__pycache__
__pycache__

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
31 changes: 14 additions & 17 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest_middleware::ClientWithMiddleware,
pub unstable_operations: Option<HashMap<String, bool>>,
pub api_key_auth: Option<String>,
pub app_key_auth: Option<String>,
unstable_operations: HashMap<String, bool>,
}

impl Configuration {
Expand All @@ -21,39 +21,36 @@ impl Configuration {
}

pub fn set_unstable_operation_enabled(&mut self, operation: &str, enabled: bool) -> bool {
if let Some(unstable_operations) = &mut self.unstable_operations {
if unstable_operations.contains_key(operation) {
unstable_operations.insert(operation.to_string(), enabled);
return true;
}
if self.unstable_operations.contains_key(operation) {
self.unstable_operations
.insert(operation.to_string(), enabled);
return true;
}

warn!(
"Operation {} is not an unstable operation, can't enable/disable",
operation
);

false
}

pub fn is_unstable_operation_enabled(&self, operation: &str) -> bool {
if let Some(unstable_operations) = &self.unstable_operations {
if unstable_operations.contains_key(operation) {
return unstable_operations.get(operation).unwrap().clone();
}
if self.unstable_operations.contains_key(operation) {
return self.unstable_operations.get(operation).unwrap().clone();
}

warn!(
"Operation {} is not an unstable operation, is always enabled",
operation
);

false
}

pub fn is_unstable_operation(&self, operation: &str) -> bool {
if let Some(unstable_operations) = &self.unstable_operations {
if unstable_operations.contains_key(operation) {
return true;
}
if self.unstable_operations.contains_key(operation) {
return true;
}

false
Expand All @@ -63,7 +60,7 @@ impl Configuration {
impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let unstable_operations = Some(HashMap::from([
let unstable_operations = HashMap::from([
("v2.get_active_billing_dimensions".to_owned(), false),
("v2.get_monthly_cost_attribution".to_owned(), false),
("v2.create_dora_deployment".to_owned(), false),
Expand Down Expand Up @@ -106,7 +103,7 @@ impl Default for Configuration {
("v2.get_incident_team".to_owned(), false),
("v2.list_incident_teams".to_owned(), false),
("v2.update_incident_team".to_owned(), false),
]));
]);

Configuration {
base_path: "https://api.datadoghq.com".to_owned(),
Expand All @@ -118,9 +115,9 @@ impl Default for Configuration {
env::consts::ARCH,
)),
client: http_client.build(),
unstable_operations: unstable_operations,
api_key_auth: env::var("DD_API_KEY").ok(),
app_key_auth: env::var("DD_APP_KEY").ok(),
unstable_operations: unstable_operations,
}
}
}
10 changes: 5 additions & 5 deletions tests/scenarios/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,16 @@ fn new_request(world: &mut DatadogWorld, operation_id: String) {
}

#[given(expr = "operation {string} enabled")]
fn enable_unstable(_world: &mut DatadogWorld, _operation_id: String) {
fn enable_unstable(world: &mut DatadogWorld, operation_id: String) {
let operation_id = format!(
"v{}.{}",
_world.api_version,
_operation_id.to_case(Case::Snake)
world.api_version,
operation_id.to_case(Case::Snake)
);
_world
world
.config
.set_unstable_operation_enabled(&operation_id, true);
initialize_api_instance(_world, _world.api_name.clone().unwrap());
initialize_api_instance(world, world.api_name.clone().unwrap());
}

#[given(regex = r"^body with value (.*)$")]
Expand Down

0 comments on commit b6b7249

Please sign in to comment.