Search in sources :

Example 1 with AnalyzedDocument

use of com.ibm.watson.discovery.v2.model.AnalyzedDocument in project java-sdk by watson-developer-cloud.

the class Discovery method analyzeDocument.

/**
 * Analyze a Document.
 *
 * <p>Process a document and return it for realtime use. Supports JSON files only.
 *
 * <p>The document is processed according to the collection's configuration settings but is not
 * stored in the collection.
 *
 * <p>**Note:** This method is supported on installed instances of Discovery only.
 *
 * @param analyzeDocumentOptions the {@link AnalyzeDocumentOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link AnalyzedDocument}
 */
public ServiceCall<AnalyzedDocument> analyzeDocument(AnalyzeDocumentOptions analyzeDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(analyzeDocumentOptions, "analyzeDocumentOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((analyzeDocumentOptions.file() != null) || (analyzeDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", analyzeDocumentOptions.projectId());
    pathParamsMap.put("collection_id", analyzeDocumentOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/collections/{collection_id}/analyze", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "analyzeDocument");
    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 (analyzeDocumentOptions.file() != null) {
        okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(analyzeDocumentOptions.file(), analyzeDocumentOptions.fileContentType());
        multipartBuilder.addFormDataPart("file", analyzeDocumentOptions.filename(), fileBody);
    }
    if (analyzeDocumentOptions.metadata() != null) {
        multipartBuilder.addFormDataPart("metadata", analyzeDocumentOptions.metadata());
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<AnalyzedDocument> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<AnalyzedDocument>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) AnalyzedDocument(com.ibm.watson.discovery.v2.model.AnalyzedDocument) MultipartBody(okhttp3.MultipartBody)

Example 2 with AnalyzedDocument

use of com.ibm.watson.discovery.v2.model.AnalyzedDocument in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testAnalyzeDocumentWOptions.

@Test
public void testAnalyzeDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"notices\": [{\"notice_id\": \"noticeId\", \"created\": \"2019-01-01T12:00:00.000Z\", \"document_id\": \"documentId\", \"collection_id\": \"collectionId\", \"query_id\": \"queryId\", \"severity\": \"warning\", \"step\": \"step\", \"description\": \"description\"}], \"result\": {\"metadata\": {\"mapKey\": \"anyValue\"}}}";
    String analyzeDocumentPath = "/v2/projects/testString/collections/testString/analyze";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the AnalyzeDocumentOptions model
    AnalyzeDocumentOptions analyzeDocumentOptionsModel = new AnalyzeDocumentOptions.Builder().projectId("testString").collectionId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<AnalyzedDocument> response = discoveryService.analyzeDocument(analyzeDocumentOptionsModel).execute();
    assertNotNull(response);
    AnalyzedDocument responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, analyzeDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) AnalyzedDocument(com.ibm.watson.discovery.v2.model.AnalyzedDocument) AnalyzeDocumentOptions(com.ibm.watson.discovery.v2.model.AnalyzeDocumentOptions) Test(org.testng.annotations.Test)

Aggregations

AnalyzedDocument (com.ibm.watson.discovery.v2.model.AnalyzedDocument)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 AnalyzeDocumentOptions (com.ibm.watson.discovery.v2.model.AnalyzeDocumentOptions)1 HashMap (java.util.HashMap)1 MultipartBody (okhttp3.MultipartBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.testng.annotations.Test)1