forked from intercom/intercom-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawMessagesClient.java
More file actions
140 lines (134 loc) · 6.84 KB
/
Copy pathRawMessagesClient.java
File metadata and controls
140 lines (134 loc) · 6.84 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.intercom.api.resources.messages;
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.RequestOptions;
import com.intercom.api.errors.BadRequestError;
import com.intercom.api.errors.ForbiddenError;
import com.intercom.api.errors.UnauthorizedError;
import com.intercom.api.errors.UnprocessableEntityError;
import com.intercom.api.resources.messages.types.Message;
import com.intercom.api.types.CreateMessageRequest;
import com.intercom.api.types.Error;
import java.io.IOException;
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 RawMessagesClient {
protected final ClientOptions clientOptions;
public RawMessagesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* You can create a message that has been initiated by an admin. The conversation can be either an in-app message or an email.
* <blockquote>
* <p>🚧 Sending for visitors</p>
* <p>There can be a short delay between when a contact is created and when a contact becomes available to be messaged through the API. A 404 Not Found error will be returned in this case.</p>
* </blockquote>
* <p>This will return the Message model that has been created.</p>
* <blockquote>
* <p>🚧 Retrieving Associated Conversations</p>
* <p>As this is a message, there will be no conversation present until the contact responds. Once they do, you will have to search for a contact's conversations with the id of the message.</p>
* </blockquote>
*/
public IntercomHttpResponse<Message> create() {
return create(Optional.empty());
}
/**
* You can create a message that has been initiated by an admin. The conversation can be either an in-app message or an email.
* <blockquote>
* <p>🚧 Sending for visitors</p>
* <p>There can be a short delay between when a contact is created and when a contact becomes available to be messaged through the API. A 404 Not Found error will be returned in this case.</p>
* </blockquote>
* <p>This will return the Message model that has been created.</p>
* <blockquote>
* <p>🚧 Retrieving Associated Conversations</p>
* <p>As this is a message, there will be no conversation present until the contact responds. Once they do, you will have to search for a contact's conversations with the id of the message.</p>
* </blockquote>
*/
public IntercomHttpResponse<Message> create(Optional<CreateMessageRequest> request) {
return create(request, null);
}
/**
* You can create a message that has been initiated by an admin. The conversation can be either an in-app message or an email.
* <blockquote>
* <p>🚧 Sending for visitors</p>
* <p>There can be a short delay between when a contact is created and when a contact becomes available to be messaged through the API. A 404 Not Found error will be returned in this case.</p>
* </blockquote>
* <p>This will return the Message model that has been created.</p>
* <blockquote>
* <p>🚧 Retrieving Associated Conversations</p>
* <p>As this is a message, there will be no conversation present until the contact responds. Once they do, you will have to search for a contact's conversations with the id of the message.</p>
* </blockquote>
*/
public IntercomHttpResponse<Message> create(Optional<CreateMessageRequest> request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("messages")
.build();
RequestBody body;
try {
body = RequestBody.create("", null);
if (request.isPresent()) {
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();
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
if (response.isSuccessful()) {
return new IntercomHttpResponse<>(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Message.class), response);
}
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 403:
throw new ForbiddenError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class), response);
case 422:
throw new UnprocessableEntityError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.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);
}
}
}