Search in sources :

Example 6 with DocumentStatus

use of com.ibm.watson.language_translator.v3.model.DocumentStatus in project java-sdk by watson-developer-cloud.

the class Discovery method getDocumentStatus.

/**
 * Get document details.
 *
 * <p>Fetch status details about a submitted document. **Note:** this operation does not return
 * the document itself. Instead, it returns only the document's processing status and any notices
 * (warnings or errors) that were generated when the document was ingested. Use the query API to
 * retrieve the actual document content.
 *
 * @param getDocumentStatusOptions the {@link GetDocumentStatusOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentStatus}
 */
public ServiceCall<DocumentStatus> getDocumentStatus(GetDocumentStatusOptions getDocumentStatusOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getDocumentStatusOptions, "getDocumentStatusOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", getDocumentStatusOptions.environmentId());
    pathParamsMap.put("collection_id", getDocumentStatusOptions.collectionId());
    pathParamsMap.put("document_id", getDocumentStatusOptions.documentId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/documents/{document_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "getDocumentStatus");
    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<DocumentStatus> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentStatus>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : DocumentStatus(com.ibm.watson.discovery.v1.model.DocumentStatus) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap)

Example 7 with DocumentStatus

use of com.ibm.watson.language_translator.v3.model.DocumentStatus in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method getDocumentIsSuccessful.

@Test
public void getDocumentIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(getDocResp));
    GetDocumentStatusOptions getRequest = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
    DocumentStatus response = discoveryService.getDocumentStatus(getRequest).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS2_PATH, request.getPath());
    assertEquals(GET, request.getMethod());
    assertEquals(getDocResp, response);
}
Also used : DocumentStatus(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) GetDocumentStatusOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 8 with DocumentStatus

use of com.ibm.watson.language_translator.v3.model.DocumentStatus in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method getDocumentIsSuccessful.

@Ignore
@Test
public void getDocumentIsSuccessful() {
    Collection collection = createTestCollection();
    String collectionId = collection.getCollectionId();
    DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
    GetDocumentStatusOptions getOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentAccepted.getDocumentId()).build();
    DocumentStatus getResponse = discovery.getDocumentStatus(getOptions).execute();
    assertEquals(DocumentStatus.Status.AVAILABLE, getResponse.getStatus());
}
Also used : DocumentStatus(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) GetDocumentStatusOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) Ignore(org.junit.Ignore) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 9 with DocumentStatus

use of com.ibm.watson.language_translator.v3.model.DocumentStatus in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method exampleIsSuccessful.

@Test
public void exampleIsSuccessful() {
    // Discovery discovery = new Discovery("2016-12-15");
    // discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
    // discovery.setUsernameAndPassword("<username>", "<password");
    String environmentId = null;
    String configurationId = null;
    String collectionId = null;
    String documentId = null;
    // See if an environment already exists
    System.out.println("Check if environment exists");
    ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
    ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute();
    for (Environment environment : listResponse.getEnvironments()) {
        // look for an existing environment that isn't read only
        if (!environment.isReadOnly()) {
            environmentId = environment.getEnvironmentId();
            System.out.println("Found existing environment ID: " + environmentId);
            break;
        }
    }
    if (environmentId == null) {
        System.out.println("No environment found, creating new one...");
        // no environment found, create a new one (assuming we are a FREE plan)
        String environmentName = "watson_developer_cloud_test_environment";
        CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(0L).build();
        Environment createResponse = discovery.createEnvironment(createOptions).execute();
        environmentId = createResponse.getEnvironmentId();
        System.out.println("Created new environment ID: " + environmentId);
        // wait for environment to be ready
        System.out.println("Waiting for environment to be ready...");
        boolean environmentReady = false;
        while (!environmentReady) {
            GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
            Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute();
            environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
            try {
                if (!environmentReady) {
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
        System.out.println("Environment Ready!");
    }
    // find the default configuration
    System.out.println("Finding the default configuration");
    ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
    ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute();
    for (Configuration configuration : listConfigsResponse.getConfigurations()) {
        if (configuration.getName().equals(DEFAULT_CONFIG_NAME)) {
            configurationId = configuration.getConfigurationId();
            System.out.println("Found default configuration ID: " + configurationId);
            break;
        }
    }
    // create a new collection
    System.out.println("Creating a new collection...");
    String collectionName = "my_watson_developer_cloud_collection" + UUID.randomUUID();
    CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
    Collection collection = discovery.createCollection(createCollectionOptions).execute();
    collectionId = collection.getCollectionId();
    System.out.println("Created a collection ID: " + collectionId);
    // wait for the collection to be "available"
    System.out.println("Waiting for collection to be ready...");
    boolean collectionReady = false;
    while (!collectionReady) {
        GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
        Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute();
        collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
        try {
            if (!collectionReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
    }
    System.out.println("Collection Ready!");
    // add a document
    System.out.println("Creating a new document...");
    String documentJson = "{\"field\":\"value\"}";
    InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
    AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
    createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    createDocumentBuilder.filename("test_file");
    DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute();
    documentId = createDocumentResponse.getDocumentId();
    System.out.println("Created a document ID: " + documentId);
    // wait for document to be ready
    System.out.println("Waiting for document to be ready...");
    boolean documentReady = false;
    while (!documentReady) {
        GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
        DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute();
        documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
        try {
            if (!documentReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted");
        }
    }
    System.out.println("Document Ready!");
    // query document
    System.out.println("Querying the collection...");
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:value");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
    // print out the results
    System.out.println("Query Results:");
    System.out.println(queryResponse);
    // cleanup the collection created
    System.out.println("Deleting the collection...");
    DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
    discovery.deleteCollection(deleteOptions).execute();
    System.out.println("Collection deleted!");
    System.out.println("Discovery example finished");
}
Also used : Configuration(com.ibm.watson.developer_cloud.discovery.v1.model.Configuration) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) GetEnvironmentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetEnvironmentOptions) GetCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetCollectionOptions) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) ListConfigurationsResponse(com.ibm.watson.developer_cloud.discovery.v1.model.ListConfigurationsResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ListConfigurationsOptions(com.ibm.watson.developer_cloud.discovery.v1.model.ListConfigurationsOptions) GetDocumentStatusOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions) DeleteCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteCollectionOptions) ListEnvironmentsResponse(com.ibm.watson.developer_cloud.discovery.v1.model.ListEnvironmentsResponse) DocumentStatus(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) CreateEnvironmentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateEnvironmentOptions) Environment(com.ibm.watson.developer_cloud.discovery.v1.model.Environment) CreateCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) ListEnvironmentsOptions(com.ibm.watson.developer_cloud.discovery.v1.model.ListEnvironmentsOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 10 with DocumentStatus

use of com.ibm.watson.language_translator.v3.model.DocumentStatus in project java-sdk by watson-developer-cloud.

the class LanguageTranslator method translateDocument.

/**
 * Translate document.
 *
 * <p>Submit a document for translation. You can submit the document contents in the `file`
 * parameter, or you can reference a previously submitted document by document ID. The maximum
 * file size for document translation is * 20 MB for service instances on the Standard, Advanced,
 * and Premium plans * 2 MB for service instances on the Lite plan.
 *
 * @param translateDocumentOptions the {@link TranslateDocumentOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentStatus}
 */
public ServiceCall<DocumentStatus> translateDocument(TranslateDocumentOptions translateDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(translateDocumentOptions, "translateDocumentOptions cannot be null");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/documents"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("language_translator", "v3", "translateDocument");
    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);
    okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(translateDocumentOptions.file(), translateDocumentOptions.fileContentType());
    multipartBuilder.addFormDataPart("file", translateDocumentOptions.filename(), fileBody);
    if (translateDocumentOptions.modelId() != null) {
        multipartBuilder.addFormDataPart("model_id", translateDocumentOptions.modelId());
    }
    if (translateDocumentOptions.source() != null) {
        multipartBuilder.addFormDataPart("source", translateDocumentOptions.source());
    }
    if (translateDocumentOptions.target() != null) {
        multipartBuilder.addFormDataPart("target", translateDocumentOptions.target());
    }
    if (translateDocumentOptions.documentId() != null) {
        multipartBuilder.addFormDataPart("document_id", translateDocumentOptions.documentId());
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<DocumentStatus> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentStatus>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) DocumentStatus(com.ibm.watson.language_translator.v3.model.DocumentStatus) MultipartBody(okhttp3.MultipartBody)

Aggregations

DocumentStatus (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus)6 GetDocumentStatusOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 Test (org.junit.Test)5 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)4 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)4 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)4 DocumentStatus (com.ibm.watson.discovery.v1.model.DocumentStatus)4 DocumentStatus (com.ibm.watson.language_translator.v3.model.DocumentStatus)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)3 GetDocumentStatusOptions (com.ibm.watson.discovery.v1.model.GetDocumentStatusOptions)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.testng.annotations.Test)3 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)2 Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)2 UpdateDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions)2 FileInputStream (java.io.FileInputStream)2 HashMap (java.util.HashMap)2