From d17202cfc0cc352aea167177da8befcdadd8c49e Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:15:09 -0400 Subject: [PATCH 01/10] add region enum --- .../java/com/coveo/pushapiclient/Region.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/com/coveo/pushapiclient/Region.java diff --git a/src/main/java/com/coveo/pushapiclient/Region.java b/src/main/java/com/coveo/pushapiclient/Region.java new file mode 100644 index 00000000..6e6a6df9 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/Region.java @@ -0,0 +1,20 @@ +package com.coveo.pushapiclient; + +/** + * Available Platform regions to connect to + */ +public enum Region { + US("us"), + EU("eu"), + AU("au"); + + private String value; + + Region(String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } +} \ No newline at end of file From f7122e1f63516a3091040af3fd122ca0b6cfdc5d Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:15:33 -0400 Subject: [PATCH 02/10] refactor Environment enum --- .../com/coveo/pushapiclient/Environment.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/coveo/pushapiclient/Environment.java b/src/main/java/com/coveo/pushapiclient/Environment.java index ccf1c4f8..b42e0176 100644 --- a/src/main/java/com/coveo/pushapiclient/Environment.java +++ b/src/main/java/com/coveo/pushapiclient/Environment.java @@ -4,18 +4,18 @@ * Available environments to use as the host for the PushAPI. */ public enum Environment { - PRODUCTION( "https://api.cloud.coveo.com"), - HIPAA("https://apihipaa.cloud.coveo.com"), - DEVELOPMENT("https://apidev.cloud.coveo.com"), - STAGING("https://apiqa.cloud.coveo.com"); + PRODUCTION( "prod"), + HIPAA("hipaa"), + DEVELOPMENT("dev"), + STAGING("stg"); - private String host; + private String value; - Environment(String host) { - this.host = host; + Environment(String value) { + this.value = value; } - public String getHost() { - return this.host; + public String getValue() { + return this.value; } } From 0833640b61be85b9896e7e0bc04cb45814917ec5 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:15:49 -0400 Subject: [PATCH 03/10] Create PlatformUrlBuilder --- .../com/coveo/pushapiclient/PlatformUrl.java | 20 ++++++++++++++++ .../pushapiclient/PlatformUrlBuilder.java | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/com/coveo/pushapiclient/PlatformUrl.java create mode 100644 src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java diff --git a/src/main/java/com/coveo/pushapiclient/PlatformUrl.java b/src/main/java/com/coveo/pushapiclient/PlatformUrl.java new file mode 100644 index 00000000..13adbf2d --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/PlatformUrl.java @@ -0,0 +1,20 @@ +package com.coveo.pushapiclient; + +public class PlatformUrl { + + private final Environment environment; + private final Region region; + + public PlatformUrl(Environment environment, Region region) { + this.environment = environment; + this.region = region; + } + + public String getPlatformUrl() { + return String.format("https://platform%s%s.cloud.coveo.com", this.environment, this.region); + } + + public String getApiUrl() { + return String.format("https://api%s%s.cloud.coveo.com", this.environment, this.region); + } +} diff --git a/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java b/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java new file mode 100644 index 00000000..1ba59b53 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java @@ -0,0 +1,24 @@ +package com.coveo.pushapiclient; + +public class PlatformUrlBuilder { + + public static final Environment DEFAULT_ENVIRONMENT = Environment.PRODUCTION; + public static final Region DEFAULT_REGION = Region.US; + + private Environment environment = PlatformUrlBuilder.DEFAULT_ENVIRONMENT; + private Region region = PlatformUrlBuilder.DEFAULT_REGION; + + public PlatformUrlBuilder withEnvironment(Environment environment) { + this.environment = environment; + return this; + } + + public PlatformUrlBuilder withRegion(Region region) { + this.region = region; + return this; + } + + public PlatformUrl build() { + return new PlatformUrl(this.environment, this.region); + } +} From 0ff2bab2088cc54fe7eb2ec2a5adacd95f8d6d06 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:16:22 -0400 Subject: [PATCH 04/10] Update source code with new platformUrl class --- samples/CreateSource.java | 1 + .../coveo/pushapiclient/PlatformClient.java | 30 ++++++++++++++----- .../java/com/coveo/pushapiclient/Source.java | 13 ++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/samples/CreateSource.java b/samples/CreateSource.java index 2c618927..34751748 100644 --- a/samples/CreateSource.java +++ b/samples/CreateSource.java @@ -6,6 +6,7 @@ public class CreateSource { public static void main(String[] args) { + // TODO: add example of how to provide different environments and regions Source source = new Source("my_api_key", "my_org_id"); try { HttpResponse response = source.create("the_name_of_my_source", SourceVisibility.SECURED); diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 2d9df7ba..6df8fbce 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -19,7 +19,7 @@ public class PlatformClient { private final String apiKey; private final String organizationId; private final HttpClient httpClient; - private final Environment environment; + private final PlatformUrl platformUrl; /** * Construct a PlatformClient @@ -28,10 +28,21 @@ public class PlatformClient { * @param organizationId The Coveo Organization identifier. */ public PlatformClient(String apiKey, String organizationId) { + this(apiKey, organizationId, new PlatformUrlBuilder().build()); + } + + /** + * Construct a PlatformClient + * + * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param organizationId The Coveo Organization identifier. + * @param platformUrl TODO: + */ + public PlatformClient(String apiKey, String organizationId, PlatformUrl platformUrl) { this.apiKey = apiKey; this.organizationId = organizationId; this.httpClient = HttpClient.newHttpClient(); - this.environment = Environment.PRODUCTION; + this.platformUrl = platformUrl; } /** @@ -45,21 +56,26 @@ public PlatformClient(String apiKey, String organizationId, HttpClient httpClien this.apiKey = apiKey; this.organizationId = organizationId; this.httpClient = httpClient; - this.environment = Environment.PRODUCTION; + this.platformUrl = new PlatformUrlBuilder().build(); } + /** - * Construct a PlatformClient + * @deprecated Please now use PlatformUrl to define your Platform environment + * @see PlatformUrl Construct a PlatformUrl * * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). * @param organizationId The Coveo Organization identifier. * @param environment The Environment to be used. */ + @Deprecated public PlatformClient(String apiKey, String organizationId, Environment environment) { this.apiKey = apiKey; this.organizationId = organizationId; this.httpClient = HttpClient.newHttpClient(); - this.environment = environment; + this.platformUrl = new PlatformUrlBuilder() + .withEnvironment(environment) + .build(); } /** @@ -382,11 +398,11 @@ private String getBaseSourceURL() { } private String getBasePlatformURL() { - return String.format("https://platform.cloud.coveo.com/rest/organizations/%s", this.organizationId); + return String.format("%s/rest/organizations/%s", this.platformUrl.getPlatformUrl(),this.organizationId); } private String getBasePushURL() { - return String.format("%s/push/v1/organizations/%s", this.environment.getHost(), this.organizationId); + return String.format("%s/push/v1/organizations/%s", this.platformUrl.getApiUrl(), this.organizationId); } private String getBaseProviderURL(String providerId) { diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index f374ddce..0192c3b1 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -17,10 +17,23 @@ public Source(String apiKey, String organizationId) { } /** + * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param organizationId The Coveo Organization identifier. + * @param platformUrl The platform Url + */ + public Source(String apiKey, String organizationId, PlatformUrl platformUrl) { + this.platformClient = new PlatformClient(apiKey, organizationId, platformUrl); + } + + /** + * @deprecated Please now use PlatformUrl to define your Platform environment + * @see PlatformUrl Construct a PlatformUrl + * * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). * @param organizationId The Coveo Organization identifier. * @param environment The Environment to be used. */ + @Deprecated public Source(String apiKey, String organizationId, Environment environment) { this.platformClient = new PlatformClient(apiKey, organizationId, environment); } From 4e32ef154b976ddd32e518f08a3e61d4b24f4b05 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:36:06 -0400 Subject: [PATCH 05/10] update doc and sample --- samples/CreateSource.java | 9 +- .../com/coveo/pushapiclient/PlatformUrl.java | 6 + .../java/com/coveo/pushapiclient/Source.java | 106 ++++++++++++------ 3 files changed, 86 insertions(+), 35 deletions(-) diff --git a/samples/CreateSource.java b/samples/CreateSource.java index 34751748..41b19c5e 100644 --- a/samples/CreateSource.java +++ b/samples/CreateSource.java @@ -1,3 +1,6 @@ +import com.coveo.pushapiclient.Region; +import com.coveo.pushapiclient.PlatformUrl; +import com.coveo.pushapiclient.PlatformUrlBuilder; import com.coveo.pushapiclient.Source; import com.coveo.pushapiclient.SourceVisibility; @@ -6,8 +9,10 @@ public class CreateSource { public static void main(String[] args) { - // TODO: add example of how to provide different environments and regions - Source source = new Source("my_api_key", "my_org_id"); + PlatformUrl platformUrl = new PlatformUrlBuilder() + .withRegion(Region.AU) // If your organization is located in a different region than Region.US + .build(); + Source source = new Source("my_api_key", "my_org_id", platformUrl); try { HttpResponse response = source.create("the_name_of_my_source", SourceVisibility.SECURED); System.out.println(String.format("Source creation status: %s", response.statusCode())); diff --git a/src/main/java/com/coveo/pushapiclient/PlatformUrl.java b/src/main/java/com/coveo/pushapiclient/PlatformUrl.java index 13adbf2d..2b1479aa 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformUrl.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformUrl.java @@ -5,6 +5,12 @@ public class PlatformUrl { private final Environment environment; private final Region region; + /** + * @param environment The environment platform of your organization + * @param region The physical center of your organization + * + * @see https://docs.coveo.com/en/2976 + */ public PlatformUrl(Environment environment, Region region) { this.environment = environment; this.region = region; diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index 0192c3b1..9fa98e92 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -9,7 +9,9 @@ public class Source { PlatformClient platformClient; /** - * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param apiKey An apiKey capable of pushing documents and managing + * sources in a Coveo organization. See [Manage API + * Keys](https://docs.coveo.com/en/1718). * @param organizationId The Coveo Organization identifier. */ public Source(String apiKey, String organizationId) { @@ -17,9 +19,11 @@ public Source(String apiKey, String organizationId) { } /** - * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param apiKey An apiKey capable of pushing documents and managing + * sources in a Coveo organization. See [Manage API + * Keys](https://docs.coveo.com/en/1718). * @param organizationId The Coveo Organization identifier. - * @param platformUrl The platform Url + * @param platformUrl */ public Source(String apiKey, String organizationId, PlatformUrl platformUrl) { this.platformClient = new PlatformClient(apiKey, organizationId, platformUrl); @@ -42,17 +46,22 @@ public Source(String apiKey, String organizationId, Environment environment) { * Create a new push source. * * @param name The name of the source to create - * @param sourceVisibility The security option that should be applied to the content of the source. See [Content Security](https://docs.coveo.com/en/1779). + * @param sourceVisibility The security option that should be applied to the + * content of the source. See [Content + * Security](https://docs.coveo.com/en/1779). * @return * @throws IOException * @throws InterruptedException */ - public HttpResponse create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + public HttpResponse create(String name, SourceVisibility sourceVisibility) + throws IOException, InterruptedException { return this.platformClient.createSource(name, sourceVisibility); } /** - * Create or update a security identity. See [Adding a Single Security Identity](https://docs.coveo.com/en/167) and [Security Identity Models](https://docs.coveo.com/en/139). + * Create or update a security identity. See [Adding a Single Security + * Identity](https://docs.coveo.com/en/167) and [Security Identity + * Models](https://docs.coveo.com/en/139). * * @param securityProviderId * @param securityIdentityModel @@ -60,12 +69,15 @@ public HttpResponse create(String name, SourceVisibility sourceVisibilit * @throws IOException * @throws InterruptedException */ - public HttpResponse createOrUpdateSecurityIdentity(String securityProviderId, SecurityIdentityModel securityIdentityModel) throws IOException, InterruptedException { + public HttpResponse createOrUpdateSecurityIdentity(String securityProviderId, + SecurityIdentityModel securityIdentityModel) throws IOException, InterruptedException { return this.platformClient.createOrUpdateSecurityIdentity(securityProviderId, securityIdentityModel); } /** - * Create or update a security identity alias. See [Adding a Single Alias](https://docs.coveo.com/en/142) and [User Alias Definition Examples](https://docs.coveo.com/en/46). + * Create or update a security identity alias. See [Adding a Single + * Alias](https://docs.coveo.com/en/142) and [User Alias Definition + * Examples](https://docs.coveo.com/en/46). * * @param securityProviderId * @param securityIdentityAliasModel @@ -73,12 +85,14 @@ public HttpResponse createOrUpdateSecurityIdentity(String securityProvid * @throws IOException * @throws InterruptedException */ - public HttpResponse createOrUpdateSecurityIdentityAlias(String securityProviderId, SecurityIdentityAliasModel securityIdentityAliasModel) throws IOException, InterruptedException { + public HttpResponse createOrUpdateSecurityIdentityAlias(String securityProviderId, + SecurityIdentityAliasModel securityIdentityAliasModel) throws IOException, InterruptedException { return this.platformClient.createOrUpdateSecurityIdentityAlias(securityProviderId, securityIdentityAliasModel); } /** - * Delete a security identity. See [Disabling a Single Security Identity](https://docs.coveo.com/en/84). + * Delete a security identity. See [Disabling a Single Security + * Identity](https://docs.coveo.com/en/84). * * @param securityProviderId * @param securityIdentityDelete @@ -86,12 +100,14 @@ public HttpResponse createOrUpdateSecurityIdentityAlias(String securityP * @throws IOException * @throws InterruptedException */ - public HttpResponse deleteSecurityIdentity(String securityProviderId, SecurityIdentityDelete securityIdentityDelete) throws IOException, InterruptedException { + public HttpResponse deleteSecurityIdentity(String securityProviderId, + SecurityIdentityDelete securityIdentityDelete) throws IOException, InterruptedException { return this.platformClient.deleteSecurityIdentity(securityProviderId, securityIdentityDelete); } /** - * Update the status of a Push source. See [Updating the Status of a Push Source](https://docs.coveo.com/en/35). + * Update the status of a Push source. See [Updating the Status of a Push + * Source](https://docs.coveo.com/en/35). * * @param sourceId * @param status @@ -99,12 +115,14 @@ public HttpResponse deleteSecurityIdentity(String securityProviderId, Se * @throws IOException * @throws InterruptedException */ - public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus status) throws IOException, InterruptedException { + public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus status) + throws IOException, InterruptedException { return this.platformClient.updateSourceStatus(sourceId, status); } /** - * Delete old security identities. See [Disabling Old Security Identities](https://docs.coveo.com/en/33). + * Delete old security identities. See [Disabling Old Security + * Identities](https://docs.coveo.com/en/33). * * @param securityProviderId * @param batchDelete @@ -112,12 +130,14 @@ public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus st * @throws IOException * @throws InterruptedException */ - public HttpResponse deleteOldSecurityIdentities(String securityProviderId, SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException { + public HttpResponse deleteOldSecurityIdentities(String securityProviderId, + SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException { return this.platformClient.deleteOldSecurityIdentities(securityProviderId, batchDelete); } /** - * Manage batches of security identities. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55). + * Manage batches of security identities. See [Manage Batches of Security + * Identities](https://docs.coveo.com/en/55). * * @param securityProviderId * @param batchConfig @@ -125,12 +145,14 @@ public HttpResponse deleteOldSecurityIdentities(String securityProviderI * @throws IOException * @throws InterruptedException */ - public HttpResponse manageSecurityIdentities(String securityProviderId, SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException { + public HttpResponse manageSecurityIdentities(String securityProviderId, + SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException { return this.platformClient.manageSecurityIdentities(securityProviderId, batchConfig); } /** - * Adds or updates an individual item in a push source. See [Adding a Single Item in a Push Source](https://docs.coveo.com/en/133). + * Adds or updates an individual item in a push source. See [Adding a Single + * Item in a Push Source](https://docs.coveo.com/en/133). * * @param sourceId * @param docBuilder @@ -138,13 +160,19 @@ public HttpResponse manageSecurityIdentities(String securityProviderId, * @throws IOException * @throws InterruptedException */ - public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) throws IOException, InterruptedException { - CompressionType compressionType = docBuilder.getDocument().compressedBinaryData != null ? docBuilder.getDocument().compressedBinaryData.getCompressionType() : CompressionType.UNCOMPRESSED; - return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri, compressionType); + public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) + throws IOException, InterruptedException { + CompressionType compressionType = docBuilder.getDocument().compressedBinaryData != null + ? docBuilder.getDocument().compressedBinaryData.getCompressionType() + : CompressionType.UNCOMPRESSED; + return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri, + compressionType); } /** - * Deletes a specific item from a Push source. Optionally, the child items of that item can also be deleted. See [Deleting an Item in a Push Source](https://docs.coveo.com/en/171). + * Deletes a specific item from a Push source. Optionally, the child items of + * that item can also be deleted. See [Deleting an Item in a Push + * Source](https://docs.coveo.com/en/171). * * @param sourceId * @param documentId @@ -153,12 +181,14 @@ public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder * @throws IOException * @throws InterruptedException */ - public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { + public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) + throws IOException, InterruptedException { return this.platformClient.deleteDocument(sourceId, documentId, deleteChildren); } /** - * Manage batches of items in a push source. See [Manage Batches of Items in a Push Source](https://docs.coveo.com/en/90) + * Manage batches of items in a push source. See [Manage Batches of Items in a + * Push Source](https://docs.coveo.com/en/90) * * @param sourceId * @param batchUpdate @@ -166,7 +196,8 @@ public HttpResponse deleteDocument(String sourceId, String documentId, B * @throws IOException * @throws InterruptedException */ - public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) throws IOException, InterruptedException { + public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) + throws IOException, InterruptedException { HttpResponse resFileContainer = this.platformClient.createFileContainer(); FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); this.platformClient.uploadContentToFileContainer(fileContainer, new Gson().toJson(batchUpdate.marshal())); @@ -174,7 +205,9 @@ public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate ba } /** - * Manages pushing batches of Security Identities to a File Container, then into Coveo. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55) + * Manages pushing batches of Security Identities to a File Container, then into + * Coveo. See [Manage Batches of Security + * Identities](https://docs.coveo.com/en/55) * * @param securityProviderId * @param batchIdentity @@ -182,21 +215,26 @@ public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate ba * @throws IOException * @throws InterruptedException */ - public SecurityIdentityBatchResponse batchUpdateSecurityIdentities(String securityProviderId, BatchIdentity batchIdentity) throws IOException, InterruptedException { + public SecurityIdentityBatchResponse batchUpdateSecurityIdentities(String securityProviderId, + BatchIdentity batchIdentity) throws IOException, InterruptedException { SecurityIdentityBatchResponse securityIdentityBatchResponse = new SecurityIdentityBatchResponse(); HttpResponse resFileContainer = this.platformClient.createFileContainer(); FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); String batchIdJson = new Gson().toJson(batchIdentity.marshal()); - securityIdentityBatchResponse.s3Response = this.platformClient.uploadContentToFileContainer(fileContainer, batchIdJson); - if (securityIdentityBatchResponse.s3Response.statusCode() >= 200 && securityIdentityBatchResponse.s3Response.statusCode() <= 299) { //maybe just 200 or 202 + securityIdentityBatchResponse.s3Response = this.platformClient.uploadContentToFileContainer(fileContainer, + batchIdJson); + if (securityIdentityBatchResponse.s3Response.statusCode() >= 200 + && securityIdentityBatchResponse.s3Response.statusCode() <= 299) { // maybe just 200 or 202 SecurityIdentityBatchConfig batchConfig = new SecurityIdentityBatchConfig(fileContainer.fileId, 0l); - securityIdentityBatchResponse.batchResponse = this.manageSecurityIdentities(securityProviderId, batchConfig); + securityIdentityBatchResponse.batchResponse = this.manageSecurityIdentities(securityProviderId, + batchConfig); } return securityIdentityBatchResponse; } /** - * Creates a File Container. [Creating a File Container](https://docs.coveo.com/en/43) + * Creates a File Container. [Creating a File + * Container](https://docs.coveo.com/en/43) * * @return * @throws IOException @@ -208,7 +246,8 @@ public FileContainer createFileContainer() throws IOException, InterruptedExcept } /** - * Push file to a File Container. [Using the compressedBinaryDataFileId Property](https://docs.coveo.com/en/69) + * Push file to a File Container. [Using the compressedBinaryDataFileId + * Property](https://docs.coveo.com/en/69) * * @param fileContainer * @param fileAsBytes @@ -216,7 +255,8 @@ public FileContainer createFileContainer() throws IOException, InterruptedExcept * @throws IOException * @throws InterruptedException */ - public HttpResponse pushBinaryToFileContainer(FileContainer fileContainer, byte[] fileAsBytes) throws IOException, InterruptedException { + public HttpResponse pushBinaryToFileContainer(FileContainer fileContainer, byte[] fileAsBytes) + throws IOException, InterruptedException { return this.platformClient.pushBinaryToFileContainer(fileContainer, fileAsBytes); } } From bbfe7839d8cbb52a3902c89fa64e8d955fba0928 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:54:42 -0400 Subject: [PATCH 06/10] unlint --- .../java/com/coveo/pushapiclient/Source.java | 96 ++++++------------- 1 file changed, 30 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index 9fa98e92..3b09a562 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -46,22 +46,17 @@ public Source(String apiKey, String organizationId, Environment environment) { * Create a new push source. * * @param name The name of the source to create - * @param sourceVisibility The security option that should be applied to the - * content of the source. See [Content - * Security](https://docs.coveo.com/en/1779). + * @param sourceVisibility The security option that should be applied to the content of the source. See [Content Security](https://docs.coveo.com/en/1779). * @return * @throws IOException * @throws InterruptedException */ - public HttpResponse create(String name, SourceVisibility sourceVisibility) - throws IOException, InterruptedException { + public HttpResponse create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { return this.platformClient.createSource(name, sourceVisibility); } /** - * Create or update a security identity. See [Adding a Single Security - * Identity](https://docs.coveo.com/en/167) and [Security Identity - * Models](https://docs.coveo.com/en/139). + * Create or update a security identity. See [Adding a Single Security Identity](https://docs.coveo.com/en/167) and [Security Identity Models](https://docs.coveo.com/en/139). * * @param securityProviderId * @param securityIdentityModel @@ -69,15 +64,12 @@ public HttpResponse create(String name, SourceVisibility sourceVisibilit * @throws IOException * @throws InterruptedException */ - public HttpResponse createOrUpdateSecurityIdentity(String securityProviderId, - SecurityIdentityModel securityIdentityModel) throws IOException, InterruptedException { + public HttpResponse createOrUpdateSecurityIdentity(String securityProviderId, SecurityIdentityModel securityIdentityModel) throws IOException, InterruptedException { return this.platformClient.createOrUpdateSecurityIdentity(securityProviderId, securityIdentityModel); } /** - * Create or update a security identity alias. See [Adding a Single - * Alias](https://docs.coveo.com/en/142) and [User Alias Definition - * Examples](https://docs.coveo.com/en/46). + * Create or update a security identity alias. See [Adding a Single Alias](https://docs.coveo.com/en/142) and [User Alias Definition Examples](https://docs.coveo.com/en/46). * * @param securityProviderId * @param securityIdentityAliasModel @@ -85,14 +77,12 @@ public HttpResponse createOrUpdateSecurityIdentity(String securityProvid * @throws IOException * @throws InterruptedException */ - public HttpResponse createOrUpdateSecurityIdentityAlias(String securityProviderId, - SecurityIdentityAliasModel securityIdentityAliasModel) throws IOException, InterruptedException { + public HttpResponse createOrUpdateSecurityIdentityAlias(String securityProviderId, SecurityIdentityAliasModel securityIdentityAliasModel) throws IOException, InterruptedException { return this.platformClient.createOrUpdateSecurityIdentityAlias(securityProviderId, securityIdentityAliasModel); } /** - * Delete a security identity. See [Disabling a Single Security - * Identity](https://docs.coveo.com/en/84). + * Delete a security identity. See [Disabling a Single Security Identity](https://docs.coveo.com/en/84). * * @param securityProviderId * @param securityIdentityDelete @@ -100,14 +90,12 @@ public HttpResponse createOrUpdateSecurityIdentityAlias(String securityP * @throws IOException * @throws InterruptedException */ - public HttpResponse deleteSecurityIdentity(String securityProviderId, - SecurityIdentityDelete securityIdentityDelete) throws IOException, InterruptedException { + public HttpResponse deleteSecurityIdentity(String securityProviderId, SecurityIdentityDelete securityIdentityDelete) throws IOException, InterruptedException { return this.platformClient.deleteSecurityIdentity(securityProviderId, securityIdentityDelete); } /** - * Update the status of a Push source. See [Updating the Status of a Push - * Source](https://docs.coveo.com/en/35). + * Update the status of a Push source. See [Updating the Status of a Push Source](https://docs.coveo.com/en/35). * * @param sourceId * @param status @@ -115,14 +103,12 @@ public HttpResponse deleteSecurityIdentity(String securityProviderId, * @throws IOException * @throws InterruptedException */ - public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus status) - throws IOException, InterruptedException { + public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus status) throws IOException, InterruptedException { return this.platformClient.updateSourceStatus(sourceId, status); } /** - * Delete old security identities. See [Disabling Old Security - * Identities](https://docs.coveo.com/en/33). + * Delete old security identities. See [Disabling Old Security Identities](https://docs.coveo.com/en/33). * * @param securityProviderId * @param batchDelete @@ -130,14 +116,12 @@ public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus st * @throws IOException * @throws InterruptedException */ - public HttpResponse deleteOldSecurityIdentities(String securityProviderId, - SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException { + public HttpResponse deleteOldSecurityIdentities(String securityProviderId, SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException { return this.platformClient.deleteOldSecurityIdentities(securityProviderId, batchDelete); } /** - * Manage batches of security identities. See [Manage Batches of Security - * Identities](https://docs.coveo.com/en/55). + * Manage batches of security identities. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55). * * @param securityProviderId * @param batchConfig @@ -145,14 +129,12 @@ public HttpResponse deleteOldSecurityIdentities(String securityProviderI * @throws IOException * @throws InterruptedException */ - public HttpResponse manageSecurityIdentities(String securityProviderId, - SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException { + public HttpResponse manageSecurityIdentities(String securityProviderId, SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException { return this.platformClient.manageSecurityIdentities(securityProviderId, batchConfig); } /** - * Adds or updates an individual item in a push source. See [Adding a Single - * Item in a Push Source](https://docs.coveo.com/en/133). + * Adds or updates an individual item in a push source. See [Adding a Single Item in a Push Source](https://docs.coveo.com/en/133). * * @param sourceId * @param docBuilder @@ -160,19 +142,13 @@ public HttpResponse manageSecurityIdentities(String securityProviderId, * @throws IOException * @throws InterruptedException */ - public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) - throws IOException, InterruptedException { - CompressionType compressionType = docBuilder.getDocument().compressedBinaryData != null - ? docBuilder.getDocument().compressedBinaryData.getCompressionType() - : CompressionType.UNCOMPRESSED; - return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri, - compressionType); + public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) throws IOException, InterruptedException { + CompressionType compressionType = docBuilder.getDocument().compressedBinaryData != null ? docBuilder.getDocument().compressedBinaryData.getCompressionType() : CompressionType.UNCOMPRESSED; + return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri, compressionType); } /** - * Deletes a specific item from a Push source. Optionally, the child items of - * that item can also be deleted. See [Deleting an Item in a Push - * Source](https://docs.coveo.com/en/171). + * Deletes a specific item from a Push source. Optionally, the child items of that item can also be deleted. See [Deleting an Item in a Push Source](https://docs.coveo.com/en/171). * * @param sourceId * @param documentId @@ -181,14 +157,12 @@ public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder * @throws IOException * @throws InterruptedException */ - public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) - throws IOException, InterruptedException { + public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { return this.platformClient.deleteDocument(sourceId, documentId, deleteChildren); } /** - * Manage batches of items in a push source. See [Manage Batches of Items in a - * Push Source](https://docs.coveo.com/en/90) + * Manage batches of items in a push source. See [Manage Batches of Items in a Push Source](https://docs.coveo.com/en/90) * * @param sourceId * @param batchUpdate @@ -196,8 +170,7 @@ public HttpResponse deleteDocument(String sourceId, String documentId, B * @throws IOException * @throws InterruptedException */ - public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) - throws IOException, InterruptedException { + public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) throws IOException, InterruptedException { HttpResponse resFileContainer = this.platformClient.createFileContainer(); FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); this.platformClient.uploadContentToFileContainer(fileContainer, new Gson().toJson(batchUpdate.marshal())); @@ -205,9 +178,7 @@ public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate ba } /** - * Manages pushing batches of Security Identities to a File Container, then into - * Coveo. See [Manage Batches of Security - * Identities](https://docs.coveo.com/en/55) + * Manages pushing batches of Security Identities to a File Container, then into Coveo. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55) * * @param securityProviderId * @param batchIdentity @@ -215,26 +186,21 @@ public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate ba * @throws IOException * @throws InterruptedException */ - public SecurityIdentityBatchResponse batchUpdateSecurityIdentities(String securityProviderId, - BatchIdentity batchIdentity) throws IOException, InterruptedException { + public SecurityIdentityBatchResponse batchUpdateSecurityIdentities(String securityProviderId, BatchIdentity batchIdentity) throws IOException, InterruptedException { SecurityIdentityBatchResponse securityIdentityBatchResponse = new SecurityIdentityBatchResponse(); HttpResponse resFileContainer = this.platformClient.createFileContainer(); FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); String batchIdJson = new Gson().toJson(batchIdentity.marshal()); - securityIdentityBatchResponse.s3Response = this.platformClient.uploadContentToFileContainer(fileContainer, - batchIdJson); - if (securityIdentityBatchResponse.s3Response.statusCode() >= 200 - && securityIdentityBatchResponse.s3Response.statusCode() <= 299) { // maybe just 200 or 202 + securityIdentityBatchResponse.s3Response = this.platformClient.uploadContentToFileContainer(fileContainer, batchIdJson); + if (securityIdentityBatchResponse.s3Response.statusCode() >= 200 && securityIdentityBatchResponse.s3Response.statusCode() <= 299) { //maybe just 200 or 202 SecurityIdentityBatchConfig batchConfig = new SecurityIdentityBatchConfig(fileContainer.fileId, 0l); - securityIdentityBatchResponse.batchResponse = this.manageSecurityIdentities(securityProviderId, - batchConfig); + securityIdentityBatchResponse.batchResponse = this.manageSecurityIdentities(securityProviderId, batchConfig); } return securityIdentityBatchResponse; } /** - * Creates a File Container. [Creating a File - * Container](https://docs.coveo.com/en/43) + * Creates a File Container. [Creating a File Container](https://docs.coveo.com/en/43) * * @return * @throws IOException @@ -246,8 +212,7 @@ public FileContainer createFileContainer() throws IOException, InterruptedExcept } /** - * Push file to a File Container. [Using the compressedBinaryDataFileId - * Property](https://docs.coveo.com/en/69) + * Push file to a File Container. [Using the compressedBinaryDataFileId Property](https://docs.coveo.com/en/69) * * @param fileContainer * @param fileAsBytes @@ -255,8 +220,7 @@ public FileContainer createFileContainer() throws IOException, InterruptedExcept * @throws IOException * @throws InterruptedException */ - public HttpResponse pushBinaryToFileContainer(FileContainer fileContainer, byte[] fileAsBytes) - throws IOException, InterruptedException { + public HttpResponse pushBinaryToFileContainer(FileContainer fileContainer, byte[] fileAsBytes) throws IOException, InterruptedException { return this.platformClient.pushBinaryToFileContainer(fileContainer, fileAsBytes); } } From 730f54db2a13f431702225fe6fcf2a9bf6e60f7f Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 12:54:49 -0400 Subject: [PATCH 07/10] replace TODO --- src/main/java/com/coveo/pushapiclient/PlatformClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 6df8fbce..f54aca7b 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -36,7 +36,7 @@ public PlatformClient(String apiKey, String organizationId) { * * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). * @param organizationId The Coveo Organization identifier. - * @param platformUrl TODO: + * @param platformUrl The PlatformUrl. */ public PlatformClient(String apiKey, String organizationId, PlatformUrl platformUrl) { this.apiKey = apiKey; From ca1aec625a1905eace2c7d8fab85725028318d7d Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 13:31:58 -0400 Subject: [PATCH 08/10] fix platformUrl --- .../com/coveo/pushapiclient/PlatformUrl.java | 20 ++++- .../pushapiclient/PlatformUrlBuilder.java | 7 +- .../pushapiclient/PlatformUrlBuilderTest.java | 73 +++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/coveo/pushapiclient/PlatformUrlBuilderTest.java diff --git a/src/main/java/com/coveo/pushapiclient/PlatformUrl.java b/src/main/java/com/coveo/pushapiclient/PlatformUrl.java index 2b1479aa..5f6830f8 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformUrl.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformUrl.java @@ -2,6 +2,9 @@ public class PlatformUrl { + public static final Environment DEFAULT_ENVIRONMENT = Environment.PRODUCTION; + public static final Region DEFAULT_REGION = Region.US; + private final Environment environment; private final Region region; @@ -17,10 +20,23 @@ public PlatformUrl(Environment environment, Region region) { } public String getPlatformUrl() { - return String.format("https://platform%s%s.cloud.coveo.com", this.environment, this.region); + return String.format("https://platform%s%s.cloud.coveo.com", this.getUrlEnvironment(), this.getUrlRegion()); } public String getApiUrl() { - return String.format("https://api%s%s.cloud.coveo.com", this.environment, this.region); + return String.format("https://api%s%s.cloud.coveo.com", this.getUrlEnvironment(), this.getUrlRegion()); + } + + private String getUrlEnvironment() { + return this.environment == PlatformUrl.DEFAULT_ENVIRONMENT + ? "" + : this.environment.getValue(); } + + private String getUrlRegion() { + return this.region == PlatformUrl.DEFAULT_REGION + ? "" + : String.format("-%s", this.region.getValue()); + } + } diff --git a/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java b/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java index 1ba59b53..3d05ff09 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformUrlBuilder.java @@ -2,11 +2,8 @@ public class PlatformUrlBuilder { - public static final Environment DEFAULT_ENVIRONMENT = Environment.PRODUCTION; - public static final Region DEFAULT_REGION = Region.US; - - private Environment environment = PlatformUrlBuilder.DEFAULT_ENVIRONMENT; - private Region region = PlatformUrlBuilder.DEFAULT_REGION; + private Environment environment = PlatformUrl.DEFAULT_ENVIRONMENT; + private Region region = PlatformUrl.DEFAULT_REGION; public PlatformUrlBuilder withEnvironment(Environment environment) { this.environment = environment; diff --git a/src/test/java/com/coveo/pushapiclient/PlatformUrlBuilderTest.java b/src/test/java/com/coveo/pushapiclient/PlatformUrlBuilderTest.java new file mode 100644 index 00000000..718fb120 --- /dev/null +++ b/src/test/java/com/coveo/pushapiclient/PlatformUrlBuilderTest.java @@ -0,0 +1,73 @@ +package com.coveo.pushapiclient; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PlatformUrlBuilderTest { + + private PlatformUrlBuilder platformUrlBuilder; + + @Before + public void setup() { + platformUrlBuilder = new PlatformUrlBuilder(); + } + + @Test + public void testWithDefaultValues() { + PlatformUrl platformUrl = platformUrlBuilder.build(); + assertEquals( + "Should return default platform URL", + "https://platform.cloud.coveo.com", + platformUrl.getPlatformUrl()); + + assertEquals( + "Should return default API URL", + "https://api.cloud.coveo.com", + platformUrl.getApiUrl()); + } + + @Test + public void testWithNonDefaultRegion() { + PlatformUrl platformUrl = platformUrlBuilder.withRegion(Region.EU).build(); + assertEquals( + "Should return Europe platform URL", + "https://platform-eu.cloud.coveo.com", + platformUrl.getPlatformUrl()); + + assertEquals( + "Should return Europe API URL", + "https://api-eu.cloud.coveo.com", + platformUrl.getApiUrl()); + } + + @Test + public void testWithNonDefaultEnvironment() { + PlatformUrl platformUrl = platformUrlBuilder.withEnvironment(Environment.STAGING).build(); + assertEquals( + "Should return the staging platform URL", + "https://platformstg.cloud.coveo.com", + platformUrl.getPlatformUrl()); + + assertEquals( + "Should return the staging API URL", + "https://apistg.cloud.coveo.com", + platformUrl.getApiUrl()); + } + + @Test + public void testWithNonDefaultEnvironmentAndRegion() { + PlatformUrl platformUrl = platformUrlBuilder + .withEnvironment(Environment.DEVELOPMENT) + .withRegion(Region.EU) + .build(); + assertEquals( + "https://platformdev-eu.cloud.coveo.com", + platformUrl.getPlatformUrl()); + + assertEquals( + "https://apidev-eu.cloud.coveo.com", + platformUrl.getApiUrl()); + } +} From df0361fb4f6a628b88a2b6988162287c7870c759 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 13:47:33 -0400 Subject: [PATCH 09/10] Upgrade Mockito --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 0e242c25..d89f1f22 100644 --- a/pom.xml +++ b/pom.xml @@ -140,7 +140,7 @@ org.mockito mockito-core - 3.11.2 + 4.7.0 test From 3c40de66315e4e2c6e283319946ca4addc19ba1c Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 26 Aug 2022 13:52:09 -0400 Subject: [PATCH 10/10] bump mockito-core --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 0e242c25..d89f1f22 100644 --- a/pom.xml +++ b/pom.xml @@ -140,7 +140,7 @@ org.mockito mockito-core - 3.11.2 + 4.7.0 test