diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 260533c..552eeb5 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -36,3 +36,26 @@ jobs:
- name: Test
run: ./gradlew test
+ publish:
+ needs: [ compile, test ]
+ if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout repo
+ uses: actions/checkout@v3
+
+ - name: Set up Java
+ id: setup-jre
+ uses: actions/setup-java@v1
+ with:
+ java-version: "11"
+ architecture: x64
+
+ - name: Publish to maven
+ run: |
+ ./gradlew publish
+ env:
+ MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
+ MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
+ MAVEN_PUBLISH_REGISTRY_URL: "https://s01.oss.sonatype.org/content/repositories/releases/"
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1c3ce8f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,157 @@
+# Intercom Java Library
+
+[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fintercom%2Fintercom-java)
+
+The Intercom Java library provides convenient access to the Intercom API from Java.
+
+## Usage
+
+Instantiate and use the client with the following:
+
+```java
+package com.example.usage;
+
+import com.intercom.api.Intercom;
+import com.intercom.api.resources.articles.requests.CreateArticleRequest;
+import com.intercom.api.resources.articles.types.CreateArticleRequestState;
+
+public class Example {
+ public static void main(String[] args) {
+ Intercom client = Intercom
+ .builder()
+ .token("")
+ .build();
+
+ client.articles().create(
+ CreateArticleRequest
+ .builder()
+ .title("Thanks for everything")
+ .authorId(1295)
+ .description("Description of the Article")
+ .body("Body of the Article")
+ .state(CreateArticleRequestState.PUBLISHED)
+ .build()
+ );
+ }
+}
+```
+
+## Environments
+
+This SDK allows you to configure different environments for API requests.
+
+```java
+import com.intercom.api.Intercom;
+import com.intercom.api.core.Environment;
+
+Intercom client = Intercom
+ .builder()
+ .environment(Environment.USProduction)
+ .build();
+```
+
+## Base Url
+
+You can set a custom base URL when constructing the client.
+
+```java
+import com.intercom.api.Intercom;
+
+Intercom client = Intercom
+ .builder()
+ .url("https://example.com")
+ .build();
+```
+
+## Exception Handling
+
+When the API returns a non-success status code (4xx or 5xx response), an API exception will be thrown.
+
+```java
+import com.intercom.api.core.IntercomApiApiException;
+
+try {
+ client.articles().create(...);
+} catch (IntercomApiApiException e) {
+ // Do something with the API exception...
+}
+```
+
+## Advanced
+
+### Custom Client
+
+This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one.
+However, you can pass your own client like so:
+
+```java
+import com.intercom.api.Intercom;
+import okhttp3.OkHttpClient;
+
+OkHttpClient customClient = ...;
+
+Intercom client = Intercom
+ .builder()
+ .httpClient(customClient)
+ .build();
+```
+
+### Retries
+
+The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
+as the request is deemed retriable and the number of retry attempts has not grown larger than the configured
+retry limit (default: 2).
+
+A request is deemed retriable when any of the following HTTP status codes is returned:
+
+- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
+- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
+- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
+
+Use the `maxRetries` request option to configure this behavior.
+
+```java
+import com.intercom.api.core.RequestOptions;
+
+client.articles().create(
+ ...,
+ RequestOptions
+ .builder()
+ .maxRetries(1)
+ .build()
+);
+```
+
+### Timeouts
+
+The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.
+
+```java
+import com.intercom.api.Intercom;
+import com.intercom.api.core.RequestOptions;
+
+// Client level
+Intercom client = Intercom
+ .builder()
+ .timeout(10)
+ .build();
+
+// Request level
+client.articles().create(
+ ...,
+ RequestOptions
+ .builder()
+ .timeout(10)
+ .build()
+);
+```
+
+## Contributing
+
+While we value open-source contributions to this SDK, this library is generated programmatically.
+Additions made directly to this library would have to be moved over to our generation code,
+otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
+a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
+an issue first to discuss with us!
+
+On the other hand, contributions to the README are always very welcome!
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index d311e1f..93668e9 100644
--- a/build.gradle
+++ b/build.gradle
@@ -42,6 +42,23 @@ java {
}
+group = 'io.intercom'
+
+version = '3.0.0-alpha0'
+
+jar {
+ dependsOn(":generatePomFileForMavenPublication")
+ archiveBaseName = "intercom-java"
+}
+
+sourcesJar {
+ archiveBaseName = "intercom-java"
+}
+
+javadocJar {
+ archiveBaseName = "intercom-java"
+}
+
test {
useJUnitPlatform()
testLogging {
@@ -49,3 +66,36 @@ test {
}
}
+publishing {
+ publications {
+ maven(MavenPublication) {
+ groupId = 'io.intercom'
+ artifactId = 'intercom-java'
+ version = '3.0.0-alpha0'
+ from components.java
+ pom {
+ licenses {
+ license {
+ name = 'The MIT License (MIT)'
+ url = 'https://mit-license.org/'
+ }
+ }
+ scm {
+ connection = 'scm:git:git://github.com/intercom/intercom-java.git'
+ developerConnection = 'scm:git:git://github.com/intercom/intercom-java.git'
+ url = 'https://github.com/intercom/intercom-java'
+ }
+ }
+ }
+ }
+ repositories {
+ maven {
+ url "$System.env.MAVEN_PUBLISH_REGISTRY_URL"
+ credentials {
+ username "$System.env.MAVEN_USERNAME"
+ password "$System.env.MAVEN_PASSWORD"
+ }
+ }
+ }
+}
+
diff --git a/settings.gradle b/settings.gradle
index aed36fe..486d83d 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1 +1,3 @@
+rootProject.name = 'intercom-java'
+
include 'sample-app'
\ No newline at end of file
diff --git a/src/main/java/com/intercom/api/AsyncIntercomBuilder.java b/src/main/java/com/intercom/api/AsyncIntercomBuilder.java
index 1573744..778432b 100644
--- a/src/main/java/com/intercom/api/AsyncIntercomBuilder.java
+++ b/src/main/java/com/intercom/api/AsyncIntercomBuilder.java
@@ -34,13 +34,21 @@ public AsyncIntercomBuilder url(String url) {
}
/**
- * Sets the timeout (in seconds) for the client
+ * Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
*/
public AsyncIntercomBuilder timeout(int timeout) {
this.clientOptionsBuilder.timeout(timeout);
return this;
}
+ /**
+ * Sets the maximum number of retries for the client. Defaults to 2 retries.
+ */
+ public AsyncIntercomBuilder maxRetries(int maxRetries) {
+ this.clientOptionsBuilder.maxRetries(maxRetries);
+ return this;
+ }
+
/**
* Sets the underlying OkHttp client
*/
diff --git a/src/main/java/com/intercom/api/IntercomBuilder.java b/src/main/java/com/intercom/api/IntercomBuilder.java
index 02ad048..ff8fbaa 100644
--- a/src/main/java/com/intercom/api/IntercomBuilder.java
+++ b/src/main/java/com/intercom/api/IntercomBuilder.java
@@ -34,13 +34,21 @@ public IntercomBuilder url(String url) {
}
/**
- * Sets the timeout (in seconds) for the client
+ * Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
*/
public IntercomBuilder timeout(int timeout) {
this.clientOptionsBuilder.timeout(timeout);
return this;
}
+ /**
+ * Sets the maximum number of retries for the client. Defaults to 2 retries.
+ */
+ public IntercomBuilder maxRetries(int maxRetries) {
+ this.clientOptionsBuilder.maxRetries(maxRetries);
+ return this;
+ }
+
/**
* Sets the underlying OkHttp client
*/
diff --git a/src/main/java/com/intercom/api/core/ClientOptions.java b/src/main/java/com/intercom/api/core/ClientOptions.java
index 9d841f2..8107f56 100644
--- a/src/main/java/com/intercom/api/core/ClientOptions.java
+++ b/src/main/java/com/intercom/api/core/ClientOptions.java
@@ -64,9 +64,10 @@ private ClientOptions(
this.headers.putAll(headers);
this.headers.putAll(new HashMap() {
{
+ put("User-Agent", "io.intercom:intercom-java/3.0.0-alpha0");
put("X-Fern-Language", "JAVA");
put("X-Fern-SDK-Name", "com.intercom.fern:api-sdk");
- put("X-Fern-SDK-Version", "0.0.155");
+ put("X-Fern-SDK-Version", "3.0.0-alpha0");
}
});
this.headerSuppliers = headerSuppliers;
@@ -133,12 +134,11 @@ public static final class Builder {
private final Map> headerSuppliers = new HashMap<>();
- private int timeout = 60;
+ private int maxRetries = 2;
- private OkHttpClient httpClient = new OkHttpClient.Builder()
- .addInterceptor(new RetryInterceptor(3))
- .callTimeout(this.timeout, TimeUnit.SECONDS)
- .build();
+ private Optional timeout = Optional.empty();
+
+ private OkHttpClient httpClient = null;
private Optional version = Optional.empty();
@@ -161,10 +161,26 @@ public Builder addHeader(String key, Supplier value) {
* Override the timeout in seconds. Defaults to 60 seconds.
*/
public Builder timeout(int timeout) {
+ this.timeout = Optional.of(timeout);
+ return this;
+ }
+
+ /**
+ * Override the timeout in seconds. Defaults to 60 seconds.
+ */
+ public Builder timeout(Optional timeout) {
this.timeout = timeout;
return this;
}
+ /**
+ * Override the maximum number of retries. Defaults to 2 retries.
+ */
+ public Builder maxRetries(int maxRetries) {
+ this.maxRetries = maxRetries;
+ return this;
+ }
+
public Builder httpClient(OkHttpClient httpClient) {
this.httpClient = httpClient;
return this;
@@ -179,7 +195,28 @@ public Builder version(ApiVersion version) {
}
public ClientOptions build() {
- return new ClientOptions(environment, headers, headerSuppliers, httpClient, this.timeout, version);
+ OkHttpClient.Builder httpClientBuilder =
+ this.httpClient != null ? this.httpClient.newBuilder() : new OkHttpClient.Builder();
+
+ if (this.httpClient != null) {
+ timeout.ifPresent(timeout -> httpClientBuilder
+ .callTimeout(timeout, TimeUnit.SECONDS)
+ .connectTimeout(0, TimeUnit.SECONDS)
+ .writeTimeout(0, TimeUnit.SECONDS)
+ .readTimeout(0, TimeUnit.SECONDS));
+ } else {
+ httpClientBuilder
+ .callTimeout(this.timeout.orElse(60), TimeUnit.SECONDS)
+ .connectTimeout(0, TimeUnit.SECONDS)
+ .writeTimeout(0, TimeUnit.SECONDS)
+ .readTimeout(0, TimeUnit.SECONDS)
+ .addInterceptor(new RetryInterceptor(this.maxRetries));
+ }
+
+ this.httpClient = httpClientBuilder.build();
+ this.timeout = Optional.of(httpClient.callTimeoutMillis() / 1000);
+
+ return new ClientOptions(environment, headers, headerSuppliers, httpClient, this.timeout.get(), version);
}
}
}
diff --git a/src/main/java/com/intercom/api/core/IntercomApiException.java b/src/main/java/com/intercom/api/core/IntercomApiException.java
index 0dc0621..f99fe97 100644
--- a/src/main/java/com/intercom/api/core/IntercomApiException.java
+++ b/src/main/java/com/intercom/api/core/IntercomApiException.java
@@ -3,6 +3,12 @@
*/
package com.intercom.api.core;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import okhttp3.Response;
+
/**
* This exception type will be thrown for any non-2XX API responses.
*/
@@ -17,10 +23,25 @@ public class IntercomApiException extends IntercomException {
*/
private final Object body;
+ private final Map> headers;
+
public IntercomApiException(String message, int statusCode, Object body) {
super(message);
this.statusCode = statusCode;
this.body = body;
+ this.headers = new HashMap<>();
+ }
+
+ public IntercomApiException(String message, int statusCode, Object body, Response rawResponse) {
+ super(message);
+ this.statusCode = statusCode;
+ this.body = body;
+ this.headers = new HashMap<>();
+ rawResponse.headers().forEach(header -> {
+ String key = header.component1();
+ String value = header.component2();
+ this.headers.computeIfAbsent(key, _str -> new ArrayList<>()).add(value);
+ });
}
/**
@@ -37,6 +58,13 @@ public Object body() {
return this.body;
}
+ /**
+ * @return the headers
+ */
+ public Map> headers() {
+ return this.headers;
+ }
+
@java.lang.Override
public String toString() {
return "IntercomApiException{" + "message: " + getMessage() + ", statusCode: " + statusCode + ", body: " + body
diff --git a/src/main/java/com/intercom/api/core/IntercomHttpResponse.java b/src/main/java/com/intercom/api/core/IntercomHttpResponse.java
new file mode 100644
index 0000000..9bf4035
--- /dev/null
+++ b/src/main/java/com/intercom/api/core/IntercomHttpResponse.java
@@ -0,0 +1,37 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.core;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import okhttp3.Response;
+
+public final class IntercomHttpResponse {
+
+ private final T body;
+
+ private final Map> headers;
+
+ public IntercomHttpResponse(T body, Response rawResponse) {
+ this.body = body;
+
+ Map> headers = new HashMap<>();
+ rawResponse.headers().forEach(header -> {
+ String key = header.component1();
+ String value = header.component2();
+ headers.computeIfAbsent(key, _str -> new ArrayList<>()).add(value);
+ });
+ this.headers = headers;
+ }
+
+ public T body() {
+ return this.body;
+ }
+
+ public Map> headers() {
+ return headers;
+ }
+}
diff --git a/src/main/java/com/intercom/api/core/RetryInterceptor.java b/src/main/java/com/intercom/api/core/RetryInterceptor.java
index 1f61a2c..0c1e4ec 100644
--- a/src/main/java/com/intercom/api/core/RetryInterceptor.java
+++ b/src/main/java/com/intercom/api/core/RetryInterceptor.java
@@ -52,7 +52,7 @@ private Response retryChain(Response response, Chain chain) throws IOException {
}
private static boolean shouldRetry(int statusCode) {
- return statusCode == 408 || statusCode == 409 || statusCode == 429 || statusCode >= 500;
+ return statusCode == 408 || statusCode == 429 || statusCode >= 500;
}
private final class ExponentialBackoff {
diff --git a/src/main/java/com/intercom/api/errors/BadRequestError.java b/src/main/java/com/intercom/api/errors/BadRequestError.java
index b20358b..ac6cca6 100644
--- a/src/main/java/com/intercom/api/errors/BadRequestError.java
+++ b/src/main/java/com/intercom/api/errors/BadRequestError.java
@@ -4,6 +4,7 @@
package com.intercom.api.errors;
import com.intercom.api.core.IntercomApiException;
+import okhttp3.Response;
public final class BadRequestError extends IntercomApiException {
/**
@@ -16,6 +17,11 @@ public BadRequestError(Object body) {
this.body = body;
}
+ public BadRequestError(Object body, Response rawResponse) {
+ super("BadRequestError", 400, body, rawResponse);
+ this.body = body;
+ }
+
/**
* @return the body
*/
diff --git a/src/main/java/com/intercom/api/errors/ForbiddenError.java b/src/main/java/com/intercom/api/errors/ForbiddenError.java
index bddad94..42f8a5c 100644
--- a/src/main/java/com/intercom/api/errors/ForbiddenError.java
+++ b/src/main/java/com/intercom/api/errors/ForbiddenError.java
@@ -5,6 +5,7 @@
import com.intercom.api.core.IntercomApiException;
import com.intercom.api.types.Error;
+import okhttp3.Response;
public final class ForbiddenError extends IntercomApiException {
/**
@@ -17,6 +18,11 @@ public ForbiddenError(Error body) {
this.body = body;
}
+ public ForbiddenError(Error body, Response rawResponse) {
+ super("ForbiddenError", 403, body, rawResponse);
+ this.body = body;
+ }
+
/**
* @return the body
*/
diff --git a/src/main/java/com/intercom/api/errors/NotFoundError.java b/src/main/java/com/intercom/api/errors/NotFoundError.java
index 730f46b..b2f1687 100644
--- a/src/main/java/com/intercom/api/errors/NotFoundError.java
+++ b/src/main/java/com/intercom/api/errors/NotFoundError.java
@@ -4,6 +4,7 @@
package com.intercom.api.errors;
import com.intercom.api.core.IntercomApiException;
+import okhttp3.Response;
public final class NotFoundError extends IntercomApiException {
/**
@@ -16,6 +17,11 @@ public NotFoundError(Object body) {
this.body = body;
}
+ public NotFoundError(Object body, Response rawResponse) {
+ super("NotFoundError", 404, body, rawResponse);
+ this.body = body;
+ }
+
/**
* @return the body
*/
diff --git a/src/main/java/com/intercom/api/errors/UnauthorizedError.java b/src/main/java/com/intercom/api/errors/UnauthorizedError.java
index 5d07b74..d1f571b 100644
--- a/src/main/java/com/intercom/api/errors/UnauthorizedError.java
+++ b/src/main/java/com/intercom/api/errors/UnauthorizedError.java
@@ -5,6 +5,7 @@
import com.intercom.api.core.IntercomApiException;
import com.intercom.api.types.Error;
+import okhttp3.Response;
public final class UnauthorizedError extends IntercomApiException {
/**
@@ -17,6 +18,11 @@ public UnauthorizedError(Error body) {
this.body = body;
}
+ public UnauthorizedError(Error body, Response rawResponse) {
+ super("UnauthorizedError", 401, body, rawResponse);
+ this.body = body;
+ }
+
/**
* @return the body
*/
diff --git a/src/main/java/com/intercom/api/errors/UnprocessableEntityError.java b/src/main/java/com/intercom/api/errors/UnprocessableEntityError.java
index 1125624..fb6e8f5 100644
--- a/src/main/java/com/intercom/api/errors/UnprocessableEntityError.java
+++ b/src/main/java/com/intercom/api/errors/UnprocessableEntityError.java
@@ -4,6 +4,7 @@
package com.intercom.api.errors;
import com.intercom.api.core.IntercomApiException;
+import okhttp3.Response;
public final class UnprocessableEntityError extends IntercomApiException {
/**
@@ -16,6 +17,11 @@ public UnprocessableEntityError(Object body) {
this.body = body;
}
+ public UnprocessableEntityError(Object body, Response rawResponse) {
+ super("UnprocessableEntityError", 422, body, rawResponse);
+ this.body = body;
+ }
+
/**
* @return the body
*/
diff --git a/src/main/java/com/intercom/api/resources/admins/AdminsClient.java b/src/main/java/com/intercom/api/resources/admins/AdminsClient.java
index ca154a4..bc494a2 100644
--- a/src/main/java/com/intercom/api/resources/admins/AdminsClient.java
+++ b/src/main/java/com/intercom/api/resources/admins/AdminsClient.java
@@ -3,16 +3,8 @@
*/
package com.intercom.api.resources.admins;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.admins.requests.ConfigureAwayAdminRequest;
import com.intercom.api.resources.admins.requests.FindAdminRequest;
import com.intercom.api.resources.admins.requests.ListAllActivityLogsRequest;
@@ -20,21 +12,22 @@
import com.intercom.api.types.ActivityLogList;
import com.intercom.api.types.AdminList;
import com.intercom.api.types.AdminWithApp;
-import com.intercom.api.types.Error;
-import java.io.IOException;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
public class AdminsClient {
protected final ClientOptions clientOptions;
+ private final RawAdminsClient rawClient;
+
public AdminsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new RawAdminsClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public RawAdminsClient withRawResponse() {
+ return this.rawClient;
}
/**
@@ -45,7 +38,7 @@ public AdminsClient(ClientOptions clientOptions) {
*
*/
public AdminWithApp identify() {
- return identify(null);
+ return this.rawClient.identify().body();
}
/**
@@ -56,247 +49,62 @@ public AdminWithApp identify() {
*
*/
public AdminWithApp identify(RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("me")
- .build();
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminWithApp.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.identify(requestOptions).body();
}
/**
* You can set an Admin as away for the Inbox.
*/
public Admin away(ConfigureAwayAdminRequest request) {
- return away(request, null);
+ return this.rawClient.away(request).body();
}
/**
* You can set an Admin as away for the Inbox.
*/
public Admin away(ConfigureAwayAdminRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins")
- .addPathSegment(request.getAdminId())
- .addPathSegments("away")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("PUT", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.away(request, requestOptions).body();
}
/**
* You can get a log of activities by all admins in an app.
*/
public ActivityLogList listAllActivityLogs(ListAllActivityLogsRequest request) {
- return listAllActivityLogs(request, null);
+ return this.rawClient.listAllActivityLogs(request).body();
}
/**
* You can get a log of activities by all admins in an app.
*/
public ActivityLogList listAllActivityLogs(ListAllActivityLogsRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins/activity_logs");
- QueryStringMapper.addQueryParameter(httpUrl, "created_at_after", request.getCreatedAtAfter(), false);
- if (request.getCreatedAtBefore().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "created_at_before", request.getCreatedAtBefore().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ActivityLogList.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.listAllActivityLogs(request, requestOptions).body();
}
/**
* You can fetch a list of admins for a given workspace.
*/
public AdminList list() {
- return list(null);
+ return this.rawClient.list().body();
}
/**
* You can fetch a list of admins for a given workspace.
*/
public AdminList list(RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins")
- .build();
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminList.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.list(requestOptions).body();
}
/**
* You can retrieve the details of a single admin.
*/
public Admin find(FindAdminRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).body();
}
/**
* You can retrieve the details of a single admin.
*/
public Admin find(FindAdminRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins")
- .addPathSegment(request.getAdminId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.find(request, requestOptions).body();
}
}
diff --git a/src/main/java/com/intercom/api/resources/admins/AsyncAdminsClient.java b/src/main/java/com/intercom/api/resources/admins/AsyncAdminsClient.java
index 28a8409..6a00a05 100644
--- a/src/main/java/com/intercom/api/resources/admins/AsyncAdminsClient.java
+++ b/src/main/java/com/intercom/api/resources/admins/AsyncAdminsClient.java
@@ -3,16 +3,8 @@
*/
package com.intercom.api.resources.admins;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.admins.requests.ConfigureAwayAdminRequest;
import com.intercom.api.resources.admins.requests.FindAdminRequest;
import com.intercom.api.resources.admins.requests.ListAllActivityLogsRequest;
@@ -20,25 +12,23 @@
import com.intercom.api.types.ActivityLogList;
import com.intercom.api.types.AdminList;
import com.intercom.api.types.AdminWithApp;
-import com.intercom.api.types.Error;
-import java.io.IOException;
import java.util.concurrent.CompletableFuture;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.jetbrains.annotations.NotNull;
public class AsyncAdminsClient {
protected final ClientOptions clientOptions;
+ private final AsyncRawAdminsClient rawClient;
+
public AsyncAdminsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new AsyncRawAdminsClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public AsyncRawAdminsClient withRawResponse() {
+ return this.rawClient;
}
/**
@@ -49,7 +39,7 @@ public AsyncAdminsClient(ClientOptions clientOptions) {
*
*/
public CompletableFuture identify() {
- return identify(null);
+ return this.rawClient.identify().thenApply(response -> response.body());
}
/**
@@ -60,131 +50,28 @@ public CompletableFuture identify() {
*
*/
public CompletableFuture identify(RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("me")
- .build();
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminWithApp.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.identify(requestOptions).thenApply(response -> response.body());
}
/**
* You can set an Admin as away for the Inbox.
*/
public CompletableFuture away(ConfigureAwayAdminRequest request) {
- return away(request, null);
+ return this.rawClient.away(request).thenApply(response -> response.body());
}
/**
* You can set an Admin as away for the Inbox.
*/
public CompletableFuture away(ConfigureAwayAdminRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins")
- .addPathSegment(request.getAdminId())
- .addPathSegments("away")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("PUT", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.away(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can get a log of activities by all admins in an app.
*/
public CompletableFuture listAllActivityLogs(ListAllActivityLogsRequest request) {
- return listAllActivityLogs(request, null);
+ return this.rawClient.listAllActivityLogs(request).thenApply(response -> response.body());
}
/**
@@ -192,192 +79,34 @@ public CompletableFuture listAllActivityLogs(ListAllActivityLog
*/
public CompletableFuture listAllActivityLogs(
ListAllActivityLogsRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins/activity_logs");
- QueryStringMapper.addQueryParameter(httpUrl, "created_at_after", request.getCreatedAtAfter(), false);
- if (request.getCreatedAtBefore().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "created_at_before", request.getCreatedAtBefore().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ActivityLogList.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAllActivityLogs(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch a list of admins for a given workspace.
*/
public CompletableFuture list() {
- return list(null);
+ return this.rawClient.list().thenApply(response -> response.body());
}
/**
* You can fetch a list of admins for a given workspace.
*/
public CompletableFuture list(RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins")
- .build();
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminList.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.list(requestOptions).thenApply(response -> response.body());
}
/**
* You can retrieve the details of a single admin.
*/
public CompletableFuture find(FindAdminRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).thenApply(response -> response.body());
}
/**
* You can retrieve the details of a single admin.
*/
public CompletableFuture find(FindAdminRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("admins")
- .addPathSegment(request.getAdminId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.find(request, requestOptions).thenApply(response -> response.body());
}
}
diff --git a/src/main/java/com/intercom/api/resources/admins/AsyncRawAdminsClient.java b/src/main/java/com/intercom/api/resources/admins/AsyncRawAdminsClient.java
new file mode 100644
index 0000000..91b3115
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/admins/AsyncRawAdminsClient.java
@@ -0,0 +1,402 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.admins;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.admins.requests.ConfigureAwayAdminRequest;
+import com.intercom.api.resources.admins.requests.FindAdminRequest;
+import com.intercom.api.resources.admins.requests.ListAllActivityLogsRequest;
+import com.intercom.api.resources.admins.types.Admin;
+import com.intercom.api.types.ActivityLogList;
+import com.intercom.api.types.AdminList;
+import com.intercom.api.types.AdminWithApp;
+import com.intercom.api.types.Error;
+import java.io.IOException;
+import java.util.concurrent.CompletableFuture;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.jetbrains.annotations.NotNull;
+
+public class AsyncRawAdminsClient {
+ protected final ClientOptions clientOptions;
+
+ public AsyncRawAdminsClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can view the currently authorised admin along with the embedded app object (a "workspace" in legacy terminology).
+ *
+ * 🚧 Single Sign On
+ * If you are building a custom "Log in with Intercom" flow for your site, and you call the /me
endpoint to identify the logged-in user, you should not accept any sign-ins from users with unverified email addresses as it poses a potential impersonation security risk.
+ *
+ */
+ public CompletableFuture> identify() {
+ return identify(null);
+ }
+
+ /**
+ * You can view the currently authorised admin along with the embedded app object (a "workspace" in legacy terminology).
+ *
+ * 🚧 Single Sign On
+ * If you are building a custom "Log in with Intercom" flow for your site, and you call the /me
endpoint to identify the logged-in user, you should not accept any sign-ins from users with unverified email addresses as it poses a potential impersonation security risk.
+ *
+ */
+ public CompletableFuture> identify(RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("me")
+ .build();
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminWithApp.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can set an Admin as away for the Inbox.
+ */
+ public CompletableFuture> away(ConfigureAwayAdminRequest request) {
+ return away(request, null);
+ }
+
+ /**
+ * You can set an Admin as away for the Inbox.
+ */
+ public CompletableFuture> away(
+ ConfigureAwayAdminRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins")
+ .addPathSegment(request.getAdminId())
+ .addPathSegments("away")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can get a log of activities by all admins in an app.
+ */
+ public CompletableFuture> listAllActivityLogs(
+ ListAllActivityLogsRequest request) {
+ return listAllActivityLogs(request, null);
+ }
+
+ /**
+ * You can get a log of activities by all admins in an app.
+ */
+ public CompletableFuture> listAllActivityLogs(
+ ListAllActivityLogsRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins/activity_logs");
+ QueryStringMapper.addQueryParameter(httpUrl, "created_at_after", request.getCreatedAtAfter(), false);
+ if (request.getCreatedAtBefore().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "created_at_before", request.getCreatedAtBefore().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ActivityLogList.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of admins for a given workspace.
+ */
+ public CompletableFuture> list() {
+ return list(null);
+ }
+
+ /**
+ * You can fetch a list of admins for a given workspace.
+ */
+ public CompletableFuture> list(RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins")
+ .build();
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminList.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can retrieve the details of a single admin.
+ */
+ public CompletableFuture> find(FindAdminRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can retrieve the details of a single admin.
+ */
+ public CompletableFuture> find(
+ FindAdminRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins")
+ .addPathSegment(request.getAdminId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+}
diff --git a/src/main/java/com/intercom/api/resources/admins/RawAdminsClient.java b/src/main/java/com/intercom/api/resources/admins/RawAdminsClient.java
new file mode 100644
index 0000000..e34c4c2
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/admins/RawAdminsClient.java
@@ -0,0 +1,318 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.admins;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.admins.requests.ConfigureAwayAdminRequest;
+import com.intercom.api.resources.admins.requests.FindAdminRequest;
+import com.intercom.api.resources.admins.requests.ListAllActivityLogsRequest;
+import com.intercom.api.resources.admins.types.Admin;
+import com.intercom.api.types.ActivityLogList;
+import com.intercom.api.types.AdminList;
+import com.intercom.api.types.AdminWithApp;
+import com.intercom.api.types.Error;
+import java.io.IOException;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
+public class RawAdminsClient {
+ protected final ClientOptions clientOptions;
+
+ public RawAdminsClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can view the currently authorised admin along with the embedded app object (a "workspace" in legacy terminology).
+ *
+ * 🚧 Single Sign On
+ * If you are building a custom "Log in with Intercom" flow for your site, and you call the /me
endpoint to identify the logged-in user, you should not accept any sign-ins from users with unverified email addresses as it poses a potential impersonation security risk.
+ *
+ */
+ public IntercomHttpResponse identify() {
+ return identify(null);
+ }
+
+ /**
+ * You can view the currently authorised admin along with the embedded app object (a "workspace" in legacy terminology).
+ *
+ * 🚧 Single Sign On
+ * If you are building a custom "Log in with Intercom" flow for your site, and you call the /me
endpoint to identify the logged-in user, you should not accept any sign-ins from users with unverified email addresses as it poses a potential impersonation security risk.
+ *
+ */
+ public IntercomHttpResponse identify(RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("me")
+ .build();
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminWithApp.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can set an Admin as away for the Inbox.
+ */
+ public IntercomHttpResponse away(ConfigureAwayAdminRequest request) {
+ return away(request, null);
+ }
+
+ /**
+ * You can set an Admin as away for the Inbox.
+ */
+ public IntercomHttpResponse away(ConfigureAwayAdminRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins")
+ .addPathSegment(request.getAdminId())
+ .addPathSegments("away")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can get a log of activities by all admins in an app.
+ */
+ public IntercomHttpResponse listAllActivityLogs(ListAllActivityLogsRequest request) {
+ return listAllActivityLogs(request, null);
+ }
+
+ /**
+ * You can get a log of activities by all admins in an app.
+ */
+ public IntercomHttpResponse listAllActivityLogs(
+ ListAllActivityLogsRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins/activity_logs");
+ QueryStringMapper.addQueryParameter(httpUrl, "created_at_after", request.getCreatedAtAfter(), false);
+ if (request.getCreatedAtBefore().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "created_at_before", request.getCreatedAtBefore().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ActivityLogList.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can fetch a list of admins for a given workspace.
+ */
+ public IntercomHttpResponse list() {
+ return list(null);
+ }
+
+ /**
+ * You can fetch a list of admins for a given workspace.
+ */
+ public IntercomHttpResponse list(RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins")
+ .build();
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdminList.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can retrieve the details of a single admin.
+ */
+ public IntercomHttpResponse find(FindAdminRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can retrieve the details of a single admin.
+ */
+ public IntercomHttpResponse find(FindAdminRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("admins")
+ .addPathSegment(request.getAdminId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Admin.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+}
diff --git a/src/main/java/com/intercom/api/resources/articles/ArticlesClient.java b/src/main/java/com/intercom/api/resources/articles/ArticlesClient.java
index 8c35cba..e7d0664 100644
--- a/src/main/java/com/intercom/api/resources/articles/ArticlesClient.java
+++ b/src/main/java/com/intercom/api/resources/articles/ArticlesClient.java
@@ -3,18 +3,9 @@
*/
package com.intercom.api.resources.articles;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
import com.intercom.api.core.pagination.SyncPagingIterable;
-import com.intercom.api.errors.BadRequestError;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.articles.requests.CreateArticleRequest;
import com.intercom.api.resources.articles.requests.DeleteArticleRequest;
import com.intercom.api.resources.articles.requests.FindArticleRequest;
@@ -24,24 +15,23 @@
import com.intercom.api.resources.articles.types.Article;
import com.intercom.api.resources.articles.types.ArticleListItem;
import com.intercom.api.resources.articles.types.SearchArticlesResponse;
-import com.intercom.api.types.ArticleList;
import com.intercom.api.types.DeletedArticleObject;
-import com.intercom.api.types.Error;
-import java.io.IOException;
-import java.util.List;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
public class ArticlesClient {
protected final ClientOptions clientOptions;
+ private final RawArticlesClient rawClient;
+
public ArticlesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new RawArticlesClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public RawArticlesClient withRawResponse() {
+ return this.rawClient;
}
/**
@@ -52,7 +42,7 @@ public ArticlesClient(ClientOptions clientOptions) {
*
*/
public SyncPagingIterable list() {
- return list(ListArticlesRequest.builder().build());
+ return this.rawClient.list().body();
}
/**
@@ -63,7 +53,7 @@ public SyncPagingIterable list() {
*
*/
public SyncPagingIterable list(ListArticlesRequest request) {
- return list(request, null);
+ return this.rawClient.list(request).body();
}
/**
@@ -74,351 +64,83 @@ public SyncPagingIterable list(ListArticlesRequest request) {
*
*/
public SyncPagingIterable list(ListArticlesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- ArticleList parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ArticleList.class);
- int newPageNumber = request.getPage().map(page -> page + 1).orElse(1);
- ListArticlesRequest nextRequest = ListArticlesRequest.builder()
- .from(request)
- .page(newPageNumber)
- .build();
- List result = parsedResponse.getData();
- return new SyncPagingIterable(true, result, () -> list(nextRequest, requestOptions));
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.list(request, requestOptions).body();
}
/**
* You can create a new article by making a POST request to https://api.intercom.io/articles
.
*/
public Article create(CreateArticleRequest request) {
- return create(request, null);
+ return this.rawClient.create(request).body();
}
/**
* You can create a new article by making a POST request to https://api.intercom.io/articles
.
*/
public Article create(CreateArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.create(request, requestOptions).body();
}
/**
* You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
*/
public Article find(FindArticleRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).body();
}
/**
* You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
*/
public Article find(FindArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .addPathSegment(request.getArticleId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.find(request, requestOptions).body();
}
/**
* You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
*/
public Article update(UpdateArticleRequest request) {
- return update(request, null);
+ return this.rawClient.update(request).body();
}
/**
* You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
*/
public Article update(UpdateArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .addPathSegment(request.getArticleId())
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("PUT", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.update(request, requestOptions).body();
}
/**
* You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
*/
public DeletedArticleObject delete(DeleteArticleRequest request) {
- return delete(request, null);
+ return this.rawClient.delete(request).body();
}
/**
* You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
*/
public DeletedArticleObject delete(DeleteArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .addPathSegment(request.getArticleId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedArticleObject.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.delete(request, requestOptions).body();
}
/**
* You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
*/
public SearchArticlesResponse search() {
- return search(SearchArticlesRequest.builder().build());
+ return this.rawClient.search().body();
}
/**
* You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
*/
public SearchArticlesResponse search(SearchArticlesRequest request) {
- return search(request, null);
+ return this.rawClient.search(request).body();
}
/**
* You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
*/
public SearchArticlesResponse search(SearchArticlesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles/search");
- if (request.getPhrase().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "phrase", request.getPhrase().get(), false);
- }
- if (request.getState().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "state", request.getState().get(), false);
- }
- if (request.getHelpCenterId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "help_center_id", request.getHelpCenterId().get().toString(), false);
- }
- if (request.getHighlight().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "highlight", request.getHighlight().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchArticlesResponse.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.search(request, requestOptions).body();
}
}
diff --git a/src/main/java/com/intercom/api/resources/articles/AsyncArticlesClient.java b/src/main/java/com/intercom/api/resources/articles/AsyncArticlesClient.java
index 8e523dd..b0c29c4 100644
--- a/src/main/java/com/intercom/api/resources/articles/AsyncArticlesClient.java
+++ b/src/main/java/com/intercom/api/resources/articles/AsyncArticlesClient.java
@@ -3,18 +3,9 @@
*/
package com.intercom.api.resources.articles;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
import com.intercom.api.core.pagination.SyncPagingIterable;
-import com.intercom.api.errors.BadRequestError;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.articles.requests.CreateArticleRequest;
import com.intercom.api.resources.articles.requests.DeleteArticleRequest;
import com.intercom.api.resources.articles.requests.FindArticleRequest;
@@ -24,29 +15,24 @@
import com.intercom.api.resources.articles.types.Article;
import com.intercom.api.resources.articles.types.ArticleListItem;
import com.intercom.api.resources.articles.types.SearchArticlesResponse;
-import com.intercom.api.types.ArticleList;
import com.intercom.api.types.DeletedArticleObject;
-import com.intercom.api.types.Error;
-import java.io.IOException;
-import java.util.List;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.jetbrains.annotations.NotNull;
public class AsyncArticlesClient {
protected final ClientOptions clientOptions;
+ private final AsyncRawArticlesClient rawClient;
+
public AsyncArticlesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new AsyncRawArticlesClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public AsyncRawArticlesClient withRawResponse() {
+ return this.rawClient;
}
/**
@@ -57,7 +43,7 @@ public AsyncArticlesClient(ClientOptions clientOptions) {
*
*/
public CompletableFuture> list() {
- return list(ListArticlesRequest.builder().build());
+ return this.rawClient.list().thenApply(response -> response.body());
}
/**
@@ -68,7 +54,7 @@ public CompletableFuture> list() {
*
*/
public CompletableFuture> list(ListArticlesRequest request) {
- return list(request, null);
+ return this.rawClient.list(request).thenApply(response -> response.body());
}
/**
@@ -80,382 +66,77 @@ public CompletableFuture> list(ListArticlesR
*/
public CompletableFuture> list(
ListArticlesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- ArticleList parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ArticleList.class);
- int newPageNumber =
- request.getPage().map(page -> page + 1).orElse(1);
- ListArticlesRequest nextRequest = ListArticlesRequest.builder()
- .from(request)
- .page(newPageNumber)
- .build();
- List result = parsedResponse.getData();
- future.complete(new SyncPagingIterable(true, result, () -> {
- try {
- return list(nextRequest, requestOptions).get();
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.list(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can create a new article by making a POST request to https://api.intercom.io/articles
.
*/
public CompletableFuture create(CreateArticleRequest request) {
- return create(request, null);
+ return this.rawClient.create(request).thenApply(response -> response.body());
}
/**
* You can create a new article by making a POST request to https://api.intercom.io/articles
.
*/
public CompletableFuture create(CreateArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.create(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
*/
public CompletableFuture find(FindArticleRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).thenApply(response -> response.body());
}
/**
* You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
*/
public CompletableFuture find(FindArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .addPathSegment(request.getArticleId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.find(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
*/
public CompletableFuture update(UpdateArticleRequest request) {
- return update(request, null);
+ return this.rawClient.update(request).thenApply(response -> response.body());
}
/**
* You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
*/
public CompletableFuture update(UpdateArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .addPathSegment(request.getArticleId())
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("PUT", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.update(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
*/
public CompletableFuture delete(DeleteArticleRequest request) {
- return delete(request, null);
+ return this.rawClient.delete(request).thenApply(response -> response.body());
}
/**
* You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
*/
public CompletableFuture delete(DeleteArticleRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles")
- .addPathSegment(request.getArticleId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedArticleObject.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.delete(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
*/
public CompletableFuture search() {
- return search(SearchArticlesRequest.builder().build());
+ return this.rawClient.search().thenApply(response -> response.body());
}
/**
* You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
*/
public CompletableFuture search(SearchArticlesRequest request) {
- return search(request, null);
+ return this.rawClient.search(request).thenApply(response -> response.body());
}
/**
@@ -463,71 +144,6 @@ public CompletableFuture search(SearchArticlesRequest re
*/
public CompletableFuture search(
SearchArticlesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("articles/search");
- if (request.getPhrase().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "phrase", request.getPhrase().get(), false);
- }
- if (request.getState().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "state", request.getState().get(), false);
- }
- if (request.getHelpCenterId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "help_center_id", request.getHelpCenterId().get().toString(), false);
- }
- if (request.getHighlight().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "highlight", request.getHighlight().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(
- responseBody.string(), SearchArticlesResponse.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.search(request, requestOptions).thenApply(response -> response.body());
}
}
diff --git a/src/main/java/com/intercom/api/resources/articles/AsyncRawArticlesClient.java b/src/main/java/com/intercom/api/resources/articles/AsyncRawArticlesClient.java
new file mode 100644
index 0000000..f7b5511
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/articles/AsyncRawArticlesClient.java
@@ -0,0 +1,563 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.articles;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.core.pagination.SyncPagingIterable;
+import com.intercom.api.errors.BadRequestError;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.articles.requests.CreateArticleRequest;
+import com.intercom.api.resources.articles.requests.DeleteArticleRequest;
+import com.intercom.api.resources.articles.requests.FindArticleRequest;
+import com.intercom.api.resources.articles.requests.ListArticlesRequest;
+import com.intercom.api.resources.articles.requests.SearchArticlesRequest;
+import com.intercom.api.resources.articles.requests.UpdateArticleRequest;
+import com.intercom.api.resources.articles.types.Article;
+import com.intercom.api.resources.articles.types.ArticleListItem;
+import com.intercom.api.resources.articles.types.SearchArticlesResponse;
+import com.intercom.api.types.ArticleList;
+import com.intercom.api.types.DeletedArticleObject;
+import com.intercom.api.types.Error;
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.jetbrains.annotations.NotNull;
+
+public class AsyncRawArticlesClient {
+ protected final ClientOptions clientOptions;
+
+ public AsyncRawArticlesClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can fetch a list of all articles by making a GET request to https://api.intercom.io/articles
.
+ *
+ * 📘 How are the articles sorted and ordered?
+ * Articles will be returned in descending order on the updated_at
attribute. This means if you need to iterate through results then we'll show the most recently updated articles first.
+ *
+ */
+ public CompletableFuture>> list() {
+ return list(ListArticlesRequest.builder().build());
+ }
+
+ /**
+ * You can fetch a list of all articles by making a GET request to https://api.intercom.io/articles
.
+ *
+ * 📘 How are the articles sorted and ordered?
+ * Articles will be returned in descending order on the updated_at
attribute. This means if you need to iterate through results then we'll show the most recently updated articles first.
+ *
+ */
+ public CompletableFuture>> list(
+ ListArticlesRequest request) {
+ return list(request, null);
+ }
+
+ /**
+ * You can fetch a list of all articles by making a GET request to https://api.intercom.io/articles
.
+ *
+ * 📘 How are the articles sorted and ordered?
+ * Articles will be returned in descending order on the updated_at
attribute. This means if you need to iterate through results then we'll show the most recently updated articles first.
+ *
+ */
+ public CompletableFuture>> list(
+ ListArticlesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture>> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ ArticleList parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ArticleList.class);
+ int newPageNumber =
+ request.getPage().map(page -> page + 1).orElse(1);
+ ListArticlesRequest nextRequest = ListArticlesRequest.builder()
+ .from(request)
+ .page(newPageNumber)
+ .build();
+ List result = parsedResponse.getData();
+ future.complete(new IntercomHttpResponse<>(
+ new SyncPagingIterable(true, result, () -> {
+ try {
+ return list(nextRequest, requestOptions)
+ .get()
+ .body();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can create a new article by making a POST request to https://api.intercom.io/articles
.
+ */
+ public CompletableFuture> create(CreateArticleRequest request) {
+ return create(request, null);
+ }
+
+ /**
+ * You can create a new article by making a POST request to https://api.intercom.io/articles
.
+ */
+ public CompletableFuture> create(
+ CreateArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ future.completeExceptionally(new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
+ */
+ public CompletableFuture> find(FindArticleRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
+ */
+ public CompletableFuture> find(
+ FindArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .addPathSegment(request.getArticleId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
+ */
+ public CompletableFuture> update(UpdateArticleRequest request) {
+ return update(request, null);
+ }
+
+ /**
+ * You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
+ */
+ public CompletableFuture> update(
+ UpdateArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .addPathSegment(request.getArticleId())
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
+ */
+ public CompletableFuture> delete(DeleteArticleRequest request) {
+ return delete(request, null);
+ }
+
+ /**
+ * You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
+ */
+ public CompletableFuture> delete(
+ DeleteArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .addPathSegment(request.getArticleId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedArticleObject.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
+ */
+ public CompletableFuture> search() {
+ return search(SearchArticlesRequest.builder().build());
+ }
+
+ /**
+ * You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
+ */
+ public CompletableFuture> search(SearchArticlesRequest request) {
+ return search(request, null);
+ }
+
+ /**
+ * You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
+ */
+ public CompletableFuture> search(
+ SearchArticlesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles/search");
+ if (request.getPhrase().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "phrase", request.getPhrase().get(), false);
+ }
+ if (request.getState().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "state", request.getState().get(), false);
+ }
+ if (request.getHelpCenterId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "help_center_id", request.getHelpCenterId().get().toString(), false);
+ }
+ if (request.getHighlight().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "highlight", request.getHighlight().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(
+ responseBody.string(), SearchArticlesResponse.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+}
diff --git a/src/main/java/com/intercom/api/resources/articles/RawArticlesClient.java b/src/main/java/com/intercom/api/resources/articles/RawArticlesClient.java
new file mode 100644
index 0000000..85302d6
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/articles/RawArticlesClient.java
@@ -0,0 +1,449 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.articles;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.core.pagination.SyncPagingIterable;
+import com.intercom.api.errors.BadRequestError;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.articles.requests.CreateArticleRequest;
+import com.intercom.api.resources.articles.requests.DeleteArticleRequest;
+import com.intercom.api.resources.articles.requests.FindArticleRequest;
+import com.intercom.api.resources.articles.requests.ListArticlesRequest;
+import com.intercom.api.resources.articles.requests.SearchArticlesRequest;
+import com.intercom.api.resources.articles.requests.UpdateArticleRequest;
+import com.intercom.api.resources.articles.types.Article;
+import com.intercom.api.resources.articles.types.ArticleListItem;
+import com.intercom.api.resources.articles.types.SearchArticlesResponse;
+import com.intercom.api.types.ArticleList;
+import com.intercom.api.types.DeletedArticleObject;
+import com.intercom.api.types.Error;
+import java.io.IOException;
+import java.util.List;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
+public class RawArticlesClient {
+ protected final ClientOptions clientOptions;
+
+ public RawArticlesClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can fetch a list of all articles by making a GET request to https://api.intercom.io/articles
.
+ *
+ * 📘 How are the articles sorted and ordered?
+ * Articles will be returned in descending order on the updated_at
attribute. This means if you need to iterate through results then we'll show the most recently updated articles first.
+ *
+ */
+ public IntercomHttpResponse> list() {
+ return list(ListArticlesRequest.builder().build());
+ }
+
+ /**
+ * You can fetch a list of all articles by making a GET request to https://api.intercom.io/articles
.
+ *
+ * 📘 How are the articles sorted and ordered?
+ * Articles will be returned in descending order on the updated_at
attribute. This means if you need to iterate through results then we'll show the most recently updated articles first.
+ *
+ */
+ public IntercomHttpResponse> list(ListArticlesRequest request) {
+ return list(request, null);
+ }
+
+ /**
+ * You can fetch a list of all articles by making a GET request to https://api.intercom.io/articles
.
+ *
+ * 📘 How are the articles sorted and ordered?
+ * Articles will be returned in descending order on the updated_at
attribute. This means if you need to iterate through results then we'll show the most recently updated articles first.
+ *
+ */
+ public IntercomHttpResponse> list(
+ ListArticlesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ ArticleList parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ArticleList.class);
+ int newPageNumber = request.getPage().map(page -> page + 1).orElse(1);
+ ListArticlesRequest nextRequest = ListArticlesRequest.builder()
+ .from(request)
+ .page(newPageNumber)
+ .build();
+ List result = parsedResponse.getData();
+ return new IntercomHttpResponse<>(
+ new SyncPagingIterable(true, result, () -> list(nextRequest, requestOptions)
+ .body()),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can create a new article by making a POST request to https://api.intercom.io/articles
.
+ */
+ public IntercomHttpResponse create(CreateArticleRequest request) {
+ return create(request, null);
+ }
+
+ /**
+ * You can create a new article by making a POST request to https://api.intercom.io/articles
.
+ */
+ public IntercomHttpResponse create(CreateArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
+ */
+ public IntercomHttpResponse find(FindArticleRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can fetch the details of a single article by making a GET request to https://api.intercom.io/articles/<id>
.
+ */
+ public IntercomHttpResponse find(FindArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .addPathSegment(request.getArticleId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
+ */
+ public IntercomHttpResponse update(UpdateArticleRequest request) {
+ return update(request, null);
+ }
+
+ /**
+ * You can update the details of a single article by making a PUT request to https://api.intercom.io/articles/<id>
.
+ */
+ public IntercomHttpResponse update(UpdateArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .addPathSegment(request.getArticleId())
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Article.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
+ */
+ public IntercomHttpResponse delete(DeleteArticleRequest request) {
+ return delete(request, null);
+ }
+
+ /**
+ * You can delete a single article by making a DELETE request to https://api.intercom.io/articles/<id>
.
+ */
+ public IntercomHttpResponse delete(
+ DeleteArticleRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles")
+ .addPathSegment(request.getArticleId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedArticleObject.class),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
+ */
+ public IntercomHttpResponse search() {
+ return search(SearchArticlesRequest.builder().build());
+ }
+
+ /**
+ * You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
+ */
+ public IntercomHttpResponse search(SearchArticlesRequest request) {
+ return search(request, null);
+ }
+
+ /**
+ * You can search for articles by making a GET request to https://api.intercom.io/articles/search
.
+ */
+ public IntercomHttpResponse search(
+ SearchArticlesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("articles/search");
+ if (request.getPhrase().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "phrase", request.getPhrase().get(), false);
+ }
+ if (request.getState().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "state", request.getState().get(), false);
+ }
+ if (request.getHelpCenterId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "help_center_id", request.getHelpCenterId().get().toString(), false);
+ }
+ if (request.getHighlight().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "highlight", request.getHighlight().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchArticlesResponse.class),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+}
diff --git a/src/main/java/com/intercom/api/resources/companies/AsyncCompaniesClient.java b/src/main/java/com/intercom/api/resources/companies/AsyncCompaniesClient.java
index 0168958..289e047 100644
--- a/src/main/java/com/intercom/api/resources/companies/AsyncCompaniesClient.java
+++ b/src/main/java/com/intercom/api/resources/companies/AsyncCompaniesClient.java
@@ -3,18 +3,9 @@
*/
package com.intercom.api.resources.companies;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
import com.intercom.api.core.pagination.SyncPagingIterable;
-import com.intercom.api.errors.BadRequestError;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.companies.requests.AttachContactToCompanyRequest;
import com.intercom.api.resources.companies.requests.CreateOrUpdateCompanyRequest;
import com.intercom.api.resources.companies.requests.DeleteCompanyRequest;
@@ -30,30 +21,24 @@
import com.intercom.api.types.CompanyAttachedContacts;
import com.intercom.api.types.CompanyAttachedSegments;
import com.intercom.api.types.CompanyList;
-import com.intercom.api.types.CompanyScroll;
import com.intercom.api.types.DeletedCompanyObject;
-import com.intercom.api.types.Error;
-import java.io.IOException;
-import java.util.List;
-import java.util.Optional;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.jetbrains.annotations.NotNull;
public class AsyncCompaniesClient {
protected final ClientOptions clientOptions;
+ private final AsyncRawCompaniesClient rawClient;
+
public AsyncCompaniesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new AsyncRawCompaniesClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public AsyncRawCompaniesClient withRawResponse() {
+ return this.rawClient;
}
/**
@@ -65,7 +50,7 @@ public AsyncCompaniesClient(ClientOptions clientOptions) {
* https://api.intercom.io/companies?segment_id={segment_id}
*/
public CompletableFuture retrieve() {
- return retrieve(RetrieveCompanyRequest.builder().build());
+ return this.rawClient.retrieve().thenApply(response -> response.body());
}
/**
@@ -77,7 +62,7 @@ public CompletableFuture retrieve() {
* https://api.intercom.io/companies?segment_id={segment_id}
*/
public CompletableFuture retrieve(RetrieveCompanyRequest request) {
- return retrieve(request, null);
+ return this.rawClient.retrieve(request).thenApply(response -> response.body());
}
/**
@@ -89,84 +74,7 @@ public CompletableFuture retrieve(RetrieveCompanyRequest request) {
* https://api.intercom.io/companies?segment_id={segment_id}
*/
public CompletableFuture retrieve(RetrieveCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies");
- if (request.getName().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "name", request.getName().get(), false);
- }
- if (request.getCompanyId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "company_id", request.getCompanyId().get(), false);
- }
- if (request.getTagId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "tag_id", request.getTagId().get(), false);
- }
- if (request.getSegmentId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "segment_id", request.getSegmentId().get(), false);
- }
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.retrieve(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -178,7 +86,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* {% /admonition %}
*/
public CompletableFuture createOrUpdate() {
- return createOrUpdate(CreateOrUpdateCompanyRequest.builder().build());
+ return this.rawClient.createOrUpdate().thenApply(response -> response.body());
}
/**
@@ -190,7 +98,7 @@ public CompletableFuture createOrUpdate() {
* {% /admonition %}
*/
public CompletableFuture createOrUpdate(CreateOrUpdateCompanyRequest request) {
- return createOrUpdate(request, null);
+ return this.rawClient.createOrUpdate(request).thenApply(response -> response.body());
}
/**
@@ -203,137 +111,21 @@ public CompletableFuture createOrUpdate(CreateOrUpdateCompanyRequest re
*/
public CompletableFuture createOrUpdate(
CreateOrUpdateCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.createOrUpdate(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch a single company.
*/
public CompletableFuture find(FindCompanyRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).thenApply(response -> response.body());
}
/**
* You can fetch a single company.
*/
public CompletableFuture find(FindCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.find(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -343,7 +135,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* {% /admonition %}
*/
public CompletableFuture update(UpdateCompanyRequest request) {
- return update(request, null);
+ return this.rawClient.update(request).thenApply(response -> response.body());
}
/**
@@ -353,139 +145,28 @@ public CompletableFuture update(UpdateCompanyRequest request) {
* {% /admonition %}
*/
public CompletableFuture update(UpdateCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("PUT", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.update(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can delete a single company.
*/
public CompletableFuture delete(DeleteCompanyRequest request) {
- return delete(request, null);
+ return this.rawClient.delete(request).thenApply(response -> response.body());
}
/**
* You can delete a single company.
*/
public CompletableFuture delete(DeleteCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedCompanyObject.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.delete(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch a list of all contacts that belong to a company.
*/
public CompletableFuture listAttachedContacts(ListAttachedContactsRequest request) {
- return listAttachedContacts(request, null);
+ return this.rawClient.listAttachedContacts(request).thenApply(response -> response.body());
}
/**
@@ -493,71 +174,7 @@ public CompletableFuture listAttachedContacts(ListAttac
*/
public CompletableFuture listAttachedContacts(
ListAttachedContactsRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .addPathSegments("contacts");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(
- responseBody.string(), CompanyAttachedContacts.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAttachedContacts(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -565,7 +182,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
*/
public CompletableFuture listAttachedSegments(
ListSegmentsAttachedToCompanyRequest request) {
- return listAttachedSegments(request, null);
+ return this.rawClient.listAttachedSegments(request).thenApply(response -> response.body());
}
/**
@@ -573,64 +190,7 @@ public CompletableFuture listAttachedSegments(
*/
public CompletableFuture listAttachedSegments(
ListSegmentsAttachedToCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .addPathSegments("segments")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(
- responseBody.string(), CompanyAttachedSegments.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAttachedSegments(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -643,7 +203,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* {% /admonition %}
*/
public CompletableFuture> list() {
- return list(ListCompaniesRequest.builder().build());
+ return this.rawClient.list().thenApply(response -> response.body());
}
/**
@@ -656,7 +216,7 @@ public CompletableFuture> list() {
* {% /admonition %}
*/
public CompletableFuture> list(ListCompaniesRequest request) {
- return list(request, null);
+ return this.rawClient.list(request).thenApply(response -> response.body());
}
/**
@@ -670,82 +230,7 @@ public CompletableFuture> list(ListCompaniesRequest
*/
public CompletableFuture> list(
ListCompaniesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies/list");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- if (request.getOrder().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "order", request.getOrder().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("POST", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- CompanyList parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class);
- int newPageNumber =
- request.getPage().map(page -> page + 1).orElse(1);
- ListCompaniesRequest nextRequest = ListCompaniesRequest.builder()
- .from(request)
- .page(newPageNumber)
- .build();
- List result = parsedResponse.getData();
- future.complete(new SyncPagingIterable(true, result, () -> {
- try {
- return list(nextRequest, requestOptions).get();
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.list(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -766,7 +251,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* {% /admonition %}
*/
public CompletableFuture> scroll() {
- return scroll(ScrollCompaniesRequest.builder().build());
+ return this.rawClient.scroll().thenApply(response -> response.body());
}
/**
@@ -787,7 +272,7 @@ public CompletableFuture> scroll() {
* {% /admonition %}
*/
public CompletableFuture> scroll(ScrollCompaniesRequest request) {
- return scroll(request, null);
+ return this.rawClient.scroll(request).thenApply(response -> response.body());
}
/**
@@ -809,80 +294,14 @@ public CompletableFuture> scroll(ScrollCompaniesRequ
*/
public CompletableFuture> scroll(
ScrollCompaniesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies/scroll");
- if (request.getScrollParam().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "scroll_param", request.getScrollParam().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- CompanyScroll parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyScroll.class);
- Optional startingAfter = parsedResponse.getScrollParam();
- ScrollCompaniesRequest nextRequest = ScrollCompaniesRequest.builder()
- .from(request)
- .scrollParam(startingAfter)
- .build();
- List result = parsedResponse.getData();
- future.complete(new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
- try {
- return scroll(nextRequest, requestOptions).get();
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.scroll(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can attach a company to a single contact.
*/
public CompletableFuture attachContact(AttachContactToCompanyRequest request) {
- return attachContact(request, null);
+ return this.rawClient.attachContact(request).thenApply(response -> response.body());
}
/**
@@ -890,81 +309,14 @@ public CompletableFuture attachContact(AttachContactToCompanyRequest re
*/
public CompletableFuture attachContact(
AttachContactToCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("companies")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.attachContact(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can detach a company from a single contact.
*/
public CompletableFuture detachContact(DetachContactFromCompanyRequest request) {
- return detachContact(request, null);
+ return this.rawClient.detachContact(request).thenApply(response -> response.body());
}
/**
@@ -972,63 +324,6 @@ public CompletableFuture detachContact(DetachContactFromCompanyRequest
*/
public CompletableFuture detachContact(
DetachContactFromCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.detachContact(request, requestOptions).thenApply(response -> response.body());
}
}
diff --git a/src/main/java/com/intercom/api/resources/companies/AsyncRawCompaniesClient.java b/src/main/java/com/intercom/api/resources/companies/AsyncRawCompaniesClient.java
new file mode 100644
index 0000000..fea4f30
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/companies/AsyncRawCompaniesClient.java
@@ -0,0 +1,1090 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.companies;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.core.pagination.SyncPagingIterable;
+import com.intercom.api.errors.BadRequestError;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.companies.requests.AttachContactToCompanyRequest;
+import com.intercom.api.resources.companies.requests.CreateOrUpdateCompanyRequest;
+import com.intercom.api.resources.companies.requests.DeleteCompanyRequest;
+import com.intercom.api.resources.companies.requests.DetachContactFromCompanyRequest;
+import com.intercom.api.resources.companies.requests.FindCompanyRequest;
+import com.intercom.api.resources.companies.requests.ListAttachedContactsRequest;
+import com.intercom.api.resources.companies.requests.ListCompaniesRequest;
+import com.intercom.api.resources.companies.requests.ListSegmentsAttachedToCompanyRequest;
+import com.intercom.api.resources.companies.requests.RetrieveCompanyRequest;
+import com.intercom.api.resources.companies.requests.ScrollCompaniesRequest;
+import com.intercom.api.resources.companies.requests.UpdateCompanyRequest;
+import com.intercom.api.resources.companies.types.Company;
+import com.intercom.api.types.CompanyAttachedContacts;
+import com.intercom.api.types.CompanyAttachedSegments;
+import com.intercom.api.types.CompanyList;
+import com.intercom.api.types.CompanyScroll;
+import com.intercom.api.types.DeletedCompanyObject;
+import com.intercom.api.types.Error;
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.jetbrains.annotations.NotNull;
+
+public class AsyncRawCompaniesClient {
+ protected final ClientOptions clientOptions;
+
+ public AsyncRawCompaniesClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can fetch a single company by passing in company_id
or name
.
+ * https://api.intercom.io/companies?name={name}
+ * https://api.intercom.io/companies?company_id={company_id}
+ * You can fetch all companies and filter by segment_id
or tag_id
as a query parameter.
+ * https://api.intercom.io/companies?tag_id={tag_id}
+ * https://api.intercom.io/companies?segment_id={segment_id}
+ */
+ public CompletableFuture> retrieve() {
+ return retrieve(RetrieveCompanyRequest.builder().build());
+ }
+
+ /**
+ * You can fetch a single company by passing in company_id
or name
.
+ * https://api.intercom.io/companies?name={name}
+ * https://api.intercom.io/companies?company_id={company_id}
+ * You can fetch all companies and filter by segment_id
or tag_id
as a query parameter.
+ * https://api.intercom.io/companies?tag_id={tag_id}
+ * https://api.intercom.io/companies?segment_id={segment_id}
+ */
+ public CompletableFuture> retrieve(RetrieveCompanyRequest request) {
+ return retrieve(request, null);
+ }
+
+ /**
+ * You can fetch a single company by passing in company_id
or name
.
+ * https://api.intercom.io/companies?name={name}
+ * https://api.intercom.io/companies?company_id={company_id}
+ * You can fetch all companies and filter by segment_id
or tag_id
as a query parameter.
+ * https://api.intercom.io/companies?tag_id={tag_id}
+ * https://api.intercom.io/companies?segment_id={segment_id}
+ */
+ public CompletableFuture> retrieve(
+ RetrieveCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies");
+ if (request.getName().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "name", request.getName().get(), false);
+ }
+ if (request.getCompanyId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "company_id", request.getCompanyId().get(), false);
+ }
+ if (request.getTagId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "tag_id", request.getTagId().get(), false);
+ }
+ if (request.getSegmentId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "segment_id", request.getSegmentId().get(), false);
+ }
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can create or update a company.
+ * Companies will be only visible in Intercom when there is at least one associated user.
+ * Companies are looked up via company_id
in a POST
request, if not found via company_id
, the new company will be created, if found, that company will be updated.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * You can set a unique company_id
value when creating a company. However, it is not possible to update company_id
. Be sure to set a unique value once upon creation of the company.
+ * {% /admonition %}
+ */
+ public CompletableFuture> createOrUpdate() {
+ return createOrUpdate(CreateOrUpdateCompanyRequest.builder().build());
+ }
+
+ /**
+ * You can create or update a company.
+ * Companies will be only visible in Intercom when there is at least one associated user.
+ * Companies are looked up via company_id
in a POST
request, if not found via company_id
, the new company will be created, if found, that company will be updated.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * You can set a unique company_id
value when creating a company. However, it is not possible to update company_id
. Be sure to set a unique value once upon creation of the company.
+ * {% /admonition %}
+ */
+ public CompletableFuture> createOrUpdate(CreateOrUpdateCompanyRequest request) {
+ return createOrUpdate(request, null);
+ }
+
+ /**
+ * You can create or update a company.
+ * Companies will be only visible in Intercom when there is at least one associated user.
+ * Companies are looked up via company_id
in a POST
request, if not found via company_id
, the new company will be created, if found, that company will be updated.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * You can set a unique company_id
value when creating a company. However, it is not possible to update company_id
. Be sure to set a unique value once upon creation of the company.
+ * {% /admonition %}
+ */
+ public CompletableFuture> createOrUpdate(
+ CreateOrUpdateCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ future.completeExceptionally(new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a single company.
+ */
+ public CompletableFuture> find(FindCompanyRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can fetch a single company.
+ */
+ public CompletableFuture> find(
+ FindCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can update a single company using the Intercom provisioned id
.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * When updating a company it is not possible to update company_id
. This can only be set once upon creation of the company.
+ * {% /admonition %}
+ */
+ public CompletableFuture> update(UpdateCompanyRequest request) {
+ return update(request, null);
+ }
+
+ /**
+ * You can update a single company using the Intercom provisioned id
.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * When updating a company it is not possible to update company_id
. This can only be set once upon creation of the company.
+ * {% /admonition %}
+ */
+ public CompletableFuture> update(
+ UpdateCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", RequestBody.create("", null))
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can delete a single company.
+ */
+ public CompletableFuture> delete(DeleteCompanyRequest request) {
+ return delete(request, null);
+ }
+
+ /**
+ * You can delete a single company.
+ */
+ public CompletableFuture> delete(
+ DeleteCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedCompanyObject.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of all contacts that belong to a company.
+ */
+ public CompletableFuture> listAttachedContacts(
+ ListAttachedContactsRequest request) {
+ return listAttachedContacts(request, null);
+ }
+
+ /**
+ * You can fetch a list of all contacts that belong to a company.
+ */
+ public CompletableFuture> listAttachedContacts(
+ ListAttachedContactsRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .addPathSegments("contacts");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(
+ responseBody.string(), CompanyAttachedContacts.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of all segments that belong to a company.
+ */
+ public CompletableFuture> listAttachedSegments(
+ ListSegmentsAttachedToCompanyRequest request) {
+ return listAttachedSegments(request, null);
+ }
+
+ /**
+ * You can fetch a list of all segments that belong to a company.
+ */
+ public CompletableFuture> listAttachedSegments(
+ ListSegmentsAttachedToCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .addPathSegments("segments")
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(
+ responseBody.string(), CompanyAttachedSegments.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can list companies. The company list is sorted by the last_request_at
field and by default is ordered descending, most recently requested first.
+ * Note that the API does not include companies who have no associated users in list responses.
+ * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the Scroll API.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 20
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> list() {
+ return list(ListCompaniesRequest.builder().build());
+ }
+
+ /**
+ * You can list companies. The company list is sorted by the last_request_at
field and by default is ordered descending, most recently requested first.
+ * Note that the API does not include companies who have no associated users in list responses.
+ * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the Scroll API.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 20
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> list(ListCompaniesRequest request) {
+ return list(request, null);
+ }
+
+ /**
+ * You can list companies. The company list is sorted by the last_request_at
field and by default is ordered descending, most recently requested first.
+ * Note that the API does not include companies who have no associated users in list responses.
+ * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the Scroll API.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 20
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> list(
+ ListCompaniesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies/list");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ if (request.getOrder().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "order", request.getOrder().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("POST", RequestBody.create("", null))
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture>> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ CompanyList parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class);
+ int newPageNumber =
+ request.getPage().map(page -> page + 1).orElse(1);
+ ListCompaniesRequest nextRequest = ListCompaniesRequest.builder()
+ .from(request)
+ .page(newPageNumber)
+ .build();
+ List result = parsedResponse.getData();
+ future.complete(new IntercomHttpResponse<>(
+ new SyncPagingIterable(true, result, () -> {
+ try {
+ return list(nextRequest, requestOptions)
+ .get()
+ .body();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * The list all companies
functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
+ *
+ * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
+ * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
+ * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
+ *
+ * {% admonition type="info" name="Scroll Parameter" %}
+ * You can get the first page of companies by simply sending a GET request to the scroll endpoint.
+ * For subsequent requests you will need to use the scroll parameter from the response.
+ * {% /admonition %}
+ * {% admonition type="danger" name="Scroll network timeouts" %}
+ * Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
+ * "Request failed due to an internal network error. Please restart the scroll operation."
+ * If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> scroll() {
+ return scroll(ScrollCompaniesRequest.builder().build());
+ }
+
+ /**
+ * The list all companies
functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
+ *
+ * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
+ * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
+ * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
+ *
+ * {% admonition type="info" name="Scroll Parameter" %}
+ * You can get the first page of companies by simply sending a GET request to the scroll endpoint.
+ * For subsequent requests you will need to use the scroll parameter from the response.
+ * {% /admonition %}
+ * {% admonition type="danger" name="Scroll network timeouts" %}
+ * Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
+ * "Request failed due to an internal network error. Please restart the scroll operation."
+ * If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> scroll(ScrollCompaniesRequest request) {
+ return scroll(request, null);
+ }
+
+ /**
+ * The list all companies
functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
+ *
+ * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
+ * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
+ * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
+ *
+ * {% admonition type="info" name="Scroll Parameter" %}
+ * You can get the first page of companies by simply sending a GET request to the scroll endpoint.
+ * For subsequent requests you will need to use the scroll parameter from the response.
+ * {% /admonition %}
+ * {% admonition type="danger" name="Scroll network timeouts" %}
+ * Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
+ * "Request failed due to an internal network error. Please restart the scroll operation."
+ * If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> scroll(
+ ScrollCompaniesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies/scroll");
+ if (request.getScrollParam().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "scroll_param", request.getScrollParam().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture>> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ CompanyScroll parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyScroll.class);
+ Optional startingAfter = parsedResponse.getScrollParam();
+ ScrollCompaniesRequest nextRequest = ScrollCompaniesRequest.builder()
+ .from(request)
+ .scrollParam(startingAfter)
+ .build();
+ List result = parsedResponse.getData();
+ future.complete(new IntercomHttpResponse<>(
+ new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
+ try {
+ return scroll(nextRequest, requestOptions)
+ .get()
+ .body();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can attach a company to a single contact.
+ */
+ public CompletableFuture> attachContact(AttachContactToCompanyRequest request) {
+ return attachContact(request, null);
+ }
+
+ /**
+ * You can attach a company to a single contact.
+ */
+ public CompletableFuture> attachContact(
+ AttachContactToCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("companies")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ future.completeExceptionally(new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can detach a company from a single contact.
+ */
+ public CompletableFuture> detachContact(DetachContactFromCompanyRequest request) {
+ return detachContact(request, null);
+ }
+
+ /**
+ * You can detach a company from a single contact.
+ */
+ public CompletableFuture> detachContact(
+ DetachContactFromCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+}
diff --git a/src/main/java/com/intercom/api/resources/companies/CompaniesClient.java b/src/main/java/com/intercom/api/resources/companies/CompaniesClient.java
index 4811c45..1268935 100644
--- a/src/main/java/com/intercom/api/resources/companies/CompaniesClient.java
+++ b/src/main/java/com/intercom/api/resources/companies/CompaniesClient.java
@@ -3,18 +3,9 @@
*/
package com.intercom.api.resources.companies;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
import com.intercom.api.core.pagination.SyncPagingIterable;
-import com.intercom.api.errors.BadRequestError;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.companies.requests.AttachContactToCompanyRequest;
import com.intercom.api.resources.companies.requests.CreateOrUpdateCompanyRequest;
import com.intercom.api.resources.companies.requests.DeleteCompanyRequest;
@@ -30,25 +21,23 @@
import com.intercom.api.types.CompanyAttachedContacts;
import com.intercom.api.types.CompanyAttachedSegments;
import com.intercom.api.types.CompanyList;
-import com.intercom.api.types.CompanyScroll;
import com.intercom.api.types.DeletedCompanyObject;
-import com.intercom.api.types.Error;
-import java.io.IOException;
-import java.util.List;
-import java.util.Optional;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
public class CompaniesClient {
protected final ClientOptions clientOptions;
+ private final RawCompaniesClient rawClient;
+
public CompaniesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new RawCompaniesClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public RawCompaniesClient withRawResponse() {
+ return this.rawClient;
}
/**
@@ -60,7 +49,7 @@ public CompaniesClient(ClientOptions clientOptions) {
* https://api.intercom.io/companies?segment_id={segment_id}
*/
public CompanyList retrieve() {
- return retrieve(RetrieveCompanyRequest.builder().build());
+ return this.rawClient.retrieve().body();
}
/**
@@ -72,7 +61,7 @@ public CompanyList retrieve() {
* https://api.intercom.io/companies?segment_id={segment_id}
*/
public CompanyList retrieve(RetrieveCompanyRequest request) {
- return retrieve(request, null);
+ return this.rawClient.retrieve(request).body();
}
/**
@@ -84,68 +73,7 @@ public CompanyList retrieve(RetrieveCompanyRequest request) {
* https://api.intercom.io/companies?segment_id={segment_id}
*/
public CompanyList retrieve(RetrieveCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies");
- if (request.getName().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "name", request.getName().get(), false);
- }
- if (request.getCompanyId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "company_id", request.getCompanyId().get(), false);
- }
- if (request.getTagId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "tag_id", request.getTagId().get(), false);
- }
- if (request.getSegmentId().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "segment_id", request.getSegmentId().get(), false);
- }
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.retrieve(request, requestOptions).body();
}
/**
@@ -157,7 +85,7 @@ public CompanyList retrieve(RetrieveCompanyRequest request, RequestOptions reque
* {% /admonition %}
*/
public Company createOrUpdate() {
- return createOrUpdate(CreateOrUpdateCompanyRequest.builder().build());
+ return this.rawClient.createOrUpdate().body();
}
/**
@@ -169,7 +97,7 @@ public Company createOrUpdate() {
* {% /admonition %}
*/
public Company createOrUpdate(CreateOrUpdateCompanyRequest request) {
- return createOrUpdate(request, null);
+ return this.rawClient.createOrUpdate(request).body();
}
/**
@@ -181,106 +109,21 @@ public Company createOrUpdate(CreateOrUpdateCompanyRequest request) {
* {% /admonition %}
*/
public Company createOrUpdate(CreateOrUpdateCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.createOrUpdate(request, requestOptions).body();
}
/**
* You can fetch a single company.
*/
public Company find(FindCompanyRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).body();
}
/**
* You can fetch a single company.
*/
public Company find(FindCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.find(request, requestOptions).body();
}
/**
@@ -290,7 +133,7 @@ public Company find(FindCompanyRequest request, RequestOptions requestOptions) {
* {% /admonition %}
*/
public Company update(UpdateCompanyRequest request) {
- return update(request, null);
+ return this.rawClient.update(request).body();
}
/**
@@ -300,106 +143,28 @@ public Company update(UpdateCompanyRequest request) {
* {% /admonition %}
*/
public Company update(UpdateCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("PUT", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.update(request, requestOptions).body();
}
/**
* You can delete a single company.
*/
public DeletedCompanyObject delete(DeleteCompanyRequest request) {
- return delete(request, null);
+ return this.rawClient.delete(request).body();
}
/**
* You can delete a single company.
*/
public DeletedCompanyObject delete(DeleteCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedCompanyObject.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.delete(request, requestOptions).body();
}
/**
* You can fetch a list of all contacts that belong to a company.
*/
public CompanyAttachedContacts listAttachedContacts(ListAttachedContactsRequest request) {
- return listAttachedContacts(request, null);
+ return this.rawClient.listAttachedContacts(request).body();
}
/**
@@ -407,61 +172,14 @@ public CompanyAttachedContacts listAttachedContacts(ListAttachedContactsRequest
*/
public CompanyAttachedContacts listAttachedContacts(
ListAttachedContactsRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .addPathSegments("contacts");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyAttachedContacts.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.listAttachedContacts(request, requestOptions).body();
}
/**
* You can fetch a list of all segments that belong to a company.
*/
public CompanyAttachedSegments listAttachedSegments(ListSegmentsAttachedToCompanyRequest request) {
- return listAttachedSegments(request, null);
+ return this.rawClient.listAttachedSegments(request).body();
}
/**
@@ -469,47 +187,7 @@ public CompanyAttachedSegments listAttachedSegments(ListSegmentsAttachedToCompan
*/
public CompanyAttachedSegments listAttachedSegments(
ListSegmentsAttachedToCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .addPathSegments("segments")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyAttachedSegments.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.listAttachedSegments(request, requestOptions).body();
}
/**
@@ -522,7 +200,7 @@ public CompanyAttachedSegments listAttachedSegments(
* {% /admonition %}
*/
public SyncPagingIterable list() {
- return list(ListCompaniesRequest.builder().build());
+ return this.rawClient.list().body();
}
/**
@@ -535,7 +213,7 @@ public SyncPagingIterable list() {
* {% /admonition %}
*/
public SyncPagingIterable list(ListCompaniesRequest request) {
- return list(request, null);
+ return this.rawClient.list(request).body();
}
/**
@@ -548,60 +226,7 @@ public SyncPagingIterable list(ListCompaniesRequest request) {
* {% /admonition %}
*/
public SyncPagingIterable list(ListCompaniesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies/list");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- if (request.getOrder().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "order", request.getOrder().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("POST", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- CompanyList parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class);
- int newPageNumber = request.getPage().map(page -> page + 1).orElse(1);
- ListCompaniesRequest nextRequest = ListCompaniesRequest.builder()
- .from(request)
- .page(newPageNumber)
- .build();
- List result = parsedResponse.getData();
- return new SyncPagingIterable(true, result, () -> list(nextRequest, requestOptions));
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.list(request, requestOptions).body();
}
/**
@@ -622,7 +247,7 @@ public SyncPagingIterable list(ListCompaniesRequest request, RequestOpt
* {% /admonition %}
*/
public SyncPagingIterable scroll() {
- return scroll(ScrollCompaniesRequest.builder().build());
+ return this.rawClient.scroll().body();
}
/**
@@ -643,7 +268,7 @@ public SyncPagingIterable scroll() {
* {% /admonition %}
*/
public SyncPagingIterable scroll(ScrollCompaniesRequest request) {
- return scroll(request, null);
+ return this.rawClient.scroll(request).body();
}
/**
@@ -664,171 +289,34 @@ public SyncPagingIterable scroll(ScrollCompaniesRequest request) {
* {% /admonition %}
*/
public SyncPagingIterable scroll(ScrollCompaniesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("companies/scroll");
- if (request.getScrollParam().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "scroll_param", request.getScrollParam().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- CompanyScroll parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyScroll.class);
- Optional startingAfter = parsedResponse.getScrollParam();
- ScrollCompaniesRequest nextRequest = ScrollCompaniesRequest.builder()
- .from(request)
- .scrollParam(startingAfter)
- .build();
- List result = parsedResponse.getData();
- return new SyncPagingIterable(
- startingAfter.isPresent(), result, () -> scroll(nextRequest, requestOptions));
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.scroll(request, requestOptions).body();
}
/**
* You can attach a company to a single contact.
*/
public Company attachContact(AttachContactToCompanyRequest request) {
- return attachContact(request, null);
+ return this.rawClient.attachContact(request).body();
}
/**
* You can attach a company to a single contact.
*/
public Company attachContact(AttachContactToCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("companies")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.attachContact(request, requestOptions).body();
}
/**
* You can detach a company from a single contact.
*/
public Company detachContact(DetachContactFromCompanyRequest request) {
- return detachContact(request, null);
+ return this.rawClient.detachContact(request).body();
}
/**
* You can detach a company from a single contact.
*/
public Company detachContact(DetachContactFromCompanyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("companies")
- .addPathSegment(request.getCompanyId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
- case 404:
- throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
- } catch (IOException e) {
- throw new IntercomException("Network error executing HTTP request", e);
- }
+ return this.rawClient.detachContact(request, requestOptions).body();
}
}
diff --git a/src/main/java/com/intercom/api/resources/companies/RawCompaniesClient.java b/src/main/java/com/intercom/api/resources/companies/RawCompaniesClient.java
new file mode 100644
index 0000000..3b48b7b
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/companies/RawCompaniesClient.java
@@ -0,0 +1,881 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.companies;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.core.pagination.SyncPagingIterable;
+import com.intercom.api.errors.BadRequestError;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.companies.requests.AttachContactToCompanyRequest;
+import com.intercom.api.resources.companies.requests.CreateOrUpdateCompanyRequest;
+import com.intercom.api.resources.companies.requests.DeleteCompanyRequest;
+import com.intercom.api.resources.companies.requests.DetachContactFromCompanyRequest;
+import com.intercom.api.resources.companies.requests.FindCompanyRequest;
+import com.intercom.api.resources.companies.requests.ListAttachedContactsRequest;
+import com.intercom.api.resources.companies.requests.ListCompaniesRequest;
+import com.intercom.api.resources.companies.requests.ListSegmentsAttachedToCompanyRequest;
+import com.intercom.api.resources.companies.requests.RetrieveCompanyRequest;
+import com.intercom.api.resources.companies.requests.ScrollCompaniesRequest;
+import com.intercom.api.resources.companies.requests.UpdateCompanyRequest;
+import com.intercom.api.resources.companies.types.Company;
+import com.intercom.api.types.CompanyAttachedContacts;
+import com.intercom.api.types.CompanyAttachedSegments;
+import com.intercom.api.types.CompanyList;
+import com.intercom.api.types.CompanyScroll;
+import com.intercom.api.types.DeletedCompanyObject;
+import com.intercom.api.types.Error;
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
+public class RawCompaniesClient {
+ protected final ClientOptions clientOptions;
+
+ public RawCompaniesClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can fetch a single company by passing in company_id
or name
.
+ * https://api.intercom.io/companies?name={name}
+ * https://api.intercom.io/companies?company_id={company_id}
+ * You can fetch all companies and filter by segment_id
or tag_id
as a query parameter.
+ * https://api.intercom.io/companies?tag_id={tag_id}
+ * https://api.intercom.io/companies?segment_id={segment_id}
+ */
+ public IntercomHttpResponse retrieve() {
+ return retrieve(RetrieveCompanyRequest.builder().build());
+ }
+
+ /**
+ * You can fetch a single company by passing in company_id
or name
.
+ * https://api.intercom.io/companies?name={name}
+ * https://api.intercom.io/companies?company_id={company_id}
+ * You can fetch all companies and filter by segment_id
or tag_id
as a query parameter.
+ * https://api.intercom.io/companies?tag_id={tag_id}
+ * https://api.intercom.io/companies?segment_id={segment_id}
+ */
+ public IntercomHttpResponse retrieve(RetrieveCompanyRequest request) {
+ return retrieve(request, null);
+ }
+
+ /**
+ * You can fetch a single company by passing in company_id
or name
.
+ * https://api.intercom.io/companies?name={name}
+ * https://api.intercom.io/companies?company_id={company_id}
+ * You can fetch all companies and filter by segment_id
or tag_id
as a query parameter.
+ * https://api.intercom.io/companies?tag_id={tag_id}
+ * https://api.intercom.io/companies?segment_id={segment_id}
+ */
+ public IntercomHttpResponse retrieve(RetrieveCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies");
+ if (request.getName().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "name", request.getName().get(), false);
+ }
+ if (request.getCompanyId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "company_id", request.getCompanyId().get(), false);
+ }
+ if (request.getTagId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "tag_id", request.getTagId().get(), false);
+ }
+ if (request.getSegmentId().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "segment_id", request.getSegmentId().get(), false);
+ }
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can create or update a company.
+ * Companies will be only visible in Intercom when there is at least one associated user.
+ * Companies are looked up via company_id
in a POST
request, if not found via company_id
, the new company will be created, if found, that company will be updated.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * You can set a unique company_id
value when creating a company. However, it is not possible to update company_id
. Be sure to set a unique value once upon creation of the company.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse createOrUpdate() {
+ return createOrUpdate(CreateOrUpdateCompanyRequest.builder().build());
+ }
+
+ /**
+ * You can create or update a company.
+ * Companies will be only visible in Intercom when there is at least one associated user.
+ * Companies are looked up via company_id
in a POST
request, if not found via company_id
, the new company will be created, if found, that company will be updated.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * You can set a unique company_id
value when creating a company. However, it is not possible to update company_id
. Be sure to set a unique value once upon creation of the company.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse createOrUpdate(CreateOrUpdateCompanyRequest request) {
+ return createOrUpdate(request, null);
+ }
+
+ /**
+ * You can create or update a company.
+ * Companies will be only visible in Intercom when there is at least one associated user.
+ * Companies are looked up via company_id
in a POST
request, if not found via company_id
, the new company will be created, if found, that company will be updated.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * You can set a unique company_id
value when creating a company. However, it is not possible to update company_id
. Be sure to set a unique value once upon creation of the company.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse createOrUpdate(
+ CreateOrUpdateCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can fetch a single company.
+ */
+ public IntercomHttpResponse find(FindCompanyRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can fetch a single company.
+ */
+ public IntercomHttpResponse find(FindCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can update a single company using the Intercom provisioned id
.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * When updating a company it is not possible to update company_id
. This can only be set once upon creation of the company.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse update(UpdateCompanyRequest request) {
+ return update(request, null);
+ }
+
+ /**
+ * You can update a single company using the Intercom provisioned id
.
+ * {% admonition type="warning" name="Using company_id
" %}
+ * When updating a company it is not possible to update company_id
. This can only be set once upon creation of the company.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse update(UpdateCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", RequestBody.create("", null))
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can delete a single company.
+ */
+ public IntercomHttpResponse delete(DeleteCompanyRequest request) {
+ return delete(request, null);
+ }
+
+ /**
+ * You can delete a single company.
+ */
+ public IntercomHttpResponse delete(
+ DeleteCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletedCompanyObject.class),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can fetch a list of all contacts that belong to a company.
+ */
+ public IntercomHttpResponse listAttachedContacts(ListAttachedContactsRequest request) {
+ return listAttachedContacts(request, null);
+ }
+
+ /**
+ * You can fetch a list of all contacts that belong to a company.
+ */
+ public IntercomHttpResponse listAttachedContacts(
+ ListAttachedContactsRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .addPathSegments("contacts");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyAttachedContacts.class),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can fetch a list of all segments that belong to a company.
+ */
+ public IntercomHttpResponse listAttachedSegments(
+ ListSegmentsAttachedToCompanyRequest request) {
+ return listAttachedSegments(request, null);
+ }
+
+ /**
+ * You can fetch a list of all segments that belong to a company.
+ */
+ public IntercomHttpResponse listAttachedSegments(
+ ListSegmentsAttachedToCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .addPathSegments("segments")
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyAttachedSegments.class),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can list companies. The company list is sorted by the last_request_at
field and by default is ordered descending, most recently requested first.
+ * Note that the API does not include companies who have no associated users in list responses.
+ * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the Scroll API.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 20
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse> list() {
+ return list(ListCompaniesRequest.builder().build());
+ }
+
+ /**
+ * You can list companies. The company list is sorted by the last_request_at
field and by default is ordered descending, most recently requested first.
+ * Note that the API does not include companies who have no associated users in list responses.
+ * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the Scroll API.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 20
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse> list(ListCompaniesRequest request) {
+ return list(request, null);
+ }
+
+ /**
+ * You can list companies. The company list is sorted by the last_request_at
field and by default is ordered descending, most recently requested first.
+ * Note that the API does not include companies who have no associated users in list responses.
+ * When using the Companies endpoint and the pages object to iterate through the returned companies, there is a limit of 10,000 Companies that can be returned. If you need to list or iterate on more than 10,000 Companies, please use the Scroll API.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 20
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse> list(
+ ListCompaniesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies/list");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ if (request.getOrder().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "order", request.getOrder().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("POST", RequestBody.create("", null))
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ CompanyList parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyList.class);
+ int newPageNumber = request.getPage().map(page -> page + 1).orElse(1);
+ ListCompaniesRequest nextRequest = ListCompaniesRequest.builder()
+ .from(request)
+ .page(newPageNumber)
+ .build();
+ List result = parsedResponse.getData();
+ return new IntercomHttpResponse<>(
+ new SyncPagingIterable(true, result, () -> list(nextRequest, requestOptions)
+ .body()),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * The list all companies
functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
+ *
+ * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
+ * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
+ * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
+ *
+ * {% admonition type="info" name="Scroll Parameter" %}
+ * You can get the first page of companies by simply sending a GET request to the scroll endpoint.
+ * For subsequent requests you will need to use the scroll parameter from the response.
+ * {% /admonition %}
+ * {% admonition type="danger" name="Scroll network timeouts" %}
+ * Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
+ * "Request failed due to an internal network error. Please restart the scroll operation."
+ * If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse> scroll() {
+ return scroll(ScrollCompaniesRequest.builder().build());
+ }
+
+ /**
+ * The list all companies
functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
+ *
+ * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
+ * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
+ * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
+ *
+ * {% admonition type="info" name="Scroll Parameter" %}
+ * You can get the first page of companies by simply sending a GET request to the scroll endpoint.
+ * For subsequent requests you will need to use the scroll parameter from the response.
+ * {% /admonition %}
+ * {% admonition type="danger" name="Scroll network timeouts" %}
+ * Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
+ * "Request failed due to an internal network error. Please restart the scroll operation."
+ * If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse> scroll(ScrollCompaniesRequest request) {
+ return scroll(request, null);
+ }
+
+ /**
+ * The list all companies
functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply. The Scroll API provides an efficient mechanism for iterating over all companies in a dataset.
+ *
+ * - Each app can only have 1 scroll open at a time. You'll get an error message if you try to have more than one open per app.
+ * - If the scroll isn't used for 1 minute, it expires and calls with that scroll param will fail
+ * - If the end of the scroll is reached, "companies" will be empty and the scroll parameter will expire
+ *
+ * {% admonition type="info" name="Scroll Parameter" %}
+ * You can get the first page of companies by simply sending a GET request to the scroll endpoint.
+ * For subsequent requests you will need to use the scroll parameter from the response.
+ * {% /admonition %}
+ * {% admonition type="danger" name="Scroll network timeouts" %}
+ * Since scroll is often used on large datasets network errors such as timeouts can be encountered. When this occurs you will see a HTTP 500 error with the following message:
+ * "Request failed due to an internal network error. Please restart the scroll operation."
+ * If this happens, you will need to restart your scroll query: It is not possible to continue from a specific point when using scroll.
+ * {% /admonition %}
+ */
+ public IntercomHttpResponse> scroll(
+ ScrollCompaniesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("companies/scroll");
+ if (request.getScrollParam().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "scroll_param", request.getScrollParam().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ CompanyScroll parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompanyScroll.class);
+ Optional startingAfter = parsedResponse.getScrollParam();
+ ScrollCompaniesRequest nextRequest = ScrollCompaniesRequest.builder()
+ .from(request)
+ .scrollParam(startingAfter)
+ .build();
+ List result = parsedResponse.getData();
+ return new IntercomHttpResponse<>(
+ new SyncPagingIterable(
+ startingAfter.isPresent(), result, () -> scroll(nextRequest, requestOptions)
+ .body()),
+ response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can attach a company to a single contact.
+ */
+ public IntercomHttpResponse attachContact(AttachContactToCompanyRequest request) {
+ return attachContact(request, null);
+ }
+
+ /**
+ * You can attach a company to a single contact.
+ */
+ public IntercomHttpResponse attachContact(
+ AttachContactToCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("companies")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+
+ /**
+ * You can detach a company from a single contact.
+ */
+ public IntercomHttpResponse detachContact(DetachContactFromCompanyRequest request) {
+ return detachContact(request, null);
+ }
+
+ /**
+ * You can detach a company from a single contact.
+ */
+ public IntercomHttpResponse detachContact(
+ DetachContactFromCompanyRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("companies")
+ .addPathSegment(request.getCompanyId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Company.class), response);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
+ case 404:
+ throw new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response);
+ } catch (IOException e) {
+ throw new IntercomException("Network error executing HTTP request", e);
+ }
+ }
+}
diff --git a/src/main/java/com/intercom/api/resources/contacts/AsyncContactsClient.java b/src/main/java/com/intercom/api/resources/contacts/AsyncContactsClient.java
index 9d09d5f..f2a70d1 100644
--- a/src/main/java/com/intercom/api/resources/contacts/AsyncContactsClient.java
+++ b/src/main/java/com/intercom/api/resources/contacts/AsyncContactsClient.java
@@ -3,17 +3,9 @@
*/
package com.intercom.api.resources.contacts;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.intercom.api.core.ClientOptions;
-import com.intercom.api.core.IntercomApiException;
-import com.intercom.api.core.IntercomException;
-import com.intercom.api.core.MediaTypes;
-import com.intercom.api.core.ObjectMappers;
-import com.intercom.api.core.QueryStringMapper;
import com.intercom.api.core.RequestOptions;
import com.intercom.api.core.pagination.SyncPagingIterable;
-import com.intercom.api.errors.NotFoundError;
-import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.resources.companies.types.Company;
import com.intercom.api.resources.contacts.requests.ArchiveContactRequest;
import com.intercom.api.resources.contacts.requests.AttachSubscriptionToContactRequest;
@@ -31,46 +23,37 @@
import com.intercom.api.resources.contacts.types.Contact;
import com.intercom.api.resources.subscriptiontypes.types.SubscriptionType;
import com.intercom.api.types.ContactArchived;
-import com.intercom.api.types.ContactAttachedCompanies;
import com.intercom.api.types.ContactDeleted;
-import com.intercom.api.types.ContactList;
import com.intercom.api.types.ContactSegments;
import com.intercom.api.types.ContactUnarchived;
import com.intercom.api.types.CreateContactRequest;
-import com.intercom.api.types.CursorPages;
-import com.intercom.api.types.Error;
import com.intercom.api.types.SearchRequest;
-import com.intercom.api.types.StartingAfterPaging;
import com.intercom.api.types.SubscriptionTypeList;
import com.intercom.api.types.TagList;
-import java.io.IOException;
-import java.util.List;
-import java.util.Optional;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.jetbrains.annotations.NotNull;
public class AsyncContactsClient {
protected final ClientOptions clientOptions;
+ private final AsyncRawContactsClient rawClient;
+
public AsyncContactsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
+ this.rawClient = new AsyncRawContactsClient(clientOptions);
+ }
+
+ /**
+ * Get responses with HTTP metadata like headers
+ */
+ public AsyncRawContactsClient withRawResponse() {
+ return this.rawClient;
}
/**
* You can fetch a list of companies that are associated to a contact.
*/
public CompletableFuture> listAttachedCompanies(ListAttachedCompaniesRequest request) {
- return listAttachedCompanies(request, null);
+ return this.rawClient.listAttachedCompanies(request).thenApply(response -> response.body());
}
/**
@@ -78,93 +61,14 @@ public CompletableFuture> listAttachedCompanies(List
*/
public CompletableFuture> listAttachedCompanies(
ListAttachedCompaniesRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("companies");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- ContactAttachedCompanies parsedResponse = ObjectMappers.JSON_MAPPER.readValue(
- responseBody.string(), ContactAttachedCompanies.class);
- int newPageNumber =
- request.getPage().map(page -> page + 1).orElse(1);
- ListAttachedCompaniesRequest nextRequest = ListAttachedCompaniesRequest.builder()
- .from(request)
- .page(newPageNumber)
- .build();
- List result = parsedResponse.getCompanies();
- future.complete(new SyncPagingIterable(true, result, () -> {
- try {
- return listAttachedCompanies(nextRequest, requestOptions)
- .get();
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAttachedCompanies(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch a list of segments that are associated to a contact.
*/
public CompletableFuture listAttachedSegments(ListSegmentsAttachedToContactRequest request) {
- return listAttachedSegments(request, null);
+ return this.rawClient.listAttachedSegments(request).thenApply(response -> response.body());
}
/**
@@ -172,64 +76,7 @@ public CompletableFuture listAttachedSegments(ListSegmentsAttac
*/
public CompletableFuture listAttachedSegments(
ListSegmentsAttachedToContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("segments")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactSegments.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAttachedSegments(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -240,7 +87,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* 2.Opt-in subscription types that the user has opted-in to receiving.
*/
public CompletableFuture listAttachedSubscriptions(ListAttachedSubscriptionsRequest request) {
- return listAttachedSubscriptions(request, null);
+ return this.rawClient.listAttachedSubscriptions(request).thenApply(response -> response.body());
}
/**
@@ -252,64 +99,7 @@ public CompletableFuture listAttachedSubscriptions(ListAtt
*/
public CompletableFuture listAttachedSubscriptions(
ListAttachedSubscriptionsRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("subscriptions")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubscriptionTypeList.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAttachedSubscriptions(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -319,7 +109,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* This will return a subscription type model for the subscription type that was added to the contact.
*/
public CompletableFuture attachSubscription(AttachSubscriptionToContactRequest request) {
- return attachSubscription(request, null);
+ return this.rawClient.attachSubscription(request).thenApply(response -> response.body());
}
/**
@@ -330,78 +120,14 @@ public CompletableFuture attachSubscription(AttachSubscription
*/
public CompletableFuture attachSubscription(
AttachSubscriptionToContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("subscriptions")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubscriptionType.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.attachSubscription(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can remove a specific subscription from a contact. This will return a subscription type model for the subscription type that was removed from the contact.
*/
public CompletableFuture detachSubscription(DetachSubscriptionFromContactRequest request) {
- return detachSubscription(request, null);
+ return this.rawClient.detachSubscription(request).thenApply(response -> response.body());
}
/**
@@ -409,72 +135,14 @@ public CompletableFuture detachSubscription(DetachSubscription
*/
public CompletableFuture detachSubscription(
DetachSubscriptionFromContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("subscriptions")
- .addPathSegment(request.getSubscriptionId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubscriptionType.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.detachSubscription(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch a list of all tags that are attached to a specific contact.
*/
public CompletableFuture listAttachedTags(ListTagsAttachedToContactRequest request) {
- return listAttachedTags(request, null);
+ return this.rawClient.listAttachedTags(request).thenApply(response -> response.body());
}
/**
@@ -482,333 +150,63 @@ public CompletableFuture listAttachedTags(ListTagsAttachedToContactRequ
*/
public CompletableFuture listAttachedTags(
ListTagsAttachedToContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("tags")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), TagList.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.listAttachedTags(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can fetch the details of a single contact.
*/
public CompletableFuture find(FindContactRequest request) {
- return find(request, null);
+ return this.rawClient.find(request).thenApply(response -> response.body());
}
/**
* You can fetch the details of a single contact.
*/
public CompletableFuture find(FindContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.find(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can update an existing contact (ie. user or lead).
*/
public CompletableFuture update(UpdateContactRequest request) {
- return update(request, null);
+ return this.rawClient.update(request).thenApply(response -> response.body());
}
/**
* You can update an existing contact (ie. user or lead).
*/
public CompletableFuture update(UpdateContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("PUT", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.update(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can delete a single contact.
*/
public CompletableFuture delete(DeleteContactRequest request) {
- return delete(request, null);
+ return this.rawClient.delete(request).thenApply(response -> response.body());
}
/**
* You can delete a single contact.
*/
public CompletableFuture delete(DeleteContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("DELETE", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactDeleted.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.delete(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can merge a contact with a role
of lead
into a contact with a role
of user
.
*/
public CompletableFuture mergeLeadInUser(MergeContactsRequest request) {
- return mergeLeadInUser(request, null);
+ return this.rawClient.mergeLeadInUser(request).thenApply(response -> response.body());
}
/**
* You can merge a contact with a role
of lead
into a contact with a role
of user
.
*/
public CompletableFuture mergeLeadInUser(MergeContactsRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts/merge")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.mergeLeadInUser(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -900,7 +298,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* | $ | String | Ends With |
*/
public CompletableFuture> search(SearchRequest request) {
- return search(request, null);
+ return this.rawClient.search(request).thenApply(response -> response.body());
}
/**
@@ -992,85 +390,7 @@ public CompletableFuture> search(SearchRequest reque
* | $ | String | Ends With |
*/
public CompletableFuture> search(SearchRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts/search")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- ContactList parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactList.class);
- Optional startingAfter = parsedResponse
- .getPages()
- .flatMap(CursorPages::getNext)
- .flatMap(StartingAfterPaging::getStartingAfter);
- Optional pagination = request.getPagination()
- .map(pagination_ -> StartingAfterPaging.builder()
- .from(pagination_)
- .startingAfter(startingAfter)
- .build());
- SearchRequest nextRequest = SearchRequest.builder()
- .from(request)
- .pagination(pagination)
- .build();
- List result = parsedResponse.getData();
- future.complete(new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
- try {
- return search(nextRequest, requestOptions).get();
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.search(request, requestOptions).thenApply(response -> response.body());
}
/**
@@ -1081,7 +401,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
* {% /admonition %}
*/
public CompletableFuture> list() {
- return list(ListContactsRequest.builder().build());
+ return this.rawClient.list().thenApply(response -> response.body());
}
/**
@@ -1092,7 +412,7 @@ public CompletableFuture> list() {
* {% /admonition %}
*/
public CompletableFuture> list(ListContactsRequest request) {
- return list(request, null);
+ return this.rawClient.list(request).thenApply(response -> response.body());
}
/**
@@ -1104,218 +424,42 @@ public CompletableFuture> list(ListContactsRequest r
*/
public CompletableFuture> list(
ListContactsRequest request, RequestOptions requestOptions) {
- HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts");
- if (request.getPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "page", request.getPage().get().toString(), false);
- }
- if (request.getPerPage().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "per_page", request.getPerPage().get().toString(), false);
- }
- if (request.getStartingAfter().isPresent()) {
- QueryStringMapper.addQueryParameter(
- httpUrl, "starting_after", request.getStartingAfter().get(), false);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl.build())
- .method("GET", null)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- ContactList parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactList.class);
- Optional startingAfter = parsedResponse
- .getPages()
- .flatMap(CursorPages::getNext)
- .flatMap(StartingAfterPaging::getStartingAfter);
- ListContactsRequest nextRequest = ListContactsRequest.builder()
- .from(request)
- .startingAfter(startingAfter)
- .build();
- List result = parsedResponse.getData();
- future.complete(new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
- try {
- return list(nextRequest, requestOptions).get();
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.list(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can create a new contact (ie. user or lead).
*/
public CompletableFuture create(CreateContactRequest request) {
- return create(request, null);
+ return this.rawClient.create(request).thenApply(response -> response.body());
}
/**
* You can create a new contact (ie. user or lead).
*/
public CompletableFuture create(CreateContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new IntercomException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- if (response.code() == 401) {
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class)));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.create(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can archive a single contact.
*/
public CompletableFuture archive(ArchiveContactRequest request) {
- return archive(request, null);
+ return this.rawClient.archive(request).thenApply(response -> response.body());
}
/**
* You can archive a single contact.
*/
public CompletableFuture archive(ArchiveContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("archive")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("POST", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactArchived.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.archive(request, requestOptions).thenApply(response -> response.body());
}
/**
* You can unarchive a single contact.
*/
public CompletableFuture unarchive(UnarchiveContactRequest request) {
- return unarchive(request, null);
+ return this.rawClient.unarchive(request).thenApply(response -> response.body());
}
/**
@@ -1323,49 +467,6 @@ public CompletableFuture unarchive(UnarchiveContactRequest re
*/
public CompletableFuture unarchive(
UnarchiveContactRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("contacts")
- .addPathSegment(request.getContactId())
- .addPathSegments("unarchive")
- .build();
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("POST", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactUnarchived.class));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- future.completeExceptionally(new IntercomApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
- }
- });
- return future;
+ return this.rawClient.unarchive(request, requestOptions).thenApply(response -> response.body());
}
}
diff --git a/src/main/java/com/intercom/api/resources/contacts/AsyncRawContactsClient.java b/src/main/java/com/intercom/api/resources/contacts/AsyncRawContactsClient.java
new file mode 100644
index 0000000..28dc93a
--- /dev/null
+++ b/src/main/java/com/intercom/api/resources/contacts/AsyncRawContactsClient.java
@@ -0,0 +1,1434 @@
+/**
+ * This file was auto-generated by Fern from our API Definition.
+ */
+package com.intercom.api.resources.contacts;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.intercom.api.core.ClientOptions;
+import com.intercom.api.core.IntercomApiException;
+import com.intercom.api.core.IntercomException;
+import com.intercom.api.core.IntercomHttpResponse;
+import com.intercom.api.core.MediaTypes;
+import com.intercom.api.core.ObjectMappers;
+import com.intercom.api.core.QueryStringMapper;
+import com.intercom.api.core.RequestOptions;
+import com.intercom.api.core.pagination.SyncPagingIterable;
+import com.intercom.api.errors.NotFoundError;
+import com.intercom.api.errors.UnauthorizedError;
+import com.intercom.api.resources.companies.types.Company;
+import com.intercom.api.resources.contacts.requests.ArchiveContactRequest;
+import com.intercom.api.resources.contacts.requests.AttachSubscriptionToContactRequest;
+import com.intercom.api.resources.contacts.requests.DeleteContactRequest;
+import com.intercom.api.resources.contacts.requests.DetachSubscriptionFromContactRequest;
+import com.intercom.api.resources.contacts.requests.FindContactRequest;
+import com.intercom.api.resources.contacts.requests.ListAttachedCompaniesRequest;
+import com.intercom.api.resources.contacts.requests.ListAttachedSubscriptionsRequest;
+import com.intercom.api.resources.contacts.requests.ListContactsRequest;
+import com.intercom.api.resources.contacts.requests.ListSegmentsAttachedToContactRequest;
+import com.intercom.api.resources.contacts.requests.ListTagsAttachedToContactRequest;
+import com.intercom.api.resources.contacts.requests.MergeContactsRequest;
+import com.intercom.api.resources.contacts.requests.UnarchiveContactRequest;
+import com.intercom.api.resources.contacts.requests.UpdateContactRequest;
+import com.intercom.api.resources.contacts.types.Contact;
+import com.intercom.api.resources.subscriptiontypes.types.SubscriptionType;
+import com.intercom.api.types.ContactArchived;
+import com.intercom.api.types.ContactAttachedCompanies;
+import com.intercom.api.types.ContactDeleted;
+import com.intercom.api.types.ContactList;
+import com.intercom.api.types.ContactSegments;
+import com.intercom.api.types.ContactUnarchived;
+import com.intercom.api.types.CreateContactRequest;
+import com.intercom.api.types.CursorPages;
+import com.intercom.api.types.Error;
+import com.intercom.api.types.SearchRequest;
+import com.intercom.api.types.StartingAfterPaging;
+import com.intercom.api.types.SubscriptionTypeList;
+import com.intercom.api.types.TagList;
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.jetbrains.annotations.NotNull;
+
+public class AsyncRawContactsClient {
+ protected final ClientOptions clientOptions;
+
+ public AsyncRawContactsClient(ClientOptions clientOptions) {
+ this.clientOptions = clientOptions;
+ }
+
+ /**
+ * You can fetch a list of companies that are associated to a contact.
+ */
+ public CompletableFuture>> listAttachedCompanies(
+ ListAttachedCompaniesRequest request) {
+ return listAttachedCompanies(request, null);
+ }
+
+ /**
+ * You can fetch a list of companies that are associated to a contact.
+ */
+ public CompletableFuture>> listAttachedCompanies(
+ ListAttachedCompaniesRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("companies");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture>> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ ContactAttachedCompanies parsedResponse = ObjectMappers.JSON_MAPPER.readValue(
+ responseBody.string(), ContactAttachedCompanies.class);
+ int newPageNumber =
+ request.getPage().map(page -> page + 1).orElse(1);
+ ListAttachedCompaniesRequest nextRequest = ListAttachedCompaniesRequest.builder()
+ .from(request)
+ .page(newPageNumber)
+ .build();
+ List result = parsedResponse.getCompanies();
+ future.complete(new IntercomHttpResponse<>(
+ new SyncPagingIterable(true, result, () -> {
+ try {
+ return listAttachedCompanies(nextRequest, requestOptions)
+ .get()
+ .body();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of segments that are associated to a contact.
+ */
+ public CompletableFuture> listAttachedSegments(
+ ListSegmentsAttachedToContactRequest request) {
+ return listAttachedSegments(request, null);
+ }
+
+ /**
+ * You can fetch a list of segments that are associated to a contact.
+ */
+ public CompletableFuture> listAttachedSegments(
+ ListSegmentsAttachedToContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("segments")
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactSegments.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of subscription types that are attached to a contact. These can be subscriptions that a user has 'opted-in' to or has 'opted-out' from, depending on the subscription type.
+ * This will return a list of Subscription Type objects that the contact is associated with.
+ * The data property will show a combined list of:
+ * 1.Opt-out subscription types that the user has opted-out from.
+ * 2.Opt-in subscription types that the user has opted-in to receiving.
+ */
+ public CompletableFuture> listAttachedSubscriptions(
+ ListAttachedSubscriptionsRequest request) {
+ return listAttachedSubscriptions(request, null);
+ }
+
+ /**
+ * You can fetch a list of subscription types that are attached to a contact. These can be subscriptions that a user has 'opted-in' to or has 'opted-out' from, depending on the subscription type.
+ * This will return a list of Subscription Type objects that the contact is associated with.
+ * The data property will show a combined list of:
+ * 1.Opt-out subscription types that the user has opted-out from.
+ * 2.Opt-in subscription types that the user has opted-in to receiving.
+ */
+ public CompletableFuture> listAttachedSubscriptions(
+ ListAttachedSubscriptionsRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("subscriptions")
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubscriptionTypeList.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can add a specific subscription to a contact. In Intercom, we have two different subscription types based on user consent - opt-out and opt-in:
+ * 1.Attaching a contact to an opt-out subscription type will opt that user out from receiving messages related to that subscription type.
+ * 2.Attaching a contact to an opt-in subscription type will opt that user in to receiving messages related to that subscription type.
+ * This will return a subscription type model for the subscription type that was added to the contact.
+ */
+ public CompletableFuture> attachSubscription(
+ AttachSubscriptionToContactRequest request) {
+ return attachSubscription(request, null);
+ }
+
+ /**
+ * You can add a specific subscription to a contact. In Intercom, we have two different subscription types based on user consent - opt-out and opt-in:
+ * 1.Attaching a contact to an opt-out subscription type will opt that user out from receiving messages related to that subscription type.
+ * 2.Attaching a contact to an opt-in subscription type will opt that user in to receiving messages related to that subscription type.
+ * This will return a subscription type model for the subscription type that was added to the contact.
+ */
+ public CompletableFuture> attachSubscription(
+ AttachSubscriptionToContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("subscriptions")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubscriptionType.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can remove a specific subscription from a contact. This will return a subscription type model for the subscription type that was removed from the contact.
+ */
+ public CompletableFuture> detachSubscription(
+ DetachSubscriptionFromContactRequest request) {
+ return detachSubscription(request, null);
+ }
+
+ /**
+ * You can remove a specific subscription from a contact. This will return a subscription type model for the subscription type that was removed from the contact.
+ */
+ public CompletableFuture> detachSubscription(
+ DetachSubscriptionFromContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("subscriptions")
+ .addPathSegment(request.getSubscriptionId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubscriptionType.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of all tags that are attached to a specific contact.
+ */
+ public CompletableFuture> listAttachedTags(ListTagsAttachedToContactRequest request) {
+ return listAttachedTags(request, null);
+ }
+
+ /**
+ * You can fetch a list of all tags that are attached to a specific contact.
+ */
+ public CompletableFuture> listAttachedTags(
+ ListTagsAttachedToContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .addPathSegments("tags")
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), TagList.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 401:
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class),
+ response));
+ return;
+ case 404:
+ future.completeExceptionally(new NotFoundError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch the details of a single contact.
+ */
+ public CompletableFuture> find(FindContactRequest request) {
+ return find(request, null);
+ }
+
+ /**
+ * You can fetch the details of a single contact.
+ */
+ public CompletableFuture> find(
+ FindContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can update an existing contact (ie. user or lead).
+ */
+ public CompletableFuture> update(UpdateContactRequest request) {
+ return update(request, null);
+ }
+
+ /**
+ * You can update an existing contact (ie. user or lead).
+ */
+ public CompletableFuture> update(
+ UpdateContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("PUT", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can delete a single contact.
+ */
+ public CompletableFuture> delete(DeleteContactRequest request) {
+ return delete(request, null);
+ }
+
+ /**
+ * You can delete a single contact.
+ */
+ public CompletableFuture> delete(
+ DeleteContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .addPathSegment(request.getContactId())
+ .build();
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("DELETE", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactDeleted.class),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can merge a contact with a role
of lead
into a contact with a role
of user
.
+ */
+ public CompletableFuture> mergeLeadInUser(MergeContactsRequest request) {
+ return mergeLeadInUser(request, null);
+ }
+
+ /**
+ * You can merge a contact with a role
of lead
into a contact with a role
of user
.
+ */
+ public CompletableFuture> mergeLeadInUser(
+ MergeContactsRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts/merge")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can search for multiple contacts by the value of their attributes in order to fetch exactly who you want.
+ * To search for contacts, you need to send a POST
request to https://api.intercom.io/contacts/search
.
+ * This will accept a query object in the body which will define your filters in order to search for contacts.
+ * {% admonition type="warning" name="Optimizing search queries" %}
+ * Search queries can be complex, so optimizing them can help the performance of your search.
+ * Use the AND
and OR
operators to combine multiple filters to get the exact results you need and utilize
+ * pagination to limit the number of results returned. The default is 50
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ * Contact Creation Delay
+ * If a contact has recently been created, there is a possibility that it will not yet be available when searching. This means that it may not appear in the response. This delay can take a few minutes. If you need to be instantly notified it is recommended to use webhooks and iterate to see if they match your search filters.
+ * Nesting & Limitations
+ * You can nest these filters in order to get even more granular insights that pinpoint exactly what you need. Example: (1 OR 2) AND (3 OR 4).
+ * There are some limitations to the amount of multiple's there can be:
+ *
+ * - There's a limit of max 2 nested filters
+ * - There's a limit of max 15 filters for each AND or OR group
+ *
+ * Searching for Timestamp Fields
+ * All timestamp fields (created_at, updated_at etc.) are indexed as Dates for Contact Search queries; Datetime queries are not currently supported. This means you can only query for timestamp fields by day - not hour, minute or second.
+ * For example, if you search for all Contacts with a created_at value greater (>) than 1577869200 (the UNIX timestamp for January 1st, 2020 9:00 AM), that will be interpreted as 1577836800 (January 1st, 2020 12:00 AM). The search results will then include Contacts created from January 2nd, 2020 12:00 AM onwards.
+ * If you'd like to get contacts created on January 1st, 2020 you should search with a created_at value equal (=) to 1577836800 (January 1st, 2020 12:00 AM).
+ * This behaviour applies only to timestamps used in search queries. The search results will still contain the full UNIX timestamp and be sorted accordingly.
+ * Accepted Fields
+ * Most key listed as part of the Contacts Model are searchable, whether writeable or not. The value you search for has to match the accepted type, otherwise the query will fail (ie. as created_at
accepts a date, the value
cannot be a string such as "foorbar"
).
+ * | Field | Type |
+ * | ---------------------------------- | ------------------------------ |
+ * | id | String |
+ * | role | String<br>Accepts user or lead |
+ * | name | String |
+ * | avatar | String |
+ * | owner_id | Integer |
+ * | email | String |
+ * | email_domain | String |
+ * | phone | String |
+ * | external_id | String |
+ * | created_at | Date (UNIX Timestamp) |
+ * | signed_up_at | Date (UNIX Timestamp) |
+ * | updated_at | Date (UNIX Timestamp) |
+ * | last_seen_at | Date (UNIX Timestamp) |
+ * | last_contacted_at | Date (UNIX Timestamp) |
+ * | last_replied_at | Date (UNIX Timestamp) |
+ * | last_email_opened_at | Date (UNIX Timestamp) |
+ * | last_email_clicked_at | Date (UNIX Timestamp) |
+ * | language_override | String |
+ * | browser | String |
+ * | browser_language | String |
+ * | os | String |
+ * | location.country | String |
+ * | location.region | String |
+ * | location.city | String |
+ * | unsubscribed_from_emails | Boolean |
+ * | marked_email_as_spam | Boolean |
+ * | has_hard_bounced | Boolean |
+ * | ios_last_seen_at | Date (UNIX Timestamp) |
+ * | ios_app_version | String |
+ * | ios_device | String |
+ * | ios_app_device | String |
+ * | ios_os_version | String |
+ * | ios_app_name | String |
+ * | ios_sdk_version | String |
+ * | android_last_seen_at | Date (UNIX Timestamp) |
+ * | android_app_version | String |
+ * | android_device | String |
+ * | android_app_name | String |
+ * | andoid_sdk_version | String |
+ * | segment_id | String |
+ * | tag_id | String |
+ * | custom_attributes.{attribute_name} | String |
+ * Accepted Operators
+ * {% admonition type="warning" name="Searching based on created_at
" %}
+ * You cannot use the <=
or >=
operators to search by created_at
.
+ * {% /admonition %}
+ * The table below shows the operators you can use to define how you want to search for the value. The operator should be put in as a string ("="
). The operator has to be compatible with the field's type (eg. you cannot search with >
for a given string value as it's only compatible for integer's and dates).
+ * | Operator | Valid Types | Description |
+ * | :------- | :------------------------------- | :--------------------------------------------------------------- |
+ * | = | All | Equals |
+ * | != | All | Doesn't Equal |
+ * | IN | All | In<br>Shortcut for OR
queries<br>Values must be in Array |
+ * | NIN | All | Not In<br>Shortcut for OR !
queries<br>Values must be in Array |
+ * | > | Integer<br>Date (UNIX Timestamp) | Greater than |
+ * | < | Integer<br>Date (UNIX Timestamp) | Lower than |
+ * | ~ | String | Contains |
+ * | !~ | String | Doesn't Contain |
+ * | ^ | String | Starts With |
+ * | $ | String | Ends With |
+ */
+ public CompletableFuture>> search(SearchRequest request) {
+ return search(request, null);
+ }
+
+ /**
+ * You can search for multiple contacts by the value of their attributes in order to fetch exactly who you want.
+ * To search for contacts, you need to send a POST
request to https://api.intercom.io/contacts/search
.
+ * This will accept a query object in the body which will define your filters in order to search for contacts.
+ * {% admonition type="warning" name="Optimizing search queries" %}
+ * Search queries can be complex, so optimizing them can help the performance of your search.
+ * Use the AND
and OR
operators to combine multiple filters to get the exact results you need and utilize
+ * pagination to limit the number of results returned. The default is 50
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ * Contact Creation Delay
+ * If a contact has recently been created, there is a possibility that it will not yet be available when searching. This means that it may not appear in the response. This delay can take a few minutes. If you need to be instantly notified it is recommended to use webhooks and iterate to see if they match your search filters.
+ * Nesting & Limitations
+ * You can nest these filters in order to get even more granular insights that pinpoint exactly what you need. Example: (1 OR 2) AND (3 OR 4).
+ * There are some limitations to the amount of multiple's there can be:
+ *
+ * - There's a limit of max 2 nested filters
+ * - There's a limit of max 15 filters for each AND or OR group
+ *
+ * Searching for Timestamp Fields
+ * All timestamp fields (created_at, updated_at etc.) are indexed as Dates for Contact Search queries; Datetime queries are not currently supported. This means you can only query for timestamp fields by day - not hour, minute or second.
+ * For example, if you search for all Contacts with a created_at value greater (>) than 1577869200 (the UNIX timestamp for January 1st, 2020 9:00 AM), that will be interpreted as 1577836800 (January 1st, 2020 12:00 AM). The search results will then include Contacts created from January 2nd, 2020 12:00 AM onwards.
+ * If you'd like to get contacts created on January 1st, 2020 you should search with a created_at value equal (=) to 1577836800 (January 1st, 2020 12:00 AM).
+ * This behaviour applies only to timestamps used in search queries. The search results will still contain the full UNIX timestamp and be sorted accordingly.
+ * Accepted Fields
+ * Most key listed as part of the Contacts Model are searchable, whether writeable or not. The value you search for has to match the accepted type, otherwise the query will fail (ie. as created_at
accepts a date, the value
cannot be a string such as "foorbar"
).
+ * | Field | Type |
+ * | ---------------------------------- | ------------------------------ |
+ * | id | String |
+ * | role | String<br>Accepts user or lead |
+ * | name | String |
+ * | avatar | String |
+ * | owner_id | Integer |
+ * | email | String |
+ * | email_domain | String |
+ * | phone | String |
+ * | external_id | String |
+ * | created_at | Date (UNIX Timestamp) |
+ * | signed_up_at | Date (UNIX Timestamp) |
+ * | updated_at | Date (UNIX Timestamp) |
+ * | last_seen_at | Date (UNIX Timestamp) |
+ * | last_contacted_at | Date (UNIX Timestamp) |
+ * | last_replied_at | Date (UNIX Timestamp) |
+ * | last_email_opened_at | Date (UNIX Timestamp) |
+ * | last_email_clicked_at | Date (UNIX Timestamp) |
+ * | language_override | String |
+ * | browser | String |
+ * | browser_language | String |
+ * | os | String |
+ * | location.country | String |
+ * | location.region | String |
+ * | location.city | String |
+ * | unsubscribed_from_emails | Boolean |
+ * | marked_email_as_spam | Boolean |
+ * | has_hard_bounced | Boolean |
+ * | ios_last_seen_at | Date (UNIX Timestamp) |
+ * | ios_app_version | String |
+ * | ios_device | String |
+ * | ios_app_device | String |
+ * | ios_os_version | String |
+ * | ios_app_name | String |
+ * | ios_sdk_version | String |
+ * | android_last_seen_at | Date (UNIX Timestamp) |
+ * | android_app_version | String |
+ * | android_device | String |
+ * | android_app_name | String |
+ * | andoid_sdk_version | String |
+ * | segment_id | String |
+ * | tag_id | String |
+ * | custom_attributes.{attribute_name} | String |
+ * Accepted Operators
+ * {% admonition type="warning" name="Searching based on created_at
" %}
+ * You cannot use the <=
or >=
operators to search by created_at
.
+ * {% /admonition %}
+ * The table below shows the operators you can use to define how you want to search for the value. The operator should be put in as a string ("="
). The operator has to be compatible with the field's type (eg. you cannot search with >
for a given string value as it's only compatible for integer's and dates).
+ * | Operator | Valid Types | Description |
+ * | :------- | :------------------------------- | :--------------------------------------------------------------- |
+ * | = | All | Equals |
+ * | != | All | Doesn't Equal |
+ * | IN | All | In<br>Shortcut for OR
queries<br>Values must be in Array |
+ * | NIN | All | Not In<br>Shortcut for OR !
queries<br>Values must be in Array |
+ * | > | Integer<br>Date (UNIX Timestamp) | Greater than |
+ * | < | Integer<br>Date (UNIX Timestamp) | Lower than |
+ * | ~ | String | Contains |
+ * | !~ | String | Doesn't Contain |
+ * | ^ | String | Starts With |
+ * | $ | String | Ends With |
+ */
+ public CompletableFuture>> search(
+ SearchRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts/search")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture>> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ ContactList parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactList.class);
+ Optional startingAfter = parsedResponse
+ .getPages()
+ .flatMap(CursorPages::getNext)
+ .flatMap(StartingAfterPaging::getStartingAfter);
+ Optional pagination = request.getPagination()
+ .map(pagination_ -> StartingAfterPaging.builder()
+ .from(pagination_)
+ .startingAfter(startingAfter)
+ .build());
+ SearchRequest nextRequest = SearchRequest.builder()
+ .from(request)
+ .pagination(pagination)
+ .build();
+ List result = parsedResponse.getData();
+ future.complete(new IntercomHttpResponse<>(
+ new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
+ try {
+ return search(nextRequest, requestOptions)
+ .get()
+ .body();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can fetch a list of all contacts (ie. users or leads) in your workspace.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 50
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> list() {
+ return list(ListContactsRequest.builder().build());
+ }
+
+ /**
+ * You can fetch a list of all contacts (ie. users or leads) in your workspace.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 50
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> list(ListContactsRequest request) {
+ return list(request, null);
+ }
+
+ /**
+ * You can fetch a list of all contacts (ie. users or leads) in your workspace.
+ * {% admonition type="warning" name="Pagination" %}
+ * You can use pagination to limit the number of results returned. The default is 50
results per page.
+ * See the pagination section for more details on how to use the starting_after
param.
+ * {% /admonition %}
+ */
+ public CompletableFuture>> list(
+ ListContactsRequest request, RequestOptions requestOptions) {
+ HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts");
+ if (request.getPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "page", request.getPage().get().toString(), false);
+ }
+ if (request.getPerPage().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "per_page", request.getPerPage().get().toString(), false);
+ }
+ if (request.getStartingAfter().isPresent()) {
+ QueryStringMapper.addQueryParameter(
+ httpUrl, "starting_after", request.getStartingAfter().get(), false);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl.build())
+ .method("GET", null)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json");
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture>> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ ContactList parsedResponse =
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ContactList.class);
+ Optional startingAfter = parsedResponse
+ .getPages()
+ .flatMap(CursorPages::getNext)
+ .flatMap(StartingAfterPaging::getStartingAfter);
+ ListContactsRequest nextRequest = ListContactsRequest.builder()
+ .from(request)
+ .startingAfter(startingAfter)
+ .build();
+ List result = parsedResponse.getData();
+ future.complete(new IntercomHttpResponse<>(
+ new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
+ try {
+ return list(nextRequest, requestOptions)
+ .get()
+ .body();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }),
+ response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can create a new contact (ie. user or lead).
+ */
+ public CompletableFuture> create(CreateContactRequest request) {
+ return create(request, null);
+ }
+
+ /**
+ * You can create a new contact (ie. user or lead).
+ */
+ public CompletableFuture> create(
+ CreateContactRequest request, RequestOptions requestOptions) {
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("contacts")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new IntercomException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .addHeader("Accept", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ CompletableFuture> future = new CompletableFuture<>();
+ client.newCall(okhttpRequest).enqueue(new Callback() {
+ @Override
+ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
+ try (ResponseBody responseBody = response.body()) {
+ if (response.isSuccessful()) {
+ future.complete(new IntercomHttpResponse<>(
+ ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Contact.class), response));
+ return;
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ if (response.code() == 401) {
+ future.completeExceptionally(new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response));
+ return;
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ future.completeExceptionally(new IntercomApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
+ response));
+ return;
+ } catch (IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ }
+
+ @Override
+ public void onFailure(@NotNull Call call, @NotNull IOException e) {
+ future.completeExceptionally(new IntercomException("Network error executing HTTP request", e));
+ }
+ });
+ return future;
+ }
+
+ /**
+ * You can archive a single contact.
+ */
+ public CompletableFuture> archive(ArchiveContactRequest request) {
+ return archive(request, null);
+ }
+
+ /**
+ * You can archive a single contact.
+ */
+ public CompletableFuture