use of com.ibm.watson.language_translator.v3.model.TranslateDocumentOptions 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);
}
use of com.ibm.watson.language_translator.v3.model.TranslateDocumentOptions in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorTest method testTranslateDocumentWOptions.
// Test the translateDocument operation with a valid options model parameter
@Test
public void testTranslateDocumentWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"document_id\": \"documentId\", \"filename\": \"filename\", \"status\": \"processing\", \"model_id\": \"modelId\", \"base_model_id\": \"baseModelId\", \"source\": \"source\", \"detected_language_confidence\": 0, \"target\": \"target\", \"created\": \"2019-01-01T12:00:00.000Z\", \"completed\": \"2019-01-01T12:00:00.000Z\", \"word_count\": 9, \"character_count\": 14}";
String translateDocumentPath = "/v3/documents";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
// Construct an instance of the TranslateDocumentOptions model
TranslateDocumentOptions translateDocumentOptionsModel = new TranslateDocumentOptions.Builder().file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/powerpoint").modelId("testString").source("testString").target("testString").documentId("testString").build();
// Invoke translateDocument() with a valid options model and verify the result
Response<DocumentStatus> response = languageTranslatorService.translateDocument(translateDocumentOptionsModel).execute();
assertNotNull(response);
DocumentStatus responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, translateDocumentPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "2018-05-01");
}
Aggregations