Skip to content

Commit

Permalink
feat(discov2): add methods for new batches api
Browse files Browse the repository at this point in the history
  • Loading branch information
apaparazzi0329 committed Dec 5, 2024
1 parent 254428b commit 750a115
Show file tree
Hide file tree
Showing 17 changed files with 1,228 additions and 11 deletions.
128 changes: 128 additions & 0 deletions discovery/src/main/java/com/ibm/watson/discovery/v2/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
import com.ibm.watson.discovery.v2.model.GetProjectOptions;
import com.ibm.watson.discovery.v2.model.GetStopwordListOptions;
import com.ibm.watson.discovery.v2.model.GetTrainingQueryOptions;
import com.ibm.watson.discovery.v2.model.ListBatchesOptions;
import com.ibm.watson.discovery.v2.model.ListBatchesResponse;
import com.ibm.watson.discovery.v2.model.ListCollectionsOptions;
import com.ibm.watson.discovery.v2.model.ListCollectionsResponse;
import com.ibm.watson.discovery.v2.model.ListDocumentClassifierModelsOptions;
Expand All @@ -86,6 +88,9 @@
import com.ibm.watson.discovery.v2.model.ListProjectsResponse;
import com.ibm.watson.discovery.v2.model.ListTrainingQueriesOptions;
import com.ibm.watson.discovery.v2.model.ProjectDetails;
import com.ibm.watson.discovery.v2.model.PullBatchesOptions;
import com.ibm.watson.discovery.v2.model.PullBatchesResponse;
import com.ibm.watson.discovery.v2.model.PushBatchesOptions;
import com.ibm.watson.discovery.v2.model.QueryCollectionNoticesOptions;
import com.ibm.watson.discovery.v2.model.QueryNoticesOptions;
import com.ibm.watson.discovery.v2.model.QueryNoticesResponse;
Expand Down Expand Up @@ -1794,6 +1799,129 @@ public ServiceCall<Void> deleteEnrichment(DeleteEnrichmentOptions deleteEnrichme
return createServiceCall(builder.build(), responseConverter);
}

/**
* List batches.
*
* <p>A batch is a set of documents that are ready for enrichment by an external application.
* After you apply a webhook enrichment to a collection, and then process or upload documents to
* the collection, Discovery creates a batch with a unique **batch_id**.
*
* <p>To start, you must register your external application as a **webhook** type by using the
* [Create enrichment API](/apidocs/discovery-data#createenrichment) method.
*
* <p>Use the List batches API to get the following:
*
* <p>* Notified batches that are not yet pulled by the external enrichment application.
*
* <p>* Batches that are pulled, but not yet pushed to Discovery by the external enrichment
* application.
*
* @param listBatchesOptions the {@link ListBatchesOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link ListBatchesResponse}
*/
public ServiceCall<ListBatchesResponse> listBatches(ListBatchesOptions listBatchesOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(
listBatchesOptions, "listBatchesOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", listBatchesOptions.projectId());
pathParamsMap.put("collection_id", listBatchesOptions.collectionId());
RequestBuilder builder =
RequestBuilder.get(
RequestBuilder.resolveRequestUrl(
getServiceUrl(),
"/v2/projects/{project_id}/collections/{collection_id}/batches",
pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "listBatches");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
ResponseConverter<ListBatchesResponse> responseConverter =
ResponseConverterUtils.getValue(
new com.google.gson.reflect.TypeToken<ListBatchesResponse>() {}.getType());
return createServiceCall(builder.build(), responseConverter);
}

/**
* Pull batches.
*
* <p>Pull a batch of documents from Discovery for enrichment by an external application. Ensure
* to include the `Accept-Encoding: gzip` header in this method to get the file. You can also
* implement retry logic when calling this method to avoid any network errors.
*
* @param pullBatchesOptions the {@link PullBatchesOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link PullBatchesResponse}
*/
public ServiceCall<PullBatchesResponse> pullBatches(PullBatchesOptions pullBatchesOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(
pullBatchesOptions, "pullBatchesOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", pullBatchesOptions.projectId());
pathParamsMap.put("collection_id", pullBatchesOptions.collectionId());
pathParamsMap.put("batch_id", pullBatchesOptions.batchId());
RequestBuilder builder =
RequestBuilder.get(
RequestBuilder.resolveRequestUrl(
getServiceUrl(),
"/v2/projects/{project_id}/collections/{collection_id}/batches/{batch_id}",
pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "pullBatches");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
ResponseConverter<PullBatchesResponse> responseConverter =
ResponseConverterUtils.getValue(
new com.google.gson.reflect.TypeToken<PullBatchesResponse>() {}.getType());
return createServiceCall(builder.build(), responseConverter);
}

/**
* Push batches.
*
* <p>Push a batch of documents to Discovery after annotation by an external application. You can
* implement retry logic when calling this method to avoid any network errors.
*
* @param pushBatchesOptions the {@link PushBatchesOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link Boolean}
*/
public ServiceCall<Boolean> pushBatches(PushBatchesOptions pushBatchesOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(
pushBatchesOptions, "pushBatchesOptions cannot be null");
com.ibm.cloud.sdk.core.util.Validator.isTrue(
(pushBatchesOptions.file() != null), "At least one of or file must be supplied.");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", pushBatchesOptions.projectId());
pathParamsMap.put("collection_id", pushBatchesOptions.collectionId());
pathParamsMap.put("batch_id", pushBatchesOptions.batchId());
RequestBuilder builder =
RequestBuilder.post(
RequestBuilder.resolveRequestUrl(
getServiceUrl(),
"/v2/projects/{project_id}/collections/{collection_id}/batches/{batch_id}",
pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "pushBatches");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (pushBatchesOptions.file() != null) {
okhttp3.RequestBody fileBody =
RequestUtils.inputStreamBody(pushBatchesOptions.file(), "application/octet-stream");
multipartBuilder.addFormDataPart("file", pushBatchesOptions.filename(), fileBody);
}
builder.body(multipartBuilder.build());
ResponseConverter<Boolean> responseConverter =
ResponseConverterUtils.getValue(
new com.google.gson.reflect.TypeToken<Boolean>() {}.getType());
return createServiceCall(builder.build(), responseConverter);
}

/**
* List document classifiers.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* (C) Copyright IBM Corp. 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.ibm.watson.discovery.v2.model;

import com.google.gson.annotations.SerializedName;
import com.ibm.cloud.sdk.core.service.model.GenericModel;
import java.util.Date;

/**
* A batch is a set of documents that are ready for enrichment by an external application. After you
* apply a webhook enrichment to a collection, and then process or upload documents to the
* collection, Discovery creates a batch with a unique **batch_id**.
*/
public class BatchDetails extends GenericModel {

@SerializedName("batch_id")
protected String batchId;

protected Date created;

@SerializedName("enrichment_id")
protected String enrichmentId;

protected BatchDetails() {}

/**
* Gets the batchId.
*
* <p>The Universally Unique Identifier (UUID) for a batch of documents.
*
* @return the batchId
*/
public String getBatchId() {
return batchId;
}

/**
* Gets the created.
*
* <p>The date and time (RFC3339) that the batch was created.
*
* @return the created
*/
public Date getCreated() {
return created;
}

/**
* Gets the enrichmentId.
*
* <p>The Universally Unique Identifier (UUID) for the external enrichment.
*
* @return the enrichmentId
*/
public String getEnrichmentId() {
return enrichmentId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public class CreateEnrichment extends GenericModel {
* <p>* `watson_knowledge_studio_model`: Creates an enrichment from a Watson Knowledge Studio
* machine learning model that is defined in a ZIP file.
*
* <p>* `webhook`: Connects to an external enrichment application by using a webhook. The feature
* is available from IBM Cloud-managed instances only. The external enrichment feature is beta
* functionality. Beta features are not supported by the SDKs.
* <p>* `webhook`: Connects to an external enrichment application by using a webhook.
*
* <p>* `sentence_classifier`: Use sentence classifier to classify sentences in your documents.
* This feature is available in IBM Cloud-managed instances only. The sentence classifier feature
Expand Down Expand Up @@ -221,9 +219,7 @@ public String description() {
* <p>* `watson_knowledge_studio_model`: Creates an enrichment from a Watson Knowledge Studio
* machine learning model that is defined in a ZIP file.
*
* <p>* `webhook`: Connects to an external enrichment application by using a webhook. The feature
* is available from IBM Cloud-managed instances only. The external enrichment feature is beta
* functionality. Beta features are not supported by the SDKs.
* <p>* `webhook`: Connects to an external enrichment application by using a webhook.
*
* <p>* `sentence_classifier`: Use sentence classifier to classify sentences in your documents.
* This feature is available in IBM Cloud-managed instances only. The sentence classifier feature
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* (C) Copyright IBM Corp. 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.ibm.watson.discovery.v2.model;

import com.ibm.cloud.sdk.core.service.model.GenericModel;

/** The listBatches options. */
public class ListBatchesOptions extends GenericModel {

protected String projectId;
protected String collectionId;

/** Builder. */
public static class Builder {
private String projectId;
private String collectionId;

/**
* Instantiates a new Builder from an existing ListBatchesOptions instance.
*
* @param listBatchesOptions the instance to initialize the Builder with
*/
private Builder(ListBatchesOptions listBatchesOptions) {
this.projectId = listBatchesOptions.projectId;
this.collectionId = listBatchesOptions.collectionId;
}

/** Instantiates a new builder. */
public Builder() {}

/**
* Instantiates a new builder with required properties.
*
* @param projectId the projectId
* @param collectionId the collectionId
*/
public Builder(String projectId, String collectionId) {
this.projectId = projectId;
this.collectionId = collectionId;
}

/**
* Builds a ListBatchesOptions.
*
* @return the new ListBatchesOptions instance
*/
public ListBatchesOptions build() {
return new ListBatchesOptions(this);
}

/**
* Set the projectId.
*
* @param projectId the projectId
* @return the ListBatchesOptions builder
*/
public Builder projectId(String projectId) {
this.projectId = projectId;
return this;
}

/**
* Set the collectionId.
*
* @param collectionId the collectionId
* @return the ListBatchesOptions builder
*/
public Builder collectionId(String collectionId) {
this.collectionId = collectionId;
return this;
}
}

protected ListBatchesOptions() {}

protected ListBatchesOptions(Builder builder) {
com.ibm.cloud.sdk.core.util.Validator.notEmpty(builder.projectId, "projectId cannot be empty");
com.ibm.cloud.sdk.core.util.Validator.notEmpty(
builder.collectionId, "collectionId cannot be empty");
projectId = builder.projectId;
collectionId = builder.collectionId;
}

/**
* New builder.
*
* @return a ListBatchesOptions builder
*/
public Builder newBuilder() {
return new Builder(this);
}

/**
* Gets the projectId.
*
* <p>The Universally Unique Identifier (UUID) of the project. This information can be found from
* the *Integrate and Deploy* page in Discovery.
*
* @return the projectId
*/
public String projectId() {
return projectId;
}

/**
* Gets the collectionId.
*
* <p>The Universally Unique Identifier (UUID) of the collection.
*
* @return the collectionId
*/
public String collectionId() {
return collectionId;
}
}
Loading

0 comments on commit 750a115

Please sign in to comment.