Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,20 @@ impl {{ structName }} {
// build auth
{%- set authMethods = operation.security if "security" in operation else openapi.security %}
{%- if authMethods %}
{%- set ns = namespace(hasOAuth2=false) %}
{%- for authMethod in authMethods %}
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
{%- if schema.type == "oauth2" %}
{%- set ns.hasOAuth2 = true %}
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- if ns.hasOAuth2 %}
if let Some(ref access_token) = local_configuration.access_token {
headers.insert("Authorization", HeaderValue::from_str(format!("Bearer {}", access_token).as_str()).expect("failed to parse Authorization header"));
} else {
{%- endif %}
{%- for authMethod in authMethods %}
{%- for name in authMethod %}
{%- set schema = openapi.components.securitySchemes[name] %}
Expand All @@ -405,6 +419,9 @@ impl {{ structName }} {
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- if ns.hasOAuth2 %}
}
{%- endif %}
{%- endif %}

{% if formParameter %}
Expand Down
9 changes: 9 additions & 0 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Configuration {
pub(crate) user_agent: String,
pub(crate) unstable_operations: HashMap<String, bool>,
pub(crate) auth_keys: HashMap<String, APIKey>,
pub(crate) access_token: Option<String>,
pub server_index: usize,
pub server_variables: HashMap<String, String>,
pub server_operation_index: HashMap<String, usize>,
Expand Down Expand Up @@ -106,6 +107,13 @@ impl Configuration {
self.auth_keys.insert(operation_str.to_string(), api_key);
}

/// Set an OAuth2 access token for bearer authentication.
/// When set, endpoints that support the AuthZ security scheme will send
/// an `Authorization: Bearer <token>` header.
pub fn set_access_token(&mut self, token: String) {
self.access_token = Some(token);
}

pub fn set_proxy_url(&mut self, proxy_url: Option<String>) {
self.proxy_url = proxy_url;
}
Expand Down Expand Up @@ -153,6 +161,7 @@ impl Default for Configuration {
user_agent: DEFAULT_USER_AGENT.clone(),
unstable_operations,
auth_keys,
access_token: None,
server_index: 0,
server_variables: HashMap::from([(
"site".into(),
Expand Down
9 changes: 9 additions & 0 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Configuration {
pub(crate) user_agent: String,
pub(crate) unstable_operations: HashMap<String, bool>,
pub(crate) auth_keys: HashMap<String, APIKey>,
pub(crate) access_token: Option<String>,
pub server_index: usize,
pub server_variables: HashMap<String, String>,
pub server_operation_index: HashMap<String, usize>,
Expand Down Expand Up @@ -109,6 +110,13 @@ impl Configuration {
self.auth_keys.insert(operation_str.to_string(), api_key);
}

/// Set an OAuth2 access token for bearer authentication.
/// When set, endpoints that support the AuthZ security scheme will send
/// an `Authorization: Bearer <token>` header.
pub fn set_access_token(&mut self, token: String) {
self.access_token = Some(token);
}

pub fn set_proxy_url(&mut self, proxy_url: Option<String>) {
self.proxy_url = proxy_url;
}
Expand Down Expand Up @@ -397,6 +405,7 @@ impl Default for Configuration {
user_agent: DEFAULT_USER_AGENT.clone(),
unstable_operations,
auth_keys,
access_token: None,
server_index: 0,
server_variables: HashMap::from([(
"site".into(),
Expand Down
3 changes: 1 addition & 2 deletions src/datadogV1/api/api_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ impl AuthenticationAPI {
let builder = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
let builder = if let Some(proxy_url) = &config.proxy_url {
builder
.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
builder.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
} else {
builder
};
Expand Down
3 changes: 1 addition & 2 deletions src/datadogV1/api/api_aws_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ impl AWSIntegrationAPI {
let builder = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
let builder = if let Some(proxy_url) = &config.proxy_url {
builder
.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
builder.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
} else {
builder
};
Expand Down
3 changes: 1 addition & 2 deletions src/datadogV1/api/api_aws_logs_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ impl AWSLogsIntegrationAPI {
let builder = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
let builder = if let Some(proxy_url) = &config.proxy_url {
builder
.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
builder.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
} else {
builder
};
Expand Down
3 changes: 1 addition & 2 deletions src/datadogV1/api/api_azure_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ impl AzureIntegrationAPI {
let builder = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
let builder = if let Some(proxy_url) = &config.proxy_url {
builder
.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
builder.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
} else {
builder
};
Expand Down
163 changes: 101 additions & 62 deletions src/datadogV1/api/api_dashboard_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ impl DashboardListsAPI {
let builder = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
let builder = if let Some(proxy_url) = &config.proxy_url {
builder
.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
builder.proxy(reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"))
} else {
builder
};
Expand Down Expand Up @@ -185,20 +184,28 @@ impl DashboardListsAPI {
};

// build auth
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
if let Some(ref access_token) = local_configuration.access_token {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
"Authorization",
HeaderValue::from_str(format!("Bearer {}", access_token).as_str())
.expect("failed to parse Authorization header"),
);
};
} else {
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};
}

// build body parameters
let output = Vec::new();
Expand Down Expand Up @@ -338,20 +345,28 @@ impl DashboardListsAPI {
};

// build auth
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
if let Some(ref access_token) = local_configuration.access_token {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
"Authorization",
HeaderValue::from_str(format!("Bearer {}", access_token).as_str())
.expect("failed to parse Authorization header"),
);
};
} else {
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};
}

local_req_builder = local_req_builder.headers(headers);
let local_req = local_req_builder.build()?;
Expand Down Expand Up @@ -444,20 +459,28 @@ impl DashboardListsAPI {
};

// build auth
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
if let Some(ref access_token) = local_configuration.access_token {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
"Authorization",
HeaderValue::from_str(format!("Bearer {}", access_token).as_str())
.expect("failed to parse Authorization header"),
);
};
} else {
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};
}

local_req_builder = local_req_builder.headers(headers);
let local_req = local_req_builder.build()?;
Expand Down Expand Up @@ -548,20 +571,28 @@ impl DashboardListsAPI {
};

// build auth
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
if let Some(ref access_token) = local_configuration.access_token {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
"Authorization",
HeaderValue::from_str(format!("Bearer {}", access_token).as_str())
.expect("failed to parse Authorization header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};
} else {
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};
}

local_req_builder = local_req_builder.headers(headers);
let local_req = local_req_builder.build()?;
Expand Down Expand Up @@ -661,20 +692,28 @@ impl DashboardListsAPI {
};

// build auth
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
if let Some(ref access_token) = local_configuration.access_token {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
"Authorization",
HeaderValue::from_str(format!("Bearer {}", access_token).as_str())
.expect("failed to parse Authorization header"),
);
};
} else {
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};
}

// build body parameters
let output = Vec::new();
Expand Down
Loading
Loading