Skip to content
Open
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
26 changes: 26 additions & 0 deletions .generator/src/generator/templates/ApiClient.j2
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public class ApiClient {
{%- set default_server = openapi.servers[0]|format_server %}
protected String basePath = "{{ default_server.geturl() }}";
protected String userAgent;
private boolean isIaC = false;
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(
Expand Down Expand Up @@ -674,6 +675,31 @@ public class ApiClient {
return this;
}

/**
* Set whether this API client is managed by Infrastructure as Code (IaC) tooling. When enabled,
* the {@code X-Datadog-Managed-By: iac} header is sent on every request made through this client
* (by adding to the default header map).
* @param isIaC Whether calls made with this client originate from IaC tooling
* @return API client
*/
public ApiClient setIsIaC(boolean isIaC) {
this.isIaC = isIaC;
if (isIaC) {
addDefaultHeader("X-Datadog-Managed-By", "iac");
} else {
defaultHeaderMap.remove("X-Datadog-Managed-By");
}
return this;
}

/**
* Get whether this API client is managed by Infrastructure as Code (IaC) tooling.
* @return Whether this client is managed by IaC tooling
*/
public boolean getIsIaC() {
return isIaC;
}

/**
* Add a default cookie.
*
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/datadog/api/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class ApiClient {
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
protected String basePath = "https://api.datadoghq.com";
protected String userAgent;
private boolean isIaC = false;
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

protected List<ServerConfiguration> servers =
Expand Down Expand Up @@ -1922,6 +1923,33 @@ public ApiClient addDefaultHeader(String key, String value) {
return this;
}

/**
* Set whether this API client is managed by Infrastructure as Code (IaC) tooling. When enabled,
* the {@code X-Datadog-Managed-By: iac} header is sent on every request made through this client
* (by adding to the default header map).
*
* @param isIaC Whether calls made with this client originate from IaC tooling
* @return API client
*/
public ApiClient setIsIaC(boolean isIaC) {
this.isIaC = isIaC;
if (isIaC) {
addDefaultHeader("X-Datadog-Managed-By", "iac");
} else {
defaultHeaderMap.remove("X-Datadog-Managed-By");
}
return this;
}

/**
* Get whether this API client is managed by Infrastructure as Code (IaC) tooling.
*
* @return Whether this client is managed by IaC tooling
*/
public boolean getIsIaC() {
return isIaC;
}

/**
* Add a default cookie.
*
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/datadog/api/client/ApiClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.datadog.api.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class ApiClientTest {

@Test
public void testIsIaCDisabledByDefault() {
ApiClient client = new ApiClient();
assertFalse(client.getIsIaC());
assertFalse(client.defaultHeaderMap.containsKey("X-Datadog-Managed-By"));
}

@Test
public void testSetIsIaCTrueAddsManagedByHeaderToDefaultHeaders() {
ApiClient client = new ApiClient();

ApiClient returned = client.setIsIaC(true);

assertSame(client, returned);
assertTrue(client.getIsIaC());
// defaultHeaderMap is merged into the headers of every request made through this client,
// see ApiClient#createBuilder.
assertEquals("iac", client.defaultHeaderMap.get("X-Datadog-Managed-By"));
}

@Test
public void testSetIsIaCFalseRemovesManagedByHeader() {
ApiClient client = new ApiClient();
client.setIsIaC(true);

client.setIsIaC(false);

assertFalse(client.getIsIaC());
assertFalse(client.defaultHeaderMap.containsKey("X-Datadog-Managed-By"));
}

@Test
public void testSetIsIaCIsPerClientInstance() {
ApiClient iacClient = new ApiClient();
ApiClient defaultClient = new ApiClient();

iacClient.setIsIaC(true);

assertTrue(iacClient.getIsIaC());
assertFalse(defaultClient.getIsIaC());
assertFalse(defaultClient.defaultHeaderMap.containsKey("X-Datadog-Managed-By"));
}
}
Loading