Skip to content

Commit 2e72bad

Browse files
committed
chore(Visual Recognition): Apply manual changes
1 parent 58a8b61 commit 2e72bad

File tree

5 files changed

+155
-8
lines changed

5 files changed

+155
-8
lines changed

visual-recognition/src/main/java/com/ibm/watson/developer_cloud/visual_recognition/v3/VisualRecognition.java

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetCoreMlModelOptions;
3333
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.ListClassifiersOptions;
3434
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions;
35-
import java.io.InputStream;
3635
import okhttp3.MultipartBody;
3736
import okhttp3.RequestBody;
3837

38+
import java.io.InputStream;
39+
import java.util.Map;
40+
3941
/**
4042
* The IBM Watson™ Visual Recognition service uses deep learning algorithms to identify scenes, objects, and faces
4143
* in images you upload to the service. You can create and train a custom classifier to identify subjects that suit
@@ -68,6 +70,52 @@ public VisualRecognition(String versionDate) {
6870
this.versionDate = versionDate;
6971
}
7072

73+
/**
74+
* Instantiates a new `VisualRecognition` with API Key.
75+
*
76+
* @param versionDate The version date (yyyy-MM-dd) of the REST API to use. Specifying this value will keep your API
77+
* calls from failing when the service introduces breaking changes.
78+
* @param apiKey the API Key
79+
* @deprecated This form of authentication is deprecated and will be removed in the next major release. Please
80+
* authenticate using IAM credentials, using either the (String, IamOptions) constructor or with the
81+
* setIamCredentials() method.
82+
*/
83+
public VisualRecognition(String versionDate, String apiKey) {
84+
this(versionDate);
85+
setApiKey(apiKey);
86+
}
87+
88+
/*
89+
* (non-Javadoc)
90+
*/
91+
@Override
92+
protected void setAuthentication(okhttp3.Request.Builder builder) {
93+
if ((getUsername() != null && getPassword() != null) || isTokenManagerSet()) {
94+
super.setAuthentication(builder);
95+
} else if (getApiKey() != null) {
96+
addApiKeyQueryParameter(builder, getApiKey());
97+
} else {
98+
throw new IllegalArgumentException(
99+
"Credentials need to be specified. Use setApiKey(), setIamCredentials(), or setUsernameAndPassword().");
100+
}
101+
}
102+
103+
/**
104+
* Adds the API key as a query parameter to the request URL.
105+
*
106+
* @param builder builder for the current request
107+
* @param apiKey API key to be added
108+
*/
109+
private void addApiKeyQueryParameter(okhttp3.Request.Builder builder, String apiKey) {
110+
final okhttp3.HttpUrl url = okhttp3.HttpUrl.parse(builder.build().url().toString());
111+
112+
if ((url.query() == null) || url.query().isEmpty()) {
113+
builder.url(builder.build().url() + "?api_key=" + apiKey);
114+
} else {
115+
builder.url(builder.build().url() + "&api_key=" + apiKey);
116+
}
117+
}
118+
71119
/**
72120
* Instantiates a new `VisualRecognition` with IAM. Note that if the access token is specified in the
73121
* iamOptions, you accept responsibility for managing the access token yourself. You must set a new access token
@@ -94,9 +142,13 @@ public VisualRecognition(String versionDate, IamOptions iamOptions) {
94142
*/
95143
public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
96144
Validator.notNull(classifyOptions, "classifyOptions cannot be null");
97-
Validator.isTrue((classifyOptions.imagesFile() != null) || (classifyOptions.url() != null) || (classifyOptions
98-
.threshold() != null) || (classifyOptions.owners() != null) || (classifyOptions.classifierIds() != null),
99-
"At least one of imagesFile, url, threshold, owners, or classifierIds must be supplied.");
145+
Validator.isTrue((classifyOptions.imagesFile() != null)
146+
|| (classifyOptions.url() != null)
147+
|| (classifyOptions.threshold() != null)
148+
|| (classifyOptions.owners() != null)
149+
|| (classifyOptions.classifierIds() != null)
150+
|| (classifyOptions.parameters() != null),
151+
"At least one of imagesFile, url, threshold, owners, classifierIds, or parameters must be supplied.");
100152
String[] pathSegments = { "v3/classify" };
101153
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
102154
builder.query(VERSION, versionDate);
@@ -110,6 +162,9 @@ public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
110162
.imagesFileContentType());
111163
multipartBuilder.addFormDataPart("images_file", classifyOptions.imagesFilename(), imagesFileBody);
112164
}
165+
if (classifyOptions.parameters() != null) {
166+
multipartBuilder.addFormDataPart("parameters", classifyOptions.parameters());
167+
}
113168
if (classifyOptions.url() != null) {
114169
multipartBuilder.addFormDataPart("url", classifyOptions.url());
115170
}
@@ -157,8 +212,10 @@ public ServiceCall<ClassifiedImages> classify() {
157212
*/
158213
public ServiceCall<DetectedFaces> detectFaces(DetectFacesOptions detectFacesOptions) {
159214
Validator.notNull(detectFacesOptions, "detectFacesOptions cannot be null");
160-
Validator.isTrue((detectFacesOptions.imagesFile() != null) || (detectFacesOptions.url() != null),
161-
"At least one of imagesFile or url must be supplied.");
215+
Validator.isTrue((detectFacesOptions.imagesFile() != null)
216+
|| (detectFacesOptions.url() != null)
217+
|| (detectFacesOptions.parameters() != null),
218+
"At least one of imagesFile, url, or parameters must be supplied.");
162219
String[] pathSegments = { "v3/detect_faces" };
163220
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
164221
builder.query(VERSION, versionDate);
@@ -169,6 +226,9 @@ public ServiceCall<DetectedFaces> detectFaces(DetectFacesOptions detectFacesOpti
169226
.imagesFileContentType());
170227
multipartBuilder.addFormDataPart("images_file", detectFacesOptions.imagesFilename(), imagesFileBody);
171228
}
229+
if (detectFacesOptions.parameters() != null) {
230+
multipartBuilder.addFormDataPart("parameters", detectFacesOptions.parameters());
231+
}
172232
if (detectFacesOptions.url() != null) {
173233
multipartBuilder.addFormDataPart("url", detectFacesOptions.url());
174234
}
@@ -302,7 +362,8 @@ public ServiceCall<Classifiers> listClassifiers() {
302362
* Update a custom classifier by adding new positive or negative classes (examples) or by adding new images to
303363
* existing classes. You must supply at least one set of positive or negative examples. For details, see [Updating
304364
* custom
305-
* classifiers](https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
365+
* classifiers]
366+
* (https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
306367
*
307368
* Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image file names, and classifier and class
308369
* names). The service assumes UTF-8 encoding if it encounters non-ASCII characters.
@@ -360,7 +421,7 @@ public ServiceCall<InputStream> getCoreMlModel(GetCoreMlModelOptions getCoreMlMo
360421
RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments,
361422
pathParameters));
362423
builder.query(VERSION, versionDate);
363-
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(InputStream.class));
424+
return createServiceCall(builder.build(), ResponseConverterUtils.getInputStream());
364425
}
365426

366427
/**

visual-recognition/src/main/java/com/ibm/watson/developer_cloud/visual_recognition/v3/model/ClassifyOptions.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public interface AcceptLanguage {
6767
private List<String> owners;
6868
private List<String> classifierIds;
6969
private String imagesFileContentType;
70+
@Deprecated
71+
private String parameters;
7072

7173
/**
7274
* Builder.
@@ -80,6 +82,8 @@ public static class Builder {
8082
private List<String> owners;
8183
private List<String> classifierIds;
8284
private String imagesFileContentType;
85+
@Deprecated
86+
private String parameters;
8387

8488
private Builder(ClassifyOptions classifyOptions) {
8589
imagesFile = classifyOptions.imagesFile;
@@ -90,6 +94,7 @@ private Builder(ClassifyOptions classifyOptions) {
9094
owners = classifyOptions.owners;
9195
classifierIds = classifyOptions.classifierIds;
9296
imagesFileContentType = classifyOptions.imagesFileContentType;
97+
parameters = classifyOptions.parameters;
9398
}
9499

95100
/**
@@ -240,6 +245,18 @@ public Builder imagesFile(File imagesFile) throws FileNotFoundException {
240245
this.imagesFilename = imagesFile.getName();
241246
return this;
242247
}
248+
249+
/**
250+
* Set the parameters.
251+
*
252+
* @param parameters the parameters
253+
* @return the ClassifyOptions builder
254+
* @deprecated replaced by the top-level parameters url, threshold, owners, and classifierIds
255+
*/
256+
public Builder parameters(String parameters) {
257+
this.parameters = parameters;
258+
return this;
259+
}
243260
}
244261

245262
private ClassifyOptions(Builder builder) {
@@ -251,6 +268,7 @@ private ClassifyOptions(Builder builder) {
251268
owners = builder.owners;
252269
classifierIds = builder.classifierIds;
253270
imagesFileContentType = builder.imagesFileContentType;
271+
parameters = builder.parameters;
254272
}
255273

256274
/**
@@ -373,4 +391,14 @@ public List<String> classifierIds() {
373391
public String imagesFileContentType() {
374392
return imagesFileContentType;
375393
}
394+
395+
/**
396+
* Gets the parameters.
397+
*
398+
* @return the parameters
399+
* @deprecated replaced by the top-level parameters url, threshold, owners, and classifierIds
400+
*/
401+
public String parameters() {
402+
return parameters;
403+
}
376404
}

visual-recognition/src/main/java/com/ibm/watson/developer_cloud/visual_recognition/v3/model/CreateClassifierOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ public Builder negativeExamples(File negativeExamples) throws FileNotFoundExcept
194194
this.negativeExamplesFilename = negativeExamples.getName();
195195
return this;
196196
}
197+
198+
/**
199+
* Adds a classifier with a name and positive examples. If the classifier name is already contained, the old
200+
* positive examples are replaced by the specified value.
201+
*
202+
* @param classname the class name
203+
* @param positiveExamples the positive examples
204+
* @return the builder
205+
* @throws FileNotFoundException if the file could not be found
206+
* @deprecated This method has been replaced by addPositiveExamples(String, File) and will be removed in the next
207+
* major release
208+
*/
209+
public Builder addClass(String classname, File positiveExamples) throws FileNotFoundException {
210+
return addPositiveExamples(classname, positiveExamples);
211+
}
197212
}
198213

199214
private CreateClassifierOptions(Builder builder) {

visual-recognition/src/main/java/com/ibm/watson/developer_cloud/visual_recognition/v3/model/DetectFacesOptions.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class DetectFacesOptions extends GenericModel {
2828
private String imagesFilename;
2929
private String url;
3030
private String imagesFileContentType;
31+
@Deprecated
32+
private String parameters;
3133

3234
/**
3335
* Builder.
@@ -37,12 +39,15 @@ public static class Builder {
3739
private String imagesFilename;
3840
private String url;
3941
private String imagesFileContentType;
42+
@Deprecated
43+
private String parameters;
4044

4145
private Builder(DetectFacesOptions detectFacesOptions) {
4246
imagesFile = detectFacesOptions.imagesFile;
4347
imagesFilename = detectFacesOptions.imagesFilename;
4448
url = detectFacesOptions.url;
4549
imagesFileContentType = detectFacesOptions.imagesFileContentType;
50+
parameters = detectFacesOptions.parameters;
4651
}
4752

4853
/**
@@ -117,13 +122,26 @@ public Builder imagesFile(File imagesFile) throws FileNotFoundException {
117122
this.imagesFilename = imagesFile.getName();
118123
return this;
119124
}
125+
126+
/**
127+
* Set the parameters.
128+
*
129+
* @param parameters the parameters
130+
* @return the DetectFacesOptions builder
131+
* @deprecated replaced by the url parameter
132+
*/
133+
public Builder parameters(String parameters) {
134+
this.parameters = parameters;
135+
return this;
136+
}
120137
}
121138

122139
private DetectFacesOptions(Builder builder) {
123140
imagesFile = builder.imagesFile;
124141
imagesFilename = builder.imagesFilename;
125142
url = builder.url;
126143
imagesFileContentType = builder.imagesFileContentType;
144+
parameters = builder.parameters;
127145
}
128146

129147
/**
@@ -188,4 +206,14 @@ public String url() {
188206
public String imagesFileContentType() {
189207
return imagesFileContentType;
190208
}
209+
210+
/**
211+
* Gets the parameters.
212+
*
213+
* @return the parameters
214+
* @deprecated replaced by the url parameter
215+
*/
216+
public String parameters() {
217+
return parameters;
218+
}
191219
}

visual-recognition/src/main/java/com/ibm/watson/developer_cloud/visual_recognition/v3/model/UpdateClassifierOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ public Builder negativeExamples(File negativeExamples) throws FileNotFoundExcept
194194
this.negativeExamplesFilename = negativeExamples.getName();
195195
return this;
196196
}
197+
198+
/**
199+
* Adds a classifier with a name and positive examples. If the classifier name is already contained, the old
200+
* positive examples are replaced by the specified value.
201+
*
202+
* @param classname the class name
203+
* @param positiveExamples the positive examples
204+
* @return the builder
205+
* @throws FileNotFoundException if the file could not be found
206+
* @deprecated This method has been replaced by addPositiveExamples(String, File) and will be removed in the next
207+
* major release
208+
*/
209+
public Builder addClass(String classname, File positiveExamples) throws FileNotFoundException {
210+
return addPositiveExamples(classname, positiveExamples);
211+
}
197212
}
198213

199214
private UpdateClassifierOptions(Builder builder) {

0 commit comments

Comments
 (0)