Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into datadog-api-spec/te…
Browse files Browse the repository at this point in the history
…st/kevinzou/rust-cassette-tx
  • Loading branch information
nkzou committed Mar 21, 2024
2 parents ddd793e + 22cd378 commit 5e94610
Show file tree
Hide file tree
Showing 83 changed files with 2,395 additions and 805 deletions.
20 changes: 18 additions & 2 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ pub enum {{operation.operationId}}Error {
#[derive(Debug, Clone)]
pub struct {{ structName }} {
config: configuration::Configuration,
client: reqwest_middleware::ClientWithMiddleware,
}

impl Default for {{ structName }} {
fn default() -> Self {
Self {
config: configuration::Configuration::new(),
client: reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build(),
}
}
}
Expand All @@ -93,7 +95,21 @@ impl {{ structName }} {
Self::default()
}
pub fn with_config(config: configuration::Configuration) -> Self {
Self { config }
let mut reqwest_client_builder = reqwest::Client::builder();

if let Some(proxy_url) = &config.proxy_url {
let proxy = reqwest::Proxy::all(proxy_url)
.expect("Failed to parse proxy URL");
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
}

let middleware_client_builder = reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
let client = middleware_client_builder.build();
Self {config, client}
}

pub fn with_client_and_config(config: configuration::Configuration, client: reqwest_middleware::ClientWithMiddleware) -> Self {
Self {config, client}
}

{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=true) %}
Expand Down Expand Up @@ -253,7 +269,7 @@ impl {{ structName }} {
let {{name|variable_name}} = params.{{name|variable_name}};
{%- endfor %}

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}{{path}}",
Expand Down
13 changes: 6 additions & 7 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,20 @@ pub struct APIKey {
#[derive(Debug, Clone)]
pub struct Configuration {
pub(crate) user_agent: String,
pub(crate) client: reqwest_middleware::ClientWithMiddleware,
pub(crate) unstable_operations: HashMap<String, bool>,
pub(crate) auth_keys: HashMap<String, APIKey>,
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>>,
pub proxy_url: Option<String>,
}

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

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

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 @@ -114,11 +110,14 @@ impl Configuration {
pub fn set_auth_key(&mut self, operation_str: &str, api_key: APIKey) {
self.auth_keys.insert(operation_str.to_string(), api_key);
}

pub fn set_proxy_url(&mut self, proxy_url: Option<String>) {
self.proxy_url = proxy_url;
}
}

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("?"),
Expand Down Expand Up @@ -158,13 +157,13 @@ impl Default for Configuration {

Self {
user_agent,
client: http_client.build(),
unstable_operations,
auth_keys,
server_index: 0,
server_variables: HashMap::new(),
server_operation_index: HashMap::new(),
server_operation_variables: HashMap::new(),
proxy_url: None,
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion .generator/src/generator/templates/function_mappings.j2
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub fn initialize_api_instance(world: &mut DatadogWorld, api: String) {
{%- set structName = name.replace(" ", "")+"API" %}
"{{name.replace(" ", "")}}" => {
{%- for version in versions %}
world.api_instances.{{version}}_{{fieldName}} = Some(datadog{{ version.upper() }}::api::{{fieldName}}::{{structName}}::with_config(world.config.clone()));
world.api_instances.{{version}}_{{fieldName}} = Some(datadog{{ version.upper() }}::api::{{fieldName}}::{{structName}}::with_client_and_config(
world.config.clone(),
world.http_client.as_ref().unwrap().clone()
));
{%- endfor %}
},
{%- endfor %}
Expand Down
13 changes: 6 additions & 7 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,20 @@ pub struct APIKey {
#[derive(Debug, Clone)]
pub struct Configuration {
pub(crate) user_agent: String,
pub(crate) client: reqwest_middleware::ClientWithMiddleware,
pub(crate) unstable_operations: HashMap<String, bool>,
pub(crate) auth_keys: HashMap<String, APIKey>,
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>>,
pub proxy_url: Option<String>,
}

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

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

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 @@ -117,11 +113,14 @@ impl Configuration {
pub fn set_auth_key(&mut self, operation_str: &str, api_key: APIKey) {
self.auth_keys.insert(operation_str.to_string(), api_key);
}

pub fn set_proxy_url(&mut self, proxy_url: Option<String>) {
self.proxy_url = proxy_url;
}
}

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("?"),
Expand Down Expand Up @@ -191,13 +190,13 @@ impl Default for Configuration {

Self {
user_agent,
client: http_client.build(),
unstable_operations,
auth_keys,
server_index: 0,
server_variables: HashMap::new(),
server_operation_index: HashMap::new(),
server_operation_variables: HashMap::new(),
proxy_url: None,
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/datadogV1/api/api_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ pub enum ValidateError {
#[derive(Debug, Clone)]
pub struct AuthenticationAPI {
config: configuration::Configuration,
client: reqwest_middleware::ClientWithMiddleware,
}

impl Default for AuthenticationAPI {
fn default() -> Self {
Self {
config: configuration::Configuration::new(),
client: reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build(),
}
}
}
Expand All @@ -32,7 +34,24 @@ impl AuthenticationAPI {
Self::default()
}
pub fn with_config(config: configuration::Configuration) -> Self {
Self { config }
let mut reqwest_client_builder = reqwest::Client::builder();

if let Some(proxy_url) = &config.proxy_url {
let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
}

let middleware_client_builder =
reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
let client = middleware_client_builder.build();
Self { config, client }
}

pub fn with_client_and_config(
config: configuration::Configuration,
client: reqwest_middleware::ClientWithMiddleware,
) -> Self {
Self { config, client }
}

/// Check if the API key (not the APP key) is valid. If invalid, a 403 is returned.
Expand Down Expand Up @@ -64,7 +83,7 @@ impl AuthenticationAPI {
let local_configuration = &self.config;
let operation_id = "v1.validate";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/validate",
Expand Down
45 changes: 32 additions & 13 deletions src/datadogV1/api/api_aws_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ pub enum UpdateAWSAccountError {
#[derive(Debug, Clone)]
pub struct AWSIntegrationAPI {
config: configuration::Configuration,
client: reqwest_middleware::ClientWithMiddleware,
}

impl Default for AWSIntegrationAPI {
fn default() -> Self {
Self {
config: configuration::Configuration::new(),
client: reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build(),
}
}
}
Expand All @@ -209,7 +211,24 @@ impl AWSIntegrationAPI {
Self::default()
}
pub fn with_config(config: configuration::Configuration) -> Self {
Self { config }
let mut reqwest_client_builder = reqwest::Client::builder();

if let Some(proxy_url) = &config.proxy_url {
let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
}

let middleware_client_builder =
reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
let client = middleware_client_builder.build();
Self { config, client }
}

pub fn with_client_and_config(
config: configuration::Configuration,
client: reqwest_middleware::ClientWithMiddleware,
) -> Self {
Self { config, client }
}

/// Create a Datadog-Amazon Web Services integration.
Expand Down Expand Up @@ -249,7 +268,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.create_aws_account";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws",
Expand Down Expand Up @@ -346,7 +365,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.create_aws_event_bridge_source";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/event_bridge",
Expand Down Expand Up @@ -438,7 +457,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.create_aws_tag_filter";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/filtering",
Expand Down Expand Up @@ -530,7 +549,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.create_new_aws_external_id";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/generate_new_external_id",
Expand Down Expand Up @@ -622,7 +641,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.delete_aws_account";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws",
Expand Down Expand Up @@ -719,7 +738,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.delete_aws_event_bridge_source";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/event_bridge",
Expand Down Expand Up @@ -811,7 +830,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.delete_aws_tag_filter";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/filtering",
Expand Down Expand Up @@ -907,7 +926,7 @@ impl AWSIntegrationAPI {
let role_name = params.role_name;
let access_key_id = params.access_key_id;

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws",
Expand Down Expand Up @@ -1005,7 +1024,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.list_aws_event_bridge_sources";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/event_bridge",
Expand Down Expand Up @@ -1090,7 +1109,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.list_aws_tag_filters";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/filtering",
Expand Down Expand Up @@ -1171,7 +1190,7 @@ impl AWSIntegrationAPI {
let local_configuration = &self.config;
let operation_id = "v1.list_available_aws_namespaces";

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws/available_namespace_rules",
Expand Down Expand Up @@ -1261,7 +1280,7 @@ impl AWSIntegrationAPI {
let role_name = params.role_name;
let access_key_id = params.access_key_id;

let local_client = &local_configuration.client;
let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v1/integration/aws",
Expand Down
Loading

0 comments on commit 5e94610

Please sign in to comment.