Skip to content

Commit

Permalink
refactor config object constructor and naming
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzou committed Feb 15, 2024
1 parent a53c4a0 commit 6c53166
Show file tree
Hide file tree
Showing 81 changed files with 3,320 additions and 2,816 deletions.
10 changes: 5 additions & 5 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl {{ structName }} {
{%- endif %}
pub async fn {{operation.operationId | snake_case}}_with_http_info(&self{% for name, parameter in requiredParams %}, {{name|variable_name}}: {{ get_type_for_parameter(parameter, version) }}{% endfor %}{% if operation|has_optional_parameter %}, params: {{operation.operationId}}OptionalParams{% endif %}) -> Result<ResponseContent<{% if returnType %}{{returnType}}{% else %}(){% endif %}>, Error<{{operation.operationId}}Error>> {
let local_configuration = &self.config;
let operation_id = "{{ version }}.{{ operation.operationId | snake_case }}";
{%- if "x-unstable" in operation %}
let operation_id = "{{ version }}.{{ operation.operationId | snake_case }}".to_string();
if local_configuration.is_unstable_operation_enabled(&operation_id) {
if local_configuration.is_unstable_operation_enabled(operation_id) {
warn!("Using unstable operation {}", operation_id);
} else {
let local_error = UnstableOperationDisabledError {
Expand All @@ -132,7 +132,7 @@ impl {{ structName }} {

let local_uri_str = format!(
"{}{{path}}",
local_configuration.get_operation_host("{{ version }}.{{ operation.operationId | snake_case }}")
local_configuration.get_operation_host(operation_id)
{%- for name, parameter in operation|parameters if parameter.in == "path" %}, {{ name|variable_name }}=
{%- if parameter.schema.type == "string" %}
urlencode({{ name|variable_name }}{% if not parameter.required %}.unwrap(){% elif parameter.schema.nullable %}.unwrap(){% endif %}{% if parameter.schema.type == "array" %}.join(",").as_ref(){% endif %})
Expand Down Expand Up @@ -179,8 +179,8 @@ impl {{ structName }} {
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
{%- if schema.type == "apiKey" and schema.in != "cookie" %}
if let Some(ref local_apikey) = local_configuration.{{name|variable_name}} {
local_req_builder = local_req_builder.header("{{schema.name}}", local_apikey);
if let Some(ref local_key) = local_configuration.{{name|variable_name|replace("_auth", "")}} {
local_req_builder = local_req_builder.header("{{schema.name}}", local_key);
};
{%- endif %}
{%- endfor %}
Expand Down
110 changes: 63 additions & 47 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ pub struct Configuration {
pub(crate) user_agent: String,
pub(crate) client: reqwest_middleware::ClientWithMiddleware,
pub unstable_operations: Option<HashMap<String, bool>>,
{%- set authMethods = openapi.security %}
{%- if authMethods %}
{%- for authMethod in authMethods %}
{%- if openapi.security %}
{%- for authMethod in openapi.security %}
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
{%- if schema.type == "apiKey" and schema.in != "cookie" %}
pub {{name|variable_name}}: Option<String>,
pub {{name|variable_name|replace("_auth", "")}}: Option<String>,
{%- endif %}
{%- endfor %}
{%- endfor %}
Expand All @@ -60,14 +59,71 @@ pub struct Configuration {
}

impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
pub fn new() -> Self {
Self::default()
}

pub fn with_keys({% if openapi.security %}{% for authMethod in openapi.security %}{% for name in authMethod %}{% set schema = openapi.components.securitySchemes[name] %}{% if schema.type == "apiKey" and schema.in != "cookie" %}{{name|variable_name|replace("_auth", "")}}: Option<String>{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% endfor %}{% endif %}) -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let user_agent = format!(
"datadog-api-client-rust/{} (rust {}; os {}; arch {})",
option_env!("CARGO_PKG_VERSION").unwrap_or("?"),
option_env!("DD_RUSTC_VERSION").unwrap_or("?"),
env::consts::OS,
env::consts::ARCH,
);
let unstable_operations = Some(HashMap::from([
{%- for version, api in apis.items() %}
{%- for operations in api.values() %}
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
{%- if "x-unstable" in operation %}
("{{ version }}.{{ operation.operationId | snake_case }}".to_owned(), false),
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
]));

Self {
user_agent,
client: http_client.build(),
unstable_operations,
{%- set authMethods = openapi.security %}
{%- if authMethods %}
{%- for authMethod in authMethods %}
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
{%- if schema.type == "apiKey" and schema.in != "cookie" %}
{{name|variable_name|replace("_auth", "")}},
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endif %}
server_index: 0,
server_variables: HashMap::new(),
server_operation_index: HashMap::new(),
server_operation_variables: HashMap::new(),
}

}

pub fn client(&mut self, client: reqwest_middleware::ClientWithMiddleware) {
self.client = client;
}

{%- if openapi.security %}
{%- for authMethod in openapi.security %}
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
{% if schema.type == "apiKey" and schema.in != "cookie" %}
pub fn {{name|variable_name|replace("_auth", "")}}(&mut self, key: String) {
self.{{name|variable_name|replace("_auth", "")}} = Some(key);
}
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endif %}

pub fn get_operation_host(&self, operation_str: &str) -> String {
let operation = operation_str.to_string();
if let Some(servers) = OPERATION_SERVERS.get(&operation) {
Expand Down Expand Up @@ -128,46 +184,7 @@ impl Configuration {

impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let user_agent = format!(
"datadog-api-client-rust/{} (rust {}; os {}; arch {})",
option_env!("CARGO_PKG_VERSION").unwrap_or("?"),
option_env!("DD_RUSTC_VERSION").unwrap_or("?"),
env::consts::OS,
env::consts::ARCH,
);
let unstable_operations = Some(HashMap::from([
{%- for version, api in apis.items() %}
{%- for operations in api.values() %}
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
{%- if "x-unstable" in operation %}
("{{ version }}.{{ operation.operationId | snake_case }}".to_owned(), false),
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
]));

Configuration {
user_agent,
client: http_client.build(),
unstable_operations,
{%- set authMethods = openapi.security %}
{%- if authMethods %}
{%- for authMethod in authMethods %}
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
{%- if schema.type == "apiKey" and schema.in != "cookie" %}
{{name|variable_name}}: env::var("{{ schema.get("x-env-name") }}").ok(),
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endif %}
server_index: 0,
server_variables: HashMap::new(),
server_operation_index: HashMap::new(),
server_operation_variables: HashMap::new(),
}
Self::with_keys({% if openapi.security %}{% for authMethod in openapi.security %}{% for name in authMethod %}{% set schema = openapi.components.securitySchemes[name] %}{% if schema.type == "apiKey" and schema.in != "cookie" %}env::var("{{ schema.get("x-env-name") }}").ok(){% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% endfor %}{% endif %})
}
}

Expand All @@ -194,7 +211,6 @@ ServerConfiguration {
},
{%- endmacro %}


lazy_static! {
static ref SERVERS: Vec<ServerConfiguration> = {
vec![
Expand Down
148 changes: 80 additions & 68 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,98 @@ pub struct Configuration {
pub(crate) user_agent: String,
pub(crate) client: reqwest_middleware::ClientWithMiddleware,
pub unstable_operations: Option<HashMap<String, bool>>,
pub api_key_auth: Option<String>,
pub app_key_auth: Option<String>,
pub api_key: Option<String>,
pub app_key: Option<String>,
pub server_index: usize,
pub server_variables: HashMap<String, String>,
pub server_operation_index: HashMap<String, usize>,
pub server_operation_variables: HashMap<String, HashMap<String, String>>,
}

impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
pub fn new() -> Self {
Self::default()
}

pub fn with_keys(api_key: Option<String>, app_key: Option<String>) -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let user_agent = format!(
"datadog-api-client-rust/{} (rust {}; os {}; arch {})",
option_env!("CARGO_PKG_VERSION").unwrap_or("?"),
option_env!("DD_RUSTC_VERSION").unwrap_or("?"),
env::consts::OS,
env::consts::ARCH,
);
let unstable_operations = Some(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),
("v2.create_dora_incident".to_owned(), false),
("v2.create_incident".to_owned(), false),
("v2.create_incident_integration".to_owned(), false),
("v2.create_incident_todo".to_owned(), false),
("v2.delete_incident".to_owned(), false),
("v2.delete_incident_integration".to_owned(), false),
("v2.delete_incident_todo".to_owned(), false),
("v2.get_incident".to_owned(), false),
("v2.get_incident_integration".to_owned(), false),
("v2.get_incident_todo".to_owned(), false),
("v2.list_incident_attachments".to_owned(), false),
("v2.list_incident_integrations".to_owned(), false),
("v2.list_incidents".to_owned(), false),
("v2.list_incident_todos".to_owned(), false),
("v2.search_incidents".to_owned(), false),
("v2.update_incident".to_owned(), false),
("v2.update_incident_attachments".to_owned(), false),
("v2.update_incident_integration".to_owned(), false),
("v2.update_incident_todo".to_owned(), false),
("v2.query_scalar_data".to_owned(), false),
("v2.query_timeseries_data".to_owned(), false),
("v2.get_finding".to_owned(), false),
("v2.list_findings".to_owned(), false),
("v2.mute_findings".to_owned(), false),
("v2.create_scorecard_outcomes_batch".to_owned(), false),
("v2.create_scorecard_rule".to_owned(), false),
("v2.delete_scorecard_rule".to_owned(), false),
("v2.list_scorecard_outcomes".to_owned(), false),
("v2.list_scorecard_rules".to_owned(), false),
("v2.create_incident_service".to_owned(), false),
("v2.delete_incident_service".to_owned(), false),
("v2.get_incident_service".to_owned(), false),
("v2.list_incident_services".to_owned(), false),
("v2.update_incident_service".to_owned(), false),
("v2.create_incident_team".to_owned(), false),
("v2.delete_incident_team".to_owned(), false),
("v2.get_incident_team".to_owned(), false),
("v2.list_incident_teams".to_owned(), false),
("v2.update_incident_team".to_owned(), false),
]));

Self {
user_agent,
client: http_client.build(),
unstable_operations,
api_key,
app_key,
server_index: 0,
server_variables: HashMap::new(),
server_operation_index: HashMap::new(),
server_operation_variables: HashMap::new(),
}
}

pub fn client(&mut self, client: reqwest_middleware::ClientWithMiddleware) {
self.client = client;
}

pub fn api_key(&mut self, key: String) {
self.api_key = Some(key);
}

pub fn app_key(&mut self, key: String) {
self.app_key = Some(key);
}

pub fn get_operation_host(&self, operation_str: &str) -> String {
let operation = operation_str.to_string();
if let Some(servers) = OPERATION_SERVERS.get(&operation) {
Expand Down Expand Up @@ -120,70 +195,7 @@ impl Configuration {

impl Default for Configuration {
fn default() -> Self {
let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new());
let user_agent = format!(
"datadog-api-client-rust/{} (rust {}; os {}; arch {})",
option_env!("CARGO_PKG_VERSION").unwrap_or("?"),
option_env!("DD_RUSTC_VERSION").unwrap_or("?"),
env::consts::OS,
env::consts::ARCH,
);
let unstable_operations = Some(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),
("v2.create_dora_incident".to_owned(), false),
("v2.create_incident".to_owned(), false),
("v2.create_incident_integration".to_owned(), false),
("v2.create_incident_todo".to_owned(), false),
("v2.delete_incident".to_owned(), false),
("v2.delete_incident_integration".to_owned(), false),
("v2.delete_incident_todo".to_owned(), false),
("v2.get_incident".to_owned(), false),
("v2.get_incident_integration".to_owned(), false),
("v2.get_incident_todo".to_owned(), false),
("v2.list_incident_attachments".to_owned(), false),
("v2.list_incident_integrations".to_owned(), false),
("v2.list_incidents".to_owned(), false),
("v2.list_incident_todos".to_owned(), false),
("v2.search_incidents".to_owned(), false),
("v2.update_incident".to_owned(), false),
("v2.update_incident_attachments".to_owned(), false),
("v2.update_incident_integration".to_owned(), false),
("v2.update_incident_todo".to_owned(), false),
("v2.query_scalar_data".to_owned(), false),
("v2.query_timeseries_data".to_owned(), false),
("v2.get_finding".to_owned(), false),
("v2.list_findings".to_owned(), false),
("v2.mute_findings".to_owned(), false),
("v2.create_scorecard_outcomes_batch".to_owned(), false),
("v2.create_scorecard_rule".to_owned(), false),
("v2.delete_scorecard_rule".to_owned(), false),
("v2.list_scorecard_outcomes".to_owned(), false),
("v2.list_scorecard_rules".to_owned(), false),
("v2.create_incident_service".to_owned(), false),
("v2.delete_incident_service".to_owned(), false),
("v2.get_incident_service".to_owned(), false),
("v2.list_incident_services".to_owned(), false),
("v2.update_incident_service".to_owned(), false),
("v2.create_incident_team".to_owned(), false),
("v2.delete_incident_team".to_owned(), false),
("v2.get_incident_team".to_owned(), false),
("v2.list_incident_teams".to_owned(), false),
("v2.update_incident_team".to_owned(), false),
]));

Configuration {
user_agent,
client: http_client.build(),
unstable_operations,
api_key_auth: env::var("DD_API_KEY").ok(),
app_key_auth: env::var("DD_APP_KEY").ok(),
server_index: 0,
server_variables: HashMap::new(),
server_operation_index: HashMap::new(),
server_operation_variables: HashMap::new(),
}
Self::with_keys(env::var("DD_API_KEY").ok(), env::var("DD_APP_KEY").ok())
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/datadogV1/api/api_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ impl AuthenticationAPI {
Error<ValidateError>,
> {
let local_configuration = &self.config;
let operation_id = "v1.validate";

let local_client = &local_configuration.client;

let local_uri_str = format!(
"{}/api/v1/validate",
local_configuration.get_operation_host("v1.validate")
local_configuration.get_operation_host(operation_id)
);
let mut local_req_builder =
local_client.request(reqwest::Method::GET, local_uri_str.as_str());
Expand All @@ -73,8 +74,8 @@ impl AuthenticationAPI {
);

// build auth
if let Some(ref local_apikey) = local_configuration.api_key_auth {
local_req_builder = local_req_builder.header("DD-API-KEY", local_apikey);
if let Some(ref local_key) = local_configuration.api_key {
local_req_builder = local_req_builder.header("DD-API-KEY", local_key);
};

let local_req = local_req_builder.build()?;
Expand Down
Loading

0 comments on commit 6c53166

Please sign in to comment.