1919import com .ibm .cloud .sdk .core .util .RequestUtils ;
2020import com .ibm .cloud .sdk .core .util .ResponseConverterUtils ;
2121import com .ibm .cloud .sdk .core .util .Validator ;
22+ import com .ibm .watson .common .SdkCommon ;
2223import com .ibm .watson .visual_recognition .v3 .model .ClassifiedImages ;
2324import com .ibm .watson .visual_recognition .v3 .model .Classifier ;
2425import com .ibm .watson .visual_recognition .v3 .model .Classifiers ;
3233import com .ibm .watson .visual_recognition .v3 .model .GetCoreMlModelOptions ;
3334import com .ibm .watson .visual_recognition .v3 .model .ListClassifiersOptions ;
3435import com .ibm .watson .visual_recognition .v3 .model .UpdateClassifierOptions ;
35- import okhttp3 .MultipartBody ;
36- import okhttp3 .RequestBody ;
37-
3836import java .io .InputStream ;
3937import java .util .Map ;
38+ import java .util .Map .Entry ;
39+ import okhttp3 .MultipartBody ;
40+ import okhttp3 .RequestBody ;
4041
4142/**
4243 * The IBM Watson™ Visual Recognition service uses deep learning algorithms to identify scenes, objects, and faces
@@ -70,52 +71,6 @@ public VisualRecognition(String versionDate) {
7071 this .versionDate = versionDate ;
7172 }
7273
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-
11974 /**
12075 * Instantiates a new `VisualRecognition` with IAM. Note that if the access token is specified in the
12176 * iamOptions, you accept responsibility for managing the access token yourself. You must set a new access token
@@ -142,18 +97,17 @@ public VisualRecognition(String versionDate, IamOptions iamOptions) {
14297 */
14398 public ServiceCall <ClassifiedImages > classify (ClassifyOptions classifyOptions ) {
14499 Validator .notNull (classifyOptions , "classifyOptions cannot be null" );
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." );
100+ Validator .isTrue ((classifyOptions .imagesFile () != null ) || (classifyOptions .url () != null ) || (classifyOptions
101+ .threshold () != null ) || (classifyOptions .owners () != null ) || (classifyOptions .classifierIds () != null ),
102+ "At least one of imagesFile, url, threshold, owners, or classifierIds must be supplied." );
152103 String [] pathSegments = { "v3/classify" };
153104 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
154105 builder .query ("version" , versionDate );
155- builder .header ("X-IBMCloud-SDK-Analytics" ,
156- "service_name=watson_vision_combined;service_version=v3;operation_id=classify" );
106+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "classify" );
107+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
108+ builder .header (header .getKey (), header .getValue ());
109+ }
110+ builder .header ("Accept" , "application/json" );
157111 if (classifyOptions .acceptLanguage () != null ) {
158112 builder .header ("Accept-Language" , classifyOptions .acceptLanguage ());
159113 }
@@ -164,9 +118,6 @@ public ServiceCall<ClassifiedImages> classify(ClassifyOptions classifyOptions) {
164118 .imagesFileContentType ());
165119 multipartBuilder .addFormDataPart ("images_file" , classifyOptions .imagesFilename (), imagesFileBody );
166120 }
167- if (classifyOptions .parameters () != null ) {
168- multipartBuilder .addFormDataPart ("parameters" , classifyOptions .parameters ());
169- }
170121 if (classifyOptions .url () != null ) {
171122 multipartBuilder .addFormDataPart ("url" , classifyOptions .url ());
172123 }
@@ -215,15 +166,16 @@ public ServiceCall<ClassifiedImages> classify() {
215166 */
216167 public ServiceCall <DetectedFaces > detectFaces (DetectFacesOptions detectFacesOptions ) {
217168 Validator .notNull (detectFacesOptions , "detectFacesOptions cannot be null" );
218- Validator .isTrue ((detectFacesOptions .imagesFile () != null )
219- || (detectFacesOptions .url () != null )
220- || (detectFacesOptions .parameters () != null ),
221- "At least one of imagesFile, url, or parameters must be supplied." );
169+ Validator .isTrue ((detectFacesOptions .imagesFile () != null ) || (detectFacesOptions .url () != null ),
170+ "At least one of imagesFile or url must be supplied." );
222171 String [] pathSegments = { "v3/detect_faces" };
223172 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
224173 builder .query ("version" , versionDate );
225- builder .header ("X-IBMCloud-SDK-Analytics" ,
226- "service_name=watson_vision_combined;service_version=v3;operation_id=detectFaces" );
174+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "detectFaces" );
175+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
176+ builder .header (header .getKey (), header .getValue ());
177+ }
178+ builder .header ("Accept" , "application/json" );
227179 if (detectFacesOptions .acceptLanguage () != null ) {
228180 builder .header ("Accept-Language" , detectFacesOptions .acceptLanguage ());
229181 }
@@ -234,9 +186,6 @@ public ServiceCall<DetectedFaces> detectFaces(DetectFacesOptions detectFacesOpti
234186 .imagesFileContentType ());
235187 multipartBuilder .addFormDataPart ("images_file" , detectFacesOptions .imagesFilename (), imagesFileBody );
236188 }
237- if (detectFacesOptions .parameters () != null ) {
238- multipartBuilder .addFormDataPart ("parameters" , detectFacesOptions .parameters ());
239- }
240189 if (detectFacesOptions .url () != null ) {
241190 multipartBuilder .addFormDataPart ("url" , detectFacesOptions .url ());
242191 }
@@ -284,17 +233,18 @@ public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createCl
284233 String [] pathSegments = { "v3/classifiers" };
285234 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
286235 builder .query ("version" , versionDate );
287- builder .header ("X-IBMCloud-SDK-Analytics" ,
288- "service_name=watson_vision_combined;service_version=v3;operation_id=createClassifier" );
236+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "createClassifier" );
237+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
238+ builder .header (header .getKey (), header .getValue ());
239+ }
240+ builder .header ("Accept" , "application/json" );
289241 MultipartBody .Builder multipartBuilder = new MultipartBody .Builder ();
290242 multipartBuilder .setType (MultipartBody .FORM );
291243 multipartBuilder .addFormDataPart ("name" , createClassifierOptions .name ());
292244 for (Map .Entry <String , InputStream > entry : createClassifierOptions .positiveExamples ().entrySet ()) {
293245 String partName = String .format ("%s_positive_examples" , entry .getKey ());
294- String fileName = createClassifierOptions .positiveExamplesFilename () != null ? createClassifierOptions
295- .positiveExamplesFilename ().get (entry .getKey ()) : null ;
296246 RequestBody part = RequestUtils .inputStreamBody (entry .getValue (), "application/octet-stream" );
297- multipartBuilder .addFormDataPart (partName , fileName , part );
247+ multipartBuilder .addFormDataPart (partName , entry . getKey () , part );
298248 }
299249 if (createClassifierOptions .negativeExamples () != null ) {
300250 RequestBody negativeExamplesBody = RequestUtils .inputStreamBody (createClassifierOptions .negativeExamples (),
@@ -319,8 +269,11 @@ public ServiceCall<Void> deleteClassifier(DeleteClassifierOptions deleteClassifi
319269 RequestBuilder builder = RequestBuilder .delete (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
320270 pathParameters ));
321271 builder .query ("version" , versionDate );
322- builder .header ("X-IBMCloud-SDK-Analytics" ,
323- "service_name=watson_vision_combined;service_version=v3;operation_id=deleteClassifier" );
272+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "deleteClassifier" );
273+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
274+ builder .header (header .getKey (), header .getValue ());
275+ }
276+ builder .header ("Accept" , "application/json" );
324277 return createServiceCall (builder .build (), ResponseConverterUtils .getVoid ());
325278 }
326279
@@ -339,8 +292,11 @@ public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierO
339292 RequestBuilder builder = RequestBuilder .get (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
340293 pathParameters ));
341294 builder .query ("version" , versionDate );
342- builder .header ("X-IBMCloud-SDK-Analytics" ,
343- "service_name=watson_vision_combined;service_version=v3;operation_id=getClassifier" );
295+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "getClassifier" );
296+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
297+ builder .header (header .getKey (), header .getValue ());
298+ }
299+ builder .header ("Accept" , "application/json" );
344300 return createServiceCall (builder .build (), ResponseConverterUtils .getObject (Classifier .class ));
345301 }
346302
@@ -354,8 +310,11 @@ public ServiceCall<Classifiers> listClassifiers(ListClassifiersOptions listClass
354310 String [] pathSegments = { "v3/classifiers" };
355311 RequestBuilder builder = RequestBuilder .get (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
356312 builder .query ("version" , versionDate );
357- builder .header ("X-IBMCloud-SDK-Analytics" ,
358- "service_name=watson_vision_combined;service_version=v3;operation_id=listClassifiers" );
313+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "listClassifiers" );
314+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
315+ builder .header (header .getKey (), header .getValue ());
316+ }
317+ builder .header ("Accept" , "application/json" );
359318 if (listClassifiersOptions != null ) {
360319 if (listClassifiersOptions .verbose () != null ) {
361320 builder .query ("verbose" , String .valueOf (listClassifiersOptions .verbose ()));
@@ -399,17 +358,18 @@ public ServiceCall<Classifier> updateClassifier(UpdateClassifierOptions updateCl
399358 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
400359 pathParameters ));
401360 builder .query ("version" , versionDate );
402- builder .header ("X-IBMCloud-SDK-Analytics" ,
403- "service_name=watson_vision_combined;service_version=v3;operation_id=updateClassifier" );
361+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "updateClassifier" );
362+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
363+ builder .header (header .getKey (), header .getValue ());
364+ }
365+ builder .header ("Accept" , "application/json" );
404366 MultipartBody .Builder multipartBuilder = new MultipartBody .Builder ();
405367 multipartBuilder .setType (MultipartBody .FORM );
406368 if (updateClassifierOptions .positiveExamples () != null ) {
407369 for (Map .Entry <String , InputStream > entry : updateClassifierOptions .positiveExamples ().entrySet ()) {
408370 String partName = String .format ("%s_positive_examples" , entry .getKey ());
409- String fileName = updateClassifierOptions .positiveExamplesFilename () != null ? updateClassifierOptions
410- .positiveExamplesFilename ().get (entry .getKey ()) : null ;
411371 RequestBody part = RequestUtils .inputStreamBody (entry .getValue (), "application/octet-stream" );
412- multipartBuilder .addFormDataPart (partName , fileName , part );
372+ multipartBuilder .addFormDataPart (partName , entry . getKey () , part );
413373 }
414374 }
415375 if (updateClassifierOptions .negativeExamples () != null ) {
@@ -438,8 +398,11 @@ public ServiceCall<InputStream> getCoreMlModel(GetCoreMlModelOptions getCoreMlMo
438398 RequestBuilder builder = RequestBuilder .get (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
439399 pathParameters ));
440400 builder .query ("version" , versionDate );
441- builder .header ("X-IBMCloud-SDK-Analytics" ,
442- "service_name=watson_vision_combined;service_version=v3;operation_id=getCoreMlModel" );
401+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "getCoreMlModel" );
402+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
403+ builder .header (header .getKey (), header .getValue ());
404+ }
405+ builder .header ("Accept" , "application/octet-stream" );
443406 return createServiceCall (builder .build (), ResponseConverterUtils .getInputStream ());
444407 }
445408
@@ -461,8 +424,11 @@ public ServiceCall<Void> deleteUserData(DeleteUserDataOptions deleteUserDataOpti
461424 String [] pathSegments = { "v3/user_data" };
462425 RequestBuilder builder = RequestBuilder .delete (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
463426 builder .query ("version" , versionDate );
464- builder .header ("X-IBMCloud-SDK-Analytics" ,
465- "service_name=watson_vision_combined;service_version=v3;operation_id=deleteUserData" );
427+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("watson_vision_combined" , "v3" , "deleteUserData" );
428+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
429+ builder .header (header .getKey (), header .getValue ());
430+ }
431+ builder .header ("Accept" , "application/json" );
466432 builder .query ("customer_id" , deleteUserDataOptions .customerId ());
467433 return createServiceCall (builder .build (), ResponseConverterUtils .getVoid ());
468434 }
0 commit comments