Skip to content

Commit f555753

Browse files
authored
chore: put API requests into dedicated class (#76)
1 parent 4254279 commit f555753

File tree

2 files changed

+82
-123
lines changed

2 files changed

+82
-123
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.coveo.pushapiclient;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpRequest.BodyPublisher;
8+
import java.net.http.HttpResponse;
9+
10+
// TODO: LENS-934 - Support throttling
11+
class ApiCore {
12+
private final HttpClient httpClient;
13+
14+
public ApiCore() {
15+
this.httpClient = HttpClient.newHttpClient();
16+
}
17+
18+
public ApiCore(HttpClient httpClient) {
19+
this.httpClient = httpClient;
20+
}
21+
22+
public HttpResponse<String> post(URI uri, String[] headers)
23+
throws IOException, InterruptedException {
24+
return this.post(uri, headers, HttpRequest.BodyPublishers.ofString(""));
25+
}
26+
27+
public HttpResponse<String> post(URI uri, String[] headers, BodyPublisher body)
28+
throws IOException, InterruptedException {
29+
HttpRequest request = HttpRequest.newBuilder().headers(headers).uri(uri).POST(body).build();
30+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
31+
}
32+
33+
public HttpResponse<String> put(URI uri, String[] headers, BodyPublisher body)
34+
throws IOException, InterruptedException {
35+
HttpRequest request = HttpRequest.newBuilder().headers(headers).uri(uri).PUT(body).build();
36+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
37+
}
38+
39+
public HttpResponse<String> delete(URI uri, String[] headers)
40+
throws IOException, InterruptedException {
41+
HttpRequest request = HttpRequest.newBuilder().headers(headers).uri(uri).DELETE().build();
42+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
43+
}
44+
45+
public HttpResponse<String> delete(URI uri, String[] headers, BodyPublisher body)
46+
throws IOException, InterruptedException {
47+
HttpRequest request =
48+
HttpRequest.newBuilder().headers(headers).uri(uri).method("DELETE", body).build();
49+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
50+
}
51+
}

src/main/java/com/coveo/pushapiclient/PlatformClient.java

Lines changed: 31 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class PlatformClient {
1616
private final String apiKey;
1717
private final String organizationId;
18-
private final HttpClient httpClient;
18+
private final ApiCore api;
1919
private final PlatformUrl platformUrl;
2020

2121
/**
@@ -42,7 +42,7 @@ public PlatformClient(String apiKey, String organizationId) {
4242
public PlatformClient(String apiKey, String organizationId, PlatformUrl platformUrl) {
4343
this.apiKey = apiKey;
4444
this.organizationId = organizationId;
45-
this.httpClient = HttpClient.newHttpClient();
45+
this.api = new ApiCore();
4646
this.platformUrl = platformUrl;
4747
}
4848

@@ -58,7 +58,7 @@ public PlatformClient(String apiKey, String organizationId, PlatformUrl platform
5858
public PlatformClient(String apiKey, String organizationId, HttpClient httpClient) {
5959
this.apiKey = apiKey;
6060
this.organizationId = organizationId;
61-
this.httpClient = httpClient;
61+
this.api = new ApiCore(httpClient);
6262
this.platformUrl = new PlatformUrlBuilder().build();
6363
}
6464

@@ -75,7 +75,7 @@ public PlatformClient(String apiKey, String organizationId, HttpClient httpClien
7575
public PlatformClient(String apiKey, String organizationId, Environment environment) {
7676
this.apiKey = apiKey;
7777
this.organizationId = organizationId;
78-
this.httpClient = HttpClient.newHttpClient();
78+
this.api = new ApiCore();
7979
this.platformUrl = new PlatformUrlBuilder().withEnvironment(environment).build();
8080
}
8181

@@ -126,14 +126,9 @@ public HttpResponse<String> createSource(
126126
}
127127
});
128128

129-
HttpRequest request =
130-
HttpRequest.newBuilder()
131-
.headers(headers)
132-
.POST(HttpRequest.BodyPublishers.ofString(json))
133-
.uri(URI.create(this.getBaseSourceURL()))
134-
.build();
129+
URI uri = URI.create(this.getBaseSourceURL());
135130

136-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
131+
return this.api.post(uri, headers, HttpRequest.BodyPublishers.ofString(json));
137132
}
138133

139134
/**
@@ -156,14 +151,7 @@ public HttpResponse<String> createOrUpdateSecurityIdentity(
156151

157152
String json = new Gson().toJson(securityIdentityModel);
158153

159-
HttpRequest request =
160-
HttpRequest.newBuilder()
161-
.headers(headers)
162-
.PUT(HttpRequest.BodyPublishers.ofString(json))
163-
.uri(uri)
164-
.build();
165-
166-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
154+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(json));
167155
}
168156

169157
/**
@@ -186,14 +174,7 @@ public HttpResponse<String> createOrUpdateSecurityIdentityAlias(
186174

187175
String json = new Gson().toJson(securityIdentityAlias);
188176

189-
HttpRequest request =
190-
HttpRequest.newBuilder()
191-
.headers(headers)
192-
.PUT(HttpRequest.BodyPublishers.ofString(json))
193-
.uri(uri)
194-
.build();
195-
196-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
177+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(json));
197178
}
198179

199180
/**
@@ -215,14 +196,7 @@ public HttpResponse<String> deleteSecurityIdentity(
215196

216197
String json = new Gson().toJson(securityIdentityToDelete);
217198

218-
HttpRequest request =
219-
HttpRequest.newBuilder()
220-
.headers(headers)
221-
.method("DELETE", HttpRequest.BodyPublishers.ofString(json))
222-
.uri(uri)
223-
.build();
224-
225-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
199+
return this.api.delete(uri, headers, HttpRequest.BodyPublishers.ofString(json));
226200
}
227201

228202
/**
@@ -240,16 +214,15 @@ public HttpResponse<String> deleteOldSecurityIdentities(
240214
throws IOException, InterruptedException {
241215
String[] headers =
242216
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
217+
243218
URI uri =
244219
URI.create(
245220
this.getBaseProviderURL(securityProviderId)
246221
+ String.format(
247222
"/permissions/olderthan?queueDelay=%s%s",
248223
batchDelete.getQueueDelay(), appendOrderingId(batchDelete.getOrderingId())));
249224

250-
HttpRequest request = HttpRequest.newBuilder().headers(headers).DELETE().uri(uri).build();
251-
252-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
225+
return this.api.delete(uri, headers);
253226
}
254227

255228
/**
@@ -280,21 +253,15 @@ public HttpResponse<String> manageSecurityIdentities(
280253
throws IOException, InterruptedException {
281254
String[] headers =
282255
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
256+
283257
URI uri =
284258
URI.create(
285259
this.getBaseProviderURL(securityProviderId)
286260
+ String.format(
287261
"/permissions/batch?fileId=%s%s",
288262
batchConfig.getFileId(), appendOrderingId(batchConfig.getOrderingId())));
289263

290-
HttpRequest request =
291-
HttpRequest.newBuilder()
292-
.headers(headers)
293-
.PUT(HttpRequest.BodyPublishers.noBody())
294-
.uri(uri)
295-
.build();
296-
297-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
264+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.noBody());
298265
}
299266

300267
/**
@@ -314,21 +281,15 @@ public HttpResponse<String> pushDocument(
314281
throws IOException, InterruptedException {
315282
String[] headers =
316283
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
284+
317285
URI uri =
318286
URI.create(
319287
this.getBasePushURL()
320288
+ String.format(
321289
"/sources/%s/documents?documentId=%s&compressionType=%s",
322290
sourceId, documentId, compressionType.toString()));
323291

324-
HttpRequest request =
325-
HttpRequest.newBuilder()
326-
.headers(headers)
327-
.PUT(HttpRequest.BodyPublishers.ofString(documentJSON))
328-
.uri(uri)
329-
.build();
330-
331-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
292+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(documentJSON));
332293
}
333294

334295
/**
@@ -348,34 +309,25 @@ public HttpResponse<String> deleteDocument(
348309
throws IOException, InterruptedException {
349310
String[] headers =
350311
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
312+
351313
URI uri =
352314
URI.create(
353315
this.getBasePushURL()
354316
+ String.format(
355317
"/sources/%s/documents?documentId=%s&deleteChildren=%s",
356318
sourceId, documentId, deleteChildren));
357319

358-
HttpRequest request = HttpRequest.newBuilder().headers(headers).DELETE().uri(uri).build();
359-
360-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
320+
return this.api.delete(uri, headers);
361321
}
362322

363323
public HttpResponse<String> openStream(String sourceId) throws IOException, InterruptedException {
364324
String[] headers =
365325
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
366-
// TODO: LENS-875: standardize string manipulation
326+
367327
URI uri =
368328
URI.create(this.getBasePushURL() + String.format("/sources/%s/stream/open", sourceId));
369329

370-
// TODO: LENS-876: reduce code duplication
371-
HttpRequest request =
372-
HttpRequest.newBuilder()
373-
.headers(headers)
374-
.uri(uri)
375-
.POST(HttpRequest.BodyPublishers.ofString(""))
376-
.build();
377-
378-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
330+
return this.api.post(uri, headers);
379331
}
380332

381333
public HttpResponse<String> closeStream(String sourceId, String streamId)
@@ -387,33 +339,20 @@ public HttpResponse<String> closeStream(String sourceId, String streamId)
387339
this.getBasePushURL()
388340
+ String.format("/sources/%s/stream/%s/close", sourceId, streamId));
389341

390-
HttpRequest request =
391-
HttpRequest.newBuilder()
392-
.headers(headers)
393-
.uri(uri)
394-
.POST(HttpRequest.BodyPublishers.ofString(""))
395-
.build();
396-
397-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
342+
return this.api.post(uri, headers);
398343
}
399344

400345
public HttpResponse<String> requireStreamChunk(String sourceId, String streamId)
401346
throws IOException, InterruptedException {
402347
String[] headers =
403348
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
349+
404350
URI uri =
405351
URI.create(
406352
this.getBasePushURL()
407353
+ String.format("/sources/%s/stream/%s/chunk", sourceId, streamId));
408354

409-
HttpRequest request =
410-
HttpRequest.newBuilder()
411-
.headers(headers)
412-
.uri(uri)
413-
.POST(HttpRequest.BodyPublishers.ofString(""))
414-
.build();
415-
416-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
355+
return this.api.post(uri, headers);
417356
}
418357

419358
/**
@@ -427,16 +366,10 @@ public HttpResponse<String> requireStreamChunk(String sourceId, String streamId)
427366
public HttpResponse<String> createFileContainer() throws IOException, InterruptedException {
428367
String[] headers =
429368
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
430-
URI uri = URI.create(this.getBasePushURL() + "/files");
431369

432-
HttpRequest request =
433-
HttpRequest.newBuilder()
434-
.headers(headers)
435-
.uri(uri)
436-
.POST(HttpRequest.BodyPublishers.ofString(""))
437-
.build();
370+
URI uri = URI.create(this.getBasePushURL() + "/files");
438371

439-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
372+
return this.api.post(uri, headers);
440373
}
441374

442375
/**
@@ -452,19 +385,13 @@ public HttpResponse<String> updateSourceStatus(String sourceId, PushAPIStatus st
452385
throws IOException, InterruptedException {
453386
String[] headers =
454387
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
388+
455389
URI uri =
456390
URI.create(
457391
this.getBasePushURL()
458392
+ String.format("/sources/%s/status?statusType=%s", sourceId, status.toString()));
459393

460-
HttpRequest request =
461-
HttpRequest.newBuilder()
462-
.headers(headers)
463-
.uri(uri)
464-
.POST(HttpRequest.BodyPublishers.ofString(""))
465-
.build();
466-
467-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
394+
return this.api.post(uri, headers);
468395
}
469396

470397
/**
@@ -486,16 +413,10 @@ public HttpResponse<String> uploadContentToFileContainer(
486413
fileContainer.requiredHeaders.entrySet().stream()
487414
.flatMap(entry -> Stream.of(entry.getKey(), entry.getValue()))
488415
.toArray(String[]::new);
489-
URI uri = URI.create(fileContainer.uploadUri);
490416

491-
HttpRequest request =
492-
HttpRequest.newBuilder()
493-
.headers(headers)
494-
.uri(uri)
495-
.PUT(HttpRequest.BodyPublishers.ofString(batchUpdateJson))
496-
.build();
417+
URI uri = URI.create(fileContainer.uploadUri);
497418

498-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
419+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(batchUpdateJson));
499420
}
500421

501422
/**
@@ -520,14 +441,7 @@ public HttpResponse<String> pushFileContainerContent(String sourceId, FileContai
520441
+ String.format(
521442
"/sources/%s/documents/batch?fileId=%s", sourceId, fileContainer.fileId));
522443

523-
HttpRequest request =
524-
HttpRequest.newBuilder()
525-
.headers(headers)
526-
.uri(uri)
527-
.PUT(HttpRequest.BodyPublishers.ofString(""))
528-
.build();
529-
530-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
444+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(""));
531445
}
532446

533447
/**
@@ -546,16 +460,10 @@ public HttpResponse<String> pushBinaryToFileContainer(
546460
FileContainer fileContainer, byte[] fileAsBytes) throws IOException, InterruptedException {
547461
String[] headers =
548462
this.getHeaders(this.getAes256Header(), this.getContentTypeApplicationOctetStreamHeader());
549-
URI uri = URI.create(fileContainer.uploadUri);
550463

551-
HttpRequest request =
552-
HttpRequest.newBuilder()
553-
.headers(headers)
554-
.uri(uri)
555-
.PUT(HttpRequest.BodyPublishers.ofByteArray(fileAsBytes))
556-
.build();
464+
URI uri = URI.create(fileContainer.uploadUri);
557465

558-
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
466+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofByteArray(fileAsBytes));
559467
}
560468

561469
private String getBaseSourceURL() {

0 commit comments

Comments
 (0)