-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportClient.java
More file actions
334 lines (306 loc) · 13.3 KB
/
SupportClient.java
File metadata and controls
334 lines (306 loc) · 13.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.outseta.client.endpoint_client;
import com.outseta.client.BaseClient;
import com.outseta.client.ClientBuilder;
import com.outseta.exception.OutsetaClientBuildException;
import com.outseta.exception.OutsetaInvalidArgumentException;
import com.outseta.exception.OutsetaInvalidURLException;
import com.outseta.exception.OutsetaParseException;
import com.outseta.exception.api_exception.OutsetaAPIBadRequestException;
import com.outseta.exception.api_exception.OutsetaAPIFailedException;
import com.outseta.exception.api_exception.OutsetaAPIUnknownException;
import com.outseta.exception.api_exception.OutsetaInvalidResponseCodeException;
import com.outseta.model.request.PageRequest;
import com.outseta.model.result.Case;
import com.outseta.model.result.CaseReply;
import com.outseta.model.result.ItemPage;
import java.util.HashMap;
/**
* This class is used to make calls to the Support endpoints of the
* Outseta API.
* <p>
* The Profile endpoints are used to manage support cases.
* The class provides a builder to make it easier to construct
* the client.
* </p>
*/
public final class SupportClient extends BaseClient {
/**
* This method is used to get a builder that can be used to build a
* SupportClient object.
*
* @param baseUrl The base url for the api.
* @return The builder that can be used to build an SupportClient object.
* @throws OutsetaClientBuildException Thrown if the client cannot be
* built.
*/
public static ClientBuilder<SupportClient> builder(final String baseUrl)
throws OutsetaClientBuildException {
return new ClientBuilder<>(new SupportClient(baseUrl));
}
/**
* The constructor creates a new Support client with the base url.
* It is intentionally private to force the use of the builder.
*
* @param pBaseUrl The base url for the client to use.
* @throws OutsetaClientBuildException Thrown if the client cannot be
* built.
*/
private SupportClient(final String pBaseUrl)
throws OutsetaClientBuildException {
super(pBaseUrl);
}
/**
* Gets a case by id.
*
* @param caseId The id of the case.
* @return The case.
* @throws OutsetaInvalidArgumentException Thrown if the
* id is null.
* @throws OutsetaParseException Thrown if the email list
* cannot be parsed.
* @throws OutsetaInvalidResponseCodeException Thrown if the response code
* is invalid.
* @throws OutsetaAPIBadRequestException Thrown if the request is bad.
* @throws OutsetaAPIFailedException Thrown if the request fails.
* @throws OutsetaAPIUnknownException Thrown if the request fails for
* an unknown reason.
* @throws OutsetaInvalidURLException Thrown if the url is invalid.
*
* Example usage:
* <pre>{@code
* SupportClient client = SupportClient.builder(outsetaUrl)
* .apiKey(outsetaKey)
* .defaultParser()
* .defaultRequestMaker()
* .build();
* String caseId = "caseId";
* Case caseObject = client.getCase(caseId);
* }</pre>
*/
public Case getCase(
final String caseId)
throws OutsetaParseException, OutsetaInvalidResponseCodeException,
OutsetaAPIBadRequestException, OutsetaAPIFailedException,
OutsetaAPIUnknownException, OutsetaInvalidURLException,
OutsetaInvalidArgumentException {
if (caseId == null
|| caseId.trim().isEmpty()) {
throw new OutsetaInvalidArgumentException(
"Case id cannot be null or blank.");
}
String result = this.get("/support/cases/" + caseId,
new HashMap<>());
return this.getParserFacade().jsonStringToObject(result,
Case.class);
}
/**
* This method is used to get a page of Case objects.
*
* @param pageRequest The page request to use.
* @return The list of case objects.
* @throws OutsetaInvalidResponseCodeException Thrown if the response code
* is invalid.
* @throws OutsetaAPIBadRequestException Thrown if the request is bad.
* @throws OutsetaAPIFailedException Thrown if the request fails.
* @throws OutsetaAPIUnknownException Thrown if the request fails
* for an unknown reason.
* @throws OutsetaInvalidURLException Thrown if the url is invalid.
* @throws OutsetaParseException Thrown if the people cannot
* be parsed.
* @throws OutsetaInvalidArgumentException Thrown if the page request
* is null.
*
* Example usage:
* <pre>{@code
* CaseClient client = CaseClient.builder(outsetaUrl)
* .apiKey(outsetaKey)
* .defaultParser()
* .defaultRequestMaker()
* .build();
* PageRequest request = PageRequest.builder()
* .page(page)
* .pageSize(pageSize)
* .build();
*
* pageRequest.setCustomParams(Map.of("FromPerson.Uid", "asd"))
* int total = 0;
* ItemPage<Case> casePage = null;
*
* do {
* // Keep making requests as long as there are more pages
* casePage = client.getCasePage(request);
* total = casePage.getMetadata().getTotal();
*
* // The current page's items are aggregated
* allCases.addAll(casePage.getItems());
* request = request.nextPageRequest();
* }
* while (allCases.size() < total);
* }</pre>
*/
public ItemPage<Case> getCasePage(final PageRequest pageRequest)
throws OutsetaInvalidResponseCodeException,
OutsetaInvalidURLException, OutsetaAPIBadRequestException,
OutsetaAPIFailedException, OutsetaAPIUnknownException,
OutsetaParseException, OutsetaInvalidArgumentException {
if (pageRequest == null) {
throw new OutsetaInvalidArgumentException(
"Page request cannot be null.");
}
String result = this.get("/support/cases",
pageRequest.buildParams());
return this.getParserFacade()
.jsonStringToPage(result,
Case.class);
}
/**
* Adds a case into the support system.
*
* Set sendAutoResponder=false if you'd like the system not to send an
* automatic message that the ticket has been created.
*
* @param sendAutoResponder Whether to send an automated message.
* @param caseObject The case object to add.
* @return The case.
* @throws OutsetaInvalidArgumentException Thrown if the
* id is null.
* @throws OutsetaParseException Thrown if the email list
* cannot be parsed.
* @throws OutsetaInvalidResponseCodeException Thrown if the response code
* is invalid.
* @throws OutsetaAPIBadRequestException Thrown if the request is bad.
* @throws OutsetaAPIFailedException Thrown if the request fails.
* @throws OutsetaAPIUnknownException Thrown if the request fails for
* an unknown reason.
* @throws OutsetaInvalidURLException Thrown if the url is invalid.
*
* Example usage:
* <pre>{@code
* SupportClient client = SupportClient.builder(outsetaUrl)
* .apiKey(outsetaKey)
* .defaultParser()
* .defaultRequestMaker()
* .build();
* Case caseObject = Case.builder().build();
* Case createdCase = client.addCase(caseObject);
* }</pre>
*/
public Case addCase(
final boolean sendAutoResponder,
final Case caseObject)
throws OutsetaParseException, OutsetaInvalidResponseCodeException,
OutsetaAPIBadRequestException, OutsetaAPIFailedException,
OutsetaAPIUnknownException, OutsetaInvalidURLException,
OutsetaInvalidArgumentException {
if (caseObject == null) {
throw new OutsetaInvalidArgumentException(
"Case object cannot be null.");
}
String result = this.post("/support/cases?sendAutoResponder="
+ (sendAutoResponder ? "true" : "false"),
new HashMap<>(),
this.getParserFacade().objectToJsonString(caseObject));
return this.getParserFacade().jsonStringToObject(result,
Case.class);
}
/**
* Adds a response to the case from the person that opened the case.
*
* @param caseUid The id of the case.
* @param comment The comment to be added.
* @throws OutsetaInvalidArgumentException Thrown if the
* id is null.
* @throws OutsetaInvalidResponseCodeException Thrown if the response code
* is invalid.
* @throws OutsetaAPIBadRequestException Thrown if the request is bad.
* @throws OutsetaAPIFailedException Thrown if the request fails.
* @throws OutsetaAPIUnknownException Thrown if the request fails for
* an unknown reason.
* @throws OutsetaInvalidURLException Thrown if the url is invalid.
*
* Example usage:
* <pre>{@code
* SupportClient client = SupportClient.builder(outsetaUrl)
* .apiKey(outsetaKey)
* .defaultParser()
* .defaultRequestMaker()
* .build();
* String caseId = "caseId";
* String comment = "comment";
* Case result = client.addClientResponse(caseUid, comment);
* }</pre>
*/
public void addClientResponse(
final String caseUid,
final String comment)
throws OutsetaInvalidResponseCodeException,
OutsetaAPIBadRequestException, OutsetaAPIFailedException,
OutsetaAPIUnknownException, OutsetaInvalidURLException,
OutsetaInvalidArgumentException {
if (caseUid == null || caseUid.trim().isEmpty()) {
throw new OutsetaInvalidArgumentException(
"Case uid cannot be null or blank.");
}
if (comment == null || comment.trim().isEmpty()) {
throw new OutsetaInvalidArgumentException(
"Comment cannot be null or blank.");
}
String safeComment = comment.replaceAll(" ", "%20");
this.post("/support/cases/" + caseUid
+ "/clientresponse/" + safeComment,
new HashMap<>(),
"");
}
/**
* Adds a reply from an agent to a support case.
*
* @param caseUid The id of the case.
* @param caseReply The reply to the case from agent.
* @return The case.
* @throws OutsetaInvalidArgumentException Thrown if the
* id is null.
* @throws OutsetaParseException Thrown if the email list
* cannot be parsed.
* @throws OutsetaInvalidResponseCodeException Thrown if the response code
* is invalid.
* @throws OutsetaAPIBadRequestException Thrown if the request is bad.
* @throws OutsetaAPIFailedException Thrown if the request fails.
* @throws OutsetaAPIUnknownException Thrown if the request fails for
* an unknown reason.
* @throws OutsetaInvalidURLException Thrown if the url is invalid.
*
* Example usage:
* <pre>{@code
* SupportClient client = SupportClient.builder(outsetaUrl)
* .apiKey(outsetaKey)
* .defaultParser()
* .defaultRequestMaker()
* .build();
* String caseId = "caseId";
* CaseReply caseReply = CaseReply.builder().build();
* Case result = client.addReply(caseUid, caseReply);
* }</pre>
*/
public Case addReply(
final String caseUid,
final CaseReply caseReply)
throws OutsetaParseException, OutsetaInvalidResponseCodeException,
OutsetaAPIBadRequestException, OutsetaAPIFailedException,
OutsetaAPIUnknownException, OutsetaInvalidURLException,
OutsetaInvalidArgumentException {
if (caseUid == null || caseUid.trim().isEmpty()) {
throw new OutsetaInvalidArgumentException(
"Case uid cannot be null or blank.");
}
if (caseReply == null) {
throw new OutsetaInvalidArgumentException(
"Case reply cannot be null.");
}
String result = this.post("/support/cases/" + caseUid
+ "/replies",
new HashMap<>(),
this.getParserFacade().objectToJsonString(caseReply));
return this.getParserFacade().jsonStringToObject(result,
Case.class);
}
}