forked from intercom/intercom-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawAwayStatusReasonsClient.java
More file actions
82 lines (77 loc) · 3.45 KB
/
Copy pathRawAwayStatusReasonsClient.java
File metadata and controls
82 lines (77 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.intercom.api.resources.awaystatusreasons;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
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.ObjectMappers;
import com.intercom.api.core.RequestOptions;
import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.types.AwayStatusReason;
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.Response;
import okhttp3.ResponseBody;
public class RawAwayStatusReasonsClient {
protected final ClientOptions clientOptions;
public RawAwayStatusReasonsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* Returns a list of all away status reasons configured for the workspace, including deleted ones.
*/
public IntercomHttpResponse<List<AwayStatusReason>> listAwayStatusReasons() {
return listAwayStatusReasons(null);
}
/**
* Returns a list of all away status reasons configured for the workspace, including deleted ones.
*/
public IntercomHttpResponse<List<AwayStatusReason>> listAwayStatusReasons(RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("away_status_reasons")
.build();
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("GET", null)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.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();
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
if (response.isSuccessful()) {
return new IntercomHttpResponse<>(
ObjectMappers.JSON_MAPPER.readValue(
responseBodyString, new TypeReference<List<AwayStatusReason>>() {}),
response);
}
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
}
Object errorBody = ObjectMappers.parseErrorBody(responseBodyString);
throw new IntercomApiException(
"Error with status code " + response.code(), response.code(), errorBody, response);
} catch (IOException e) {
throw new IntercomException("Network error executing HTTP request", e);
}
}
}