Skip to content

Commit fed6c3e

Browse files
committed
refactor(Language Translator): Add required filename param to TranslateDocumentOptions
1 parent 4afca11 commit fed6c3e

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎language-translator/src/main/java/com/ibm/watson/language_translator/v3/LanguageTranslator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public ServiceCall<DocumentStatus> translateDocument(TranslateDocumentOptions tr
429429
multipartBuilder.setType(MultipartBody.FORM);
430430
RequestBody fileBody = RequestUtils.inputStreamBody(translateDocumentOptions.file(), translateDocumentOptions
431431
.fileContentType());
432-
multipartBuilder.addFormDataPart("file", "filename", fileBody);
432+
multipartBuilder.addFormDataPart("file", translateDocumentOptions.filename(), fileBody);
433433
if (translateDocumentOptions.modelId() != null) {
434434
multipartBuilder.addFormDataPart("model_id", translateDocumentOptions.modelId());
435435
}

‎language-translator/src/main/java/com/ibm/watson/language_translator/v3/model/TranslateDocumentOptions.java‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class TranslateDocumentOptions extends GenericModel {
2727

2828
private InputStream file;
29+
private String filename;
2930
private String fileContentType;
3031
private String modelId;
3132
private String source;
@@ -37,6 +38,7 @@ public class TranslateDocumentOptions extends GenericModel {
3738
*/
3839
public static class Builder {
3940
private InputStream file;
41+
private String filename;
4042
private String fileContentType;
4143
private String modelId;
4244
private String source;
@@ -45,6 +47,7 @@ public static class Builder {
4547

4648
private Builder(TranslateDocumentOptions translateDocumentOptions) {
4749
this.file = translateDocumentOptions.file;
50+
this.filename = translateDocumentOptions.filename;
4851
this.fileContentType = translateDocumentOptions.fileContentType;
4952
this.modelId = translateDocumentOptions.modelId;
5053
this.source = translateDocumentOptions.source;
@@ -62,9 +65,11 @@ public Builder() {
6265
* Instantiates a new builder with required properties.
6366
*
6467
* @param file the file
68+
* @param filename the filename
6569
*/
66-
public Builder(InputStream file) {
70+
public Builder(InputStream file, String filename) {
6771
this.file = file;
72+
this.filename = filename;
6873
}
6974

7075
/**
@@ -87,6 +92,17 @@ public Builder file(InputStream file) {
8792
return this;
8893
}
8994

95+
/**
96+
* Set the filename.
97+
*
98+
* @param filename the filename
99+
* @return the TranslateDocumentOptions builder
100+
*/
101+
public Builder filename(String filename) {
102+
this.filename = filename;
103+
return this;
104+
}
105+
90106
/**
91107
* Set the fileContentType.
92108
*
@@ -152,13 +168,16 @@ public Builder documentId(String documentId) {
152168
*/
153169
public Builder file(File file) throws FileNotFoundException {
154170
this.file = new FileInputStream(file);
171+
this.filename = file.getName();
155172
return this;
156173
}
157174
}
158175

159176
private TranslateDocumentOptions(Builder builder) {
160177
Validator.notNull(builder.file, "file cannot be null");
178+
Validator.notNull(builder.filename, "filename cannot be null");
161179
file = builder.file;
180+
filename = builder.filename;
162181
fileContentType = builder.fileContentType;
163182
modelId = builder.modelId;
164183
source = builder.source;
@@ -181,9 +200,7 @@ public Builder newBuilder() {
181200
* The source file to translate.
182201
*
183202
* [Supported file
184-
* types]
185-
* (https://cloud.ibm.com/docs/services/language-translator
186-
* ?topic=language-translator-document-translator-tutorial#supported-file-formats)
203+
* types](https://cloud.ibm.com/docs/services/language-translator?topic=language-translator-document-translator-tutorial#supported-file-formats)
187204
*
188205
* Maximum file size: **20 MB**.
189206
*
@@ -193,6 +210,17 @@ public InputStream file() {
193210
return file;
194211
}
195212

213+
/**
214+
* Gets the filename.
215+
*
216+
* The filename for file.
217+
*
218+
* @return the filename
219+
*/
220+
public String filename() {
221+
return filename;
222+
}
223+
196224
/**
197225
* Gets the fileContentType.
198226
*

‎language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorIT.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public void testDocumentTranslation() throws FileNotFoundException, InterruptedE
226226
TranslateDocumentOptions translateOptions = new TranslateDocumentOptions.Builder()
227227
.file(new File("src/test/resources/language_translation/document_to_translate.txt"))
228228
.fileContentType(HttpMediaType.TEXT_PLAIN)
229+
.filename("java-integration-test-file")
229230
.source("en")
230231
.target("es")
231232
.build();

‎language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorTest.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ public void testListDocumentsNoOptions() throws InterruptedException {
420420
public void testTranslateDocumentOptions() throws FileNotFoundException {
421421
File file = new File(RESOURCE + "document_to_translate.txt");
422422
String fileContentType = HttpMediaType.TEXT_PLAIN;
423+
String filename = "filename";
423424
String modelId = "modelId";
424425
String source = "en";
425426
String target = "es";
@@ -428,6 +429,7 @@ public void testTranslateDocumentOptions() throws FileNotFoundException {
428429
TranslateDocumentOptions options = new TranslateDocumentOptions.Builder()
429430
.file(file)
430431
.fileContentType(fileContentType)
432+
.filename(filename)
431433
.modelId(modelId)
432434
.source(source)
433435
.target(target)
@@ -437,6 +439,7 @@ public void testTranslateDocumentOptions() throws FileNotFoundException {
437439

438440
assertNotNull(options.file());
439441
assertEquals(fileContentType, options.fileContentType());
442+
assertEquals(filename, options.filename());
440443
assertEquals(modelId, options.modelId());
441444
assertEquals(source, options.source());
442445
assertEquals(target, options.target());

0 commit comments

Comments
 (0)