diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 5261a22a..574a17ec 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -126,8 +126,17 @@ public HttpResponse pushDocument(String sourceId, String documentJSON, S return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } - public void deleteDocument(String sourceId, String documentId, Boolean deleteChildren) { - // TODO + public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { + String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/documents?documentId=%s&deleteChildren=%s", sourceId, documentId, deleteChildren)); + + HttpRequest request = HttpRequest.newBuilder() + .headers(headers) + .DELETE() + .uri(uri) + .build(); + + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } public HttpResponse createFileContainer() throws IOException, InterruptedException { diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index 4adbb5b3..c77b30f6 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -40,6 +40,10 @@ public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri); } + public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { + return this.platformClient.deleteDocument(sourceId, documentId, deleteChildren); + } + public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) throws IOException, InterruptedException { HttpResponse resFileContainer = this.platformClient.createFileContainer(); FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java index d0d60318..8d16ccc7 100644 --- a/src/main/java/com/coveo/testlocally/TestingLocally.java +++ b/src/main/java/com/coveo/testlocally/TestingLocally.java @@ -36,7 +36,7 @@ public static void testPushDocument(String sourceId, Source source) { put("my_field_3", 1234); put("my_field_4", new String[]{"a", "b", "c"}); }}); - DocumentBuilder docWithSecurity = new DocumentBuilder("https://perdu.com/2", "the title 2") + DocumentBuilder docWithSecurity = new DocumentBuilder("https://perdu.com/2", "the title 2") .withData("this is searchable also") .withAllowAnonymousUsers(false) .withAllowedPermissions(new UserSecurityIdentityBuilder("olamothe@coveo.com")) @@ -52,10 +52,18 @@ public static void testPushDocument(String sourceId, Source source) { } try { - source.addOrUpdateDocument(sourceId, simpleDoc); - source.addOrUpdateDocument(sourceId, docWithMetadata); - source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove)); - source.addOrUpdateDocument(sourceId, docWithSecurity); + HttpResponse resAddSimpleDoc = source.addOrUpdateDocument(sourceId, simpleDoc); + HttpResponse resAddOrUpdateMetadata = source.addOrUpdateDocument(sourceId, docWithMetadata); + HttpResponse resDelete = source.deleteDocument(sourceId, simpleDoc.getDocument().uri, true); + HttpResponse resBatch = source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove)); + + + System.out.println(resAddSimpleDoc.statusCode()); + System.out.println(resAddOrUpdateMetadata.statusCode()); + System.out.println(resDelete.statusCode()); + System.out.println(resBatch.statusCode()); + + } catch (IOException | InterruptedException e) { System.out.println(e); }