2828import java .util .Map ;
2929import java .util .HashMap ;
3030import java .util .concurrent .TimeUnit ;
31-
3231import com .recombee .api_client .api_requests .Request ;
3332import com .recombee .api_client .exceptions .ApiException ;
3433import com .recombee .api_client .exceptions .ApiTimeoutException ;
3534import com .recombee .api_client .exceptions .ResponseException ;
3635import com .recombee .api_client .util .NetworkApplicationProtocol ;
36+ import com .recombee .api_client .util .Region ;
37+ import com .recombee .api_client .util .HTTPMethod ;
3738
3839import com .recombee .api_client .bindings .Recommendation ;
3940import com .recombee .api_client .api_requests .Batch ;
4445import com .recombee .api_client .api_requests .ListItems ;
4546import com .recombee .api_client .api_requests .GetItemPropertyInfo ;
4647import com .recombee .api_client .api_requests .ListItemProperties ;
48+ import com .recombee .api_client .api_requests .UpdateMoreItems ;
49+ import com .recombee .api_client .api_requests .DeleteMoreItems ;
4750import com .recombee .api_client .api_requests .ListSeries ;
4851import com .recombee .api_client .api_requests .ListSeriesItems ;
4952import com .recombee .api_client .api_requests .ListGroups ;
6972import com .recombee .api_client .api_requests .RecommendNextItems ;
7073import com .recombee .api_client .api_requests .RecommendUsersToUser ;
7174import com .recombee .api_client .api_requests .RecommendUsersToItem ;
72- import com .recombee .api_client .api_requests .UserBasedRecommendation ;
73- import com .recombee .api_client .api_requests .ItemBasedRecommendation ;
7475import com .recombee .api_client .api_requests .SearchItems ;
7576import com .recombee .api_client .api_requests .AddSearchSynonym ;
7677import com .recombee .api_client .api_requests .ListSearchSynonyms ;
@@ -90,7 +91,7 @@ public class RecombeeClient {
9091
9192 final int BATCH_MAX_SIZE = 10000 ; //Maximal number of requests within one batch request
9293
93- final String USER_AGENT = "recombee-java-api-client/3.2.1 " ;
94+ final String USER_AGENT = "recombee-java-api-client/4.0.0 " ;
9495
9596 private final OkHttpClient httpClient = new OkHttpClient ();
9697
@@ -114,11 +115,37 @@ public RecombeeClient setDefaultProtocol(NetworkApplicationProtocol defaultProto
114115 this .defaultProtocol = defaultProtocol ;
115116 return this ;
116117 }
117- public RecombeeClient setBaseUri ( String baseUri )
118- {
118+
119+ public RecombeeClient setBaseUri ( String baseUri ) {
119120 this .baseUri = baseUri ;
120121 return this ;
121122 }
123+
124+ public RecombeeClient setRegion (Region region ) {
125+ switch (region ) {
126+ case AP_SE :
127+ this .baseUri = "rapi-ap-se.recombee.com" ;
128+ break ;
129+ case CA_EAST :
130+ this .baseUri ="rapi-ca-east.recombee.com" ;
131+ break ;
132+ case EU_WEST :
133+ this .baseUri = "rapi-eu-west.recombee.com" ;
134+ break ;
135+ case US_WEST :
136+ this .baseUri = "rapi-us-west.recombee.com" ;
137+ break ;
138+ default :
139+ throw new IllegalArgumentException ("Unknown region given" );
140+ }
141+ return this ;
142+ }
143+
144+ private String processRequestUri (Request request ) {
145+ String uri = "/" + this .databaseId + request .getPath ();
146+ uri = appendQueryParameters (uri , request );
147+ return uri ;
148+ }
122149 /* Start of the generated code */
123150 public PropertyInfo send (GetItemPropertyInfo request ) throws ApiException {
124151 String responseStr = sendRequest (request );
@@ -140,6 +167,26 @@ public PropertyInfo[] send(ListItemProperties request) throws ApiException {
140167 return null ;
141168 }
142169
170+ public UpdateMoreItemsResponse send (UpdateMoreItems request ) throws ApiException {
171+ String responseStr = sendRequest (request );
172+ try {
173+ return this .mapper .readValue (responseStr , UpdateMoreItemsResponse .class );
174+ } catch (IOException e ) {
175+ e .printStackTrace ();
176+ }
177+ return null ;
178+ }
179+
180+ public DeleteMoreItemsResponse send (DeleteMoreItems request ) throws ApiException {
181+ String responseStr = sendRequest (request );
182+ try {
183+ return this .mapper .readValue (responseStr , DeleteMoreItemsResponse .class );
184+ } catch (IOException e ) {
185+ e .printStackTrace ();
186+ }
187+ return null ;
188+ }
189+
143190 public Series [] send (ListSeries request ) throws ApiException {
144191 String responseStr = sendRequest (request );
145192 try {
@@ -427,28 +474,7 @@ public BatchResponse[] send(Batch batchRequest) throws ApiException {
427474 }
428475 else
429476 {
430- if ((request instanceof ItemBasedRecommendation ) || (request instanceof UserBasedRecommendation ))
431- {
432- boolean returnProperties = false ;
433- if (request instanceof ItemBasedRecommendation ) returnProperties = ((ItemBasedRecommendation ) request ).getReturnProperties ();
434- if (request instanceof UserBasedRecommendation ) returnProperties = ((UserBasedRecommendation ) request ).getReturnProperties ();
435-
436- if (returnProperties )
437- {
438- ArrayList <Map <String , Object >> array = (ArrayList <Map <String , Object >>) parsedResponse ;
439- Recommendation [] ar = new Recommendation [array .size ()];
440- for (int j =0 ;j <ar .length ;j ++) ar [j ] = new Recommendation ((String )array .get (j ).get ("itemId" ), array .get (j ));
441- parsedResponse = ar ;
442- }
443- else
444- {
445- ArrayList <String > array = (ArrayList <String >) parsedResponse ;
446- Recommendation [] ar = new Recommendation [array .size ()];
447- for (int j =0 ;j <ar .length ;j ++) ar [j ] = new Recommendation (array .get (j ));
448- parsedResponse = ar ;
449- }
450- }
451- else if (request instanceof ListItems )
477+ if (request instanceof ListItems )
452478 {
453479 boolean returnProperties = ((ListItems ) request ).getReturnProperties ();
454480 if (returnProperties )
@@ -504,6 +530,14 @@ else if (request instanceof SearchItems)
504530 {
505531 parsedResponse = mapper .convertValue (parsedResponse , SearchResponse .class );
506532 }
533+ else if (request instanceof UpdateMoreItems )
534+ {
535+ parsedResponse = mapper .convertValue (parsedResponse , UpdateMoreItemsResponse .class );
536+ }
537+ else if (request instanceof DeleteMoreItems )
538+ {
539+ parsedResponse = mapper .convertValue (parsedResponse , DeleteMoreItemsResponse .class );
540+ }
507541 /* Start of the generated code */
508542 else if (request instanceof GetItemPropertyInfo )
509543 {
@@ -745,41 +779,6 @@ public Map<String, Object> send(GetUserValues request) throws ApiException {
745779 return null ;
746780 }
747781
748-
749- public Recommendation [] send (UserBasedRecommendation request ) throws ApiException {
750- return sendDeprecatedRecomm (request );
751- }
752-
753- public Recommendation [] send (ItemBasedRecommendation request ) throws ApiException {
754- return sendDeprecatedRecomm (request );
755- }
756-
757- protected Recommendation [] sendDeprecatedRecomm (Request request ) throws ApiException {
758- String responseStr = sendRequest (request );
759-
760- try {
761- this .mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , true ); // Check exact match
762- return this .mapper .readValue (responseStr , Recommendation [].class );
763- } catch (IOException e ) {
764- //might have failed because it returned also the item properties
765- TypeReference <HashMap <String ,Object >[]> typeRef
766- = new TypeReference <HashMap <String ,Object >[]>() {};
767- try {
768- Map <String , Object >[] valsArray = this .mapper .readValue (responseStr , typeRef );
769- Recommendation [] recomms = new Recommendation [valsArray .length ];
770- for (int i =0 ;i <valsArray .length ;i ++)
771- recomms [i ] = new Recommendation ((String )valsArray [i ].get ("itemId" ), valsArray [i ]);
772- return recomms ;
773- } catch (IOException e2 ) {
774- e2 .printStackTrace ();
775- }
776- }
777- finally {
778- this .mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
779- }
780- return null ;
781- }
782-
783782 public Item [] send (ListItems request ) throws ApiException {
784783 String responseStr = sendRequest (request );
785784
@@ -832,7 +831,6 @@ public User[] send(ListUsers request) throws ApiException {
832831 }
833832 return null ;
834833 }
835-
836834 public String send (Request request ) throws ApiException {
837835 return sendRequest (request );
838836 }
@@ -854,21 +852,24 @@ private String sendRequest(Request request) throws ApiException {
854852 .addHeader ("User-Agent" , this .USER_AGENT );
855853
856854
857- switch (request .getHTTPMethod ()) {
858- case GET :
859- break ;
860- case POST :
861- httpRequestBuilder = post (httpRequestBuilder , request );
862- break ;
863- case PUT :
864- httpRequestBuilder = put (httpRequestBuilder , request );
865- break ;
866- case DELETE :
867- httpRequestBuilder .delete ();
868- break ;
855+ if (request .getHTTPMethod () != HTTPMethod .GET ) {
856+ httpRequestBuilder .addHeader ("Content-Type" , "application/json; charset=utf-8" );
857+ RequestBody body = getBody (request );
858+ if (body != null ){
859+ switch (request .getHTTPMethod ()) {
860+ case POST :
861+ httpRequestBuilder .post (body );
862+ break ;
863+ case PUT :
864+ httpRequestBuilder .put (body );
865+ break ;
866+ case DELETE :
867+ httpRequestBuilder .delete (body );
868+ break ;
869+ }
870+ }
869871 }
870872
871-
872873 try {
873874 Response response = tempClient .newCall (httpRequestBuilder .build ()).execute ();
874875 checkErrors (response , request );
@@ -901,12 +902,6 @@ private String signUrl(String url) {
901902 return null ;
902903 }
903904
904- private String processRequestUri (Request request ) {
905- String uri = "/" + this .databaseId + request .getPath ();
906- uri = appendQueryParameters (uri , request );
907- return uri ;
908- }
909-
910905 private String appendQueryParameters (String uri , Request request ) {
911906 for (Map .Entry <String , Object > pair : request .getQueryParameters ().entrySet ()) {
912907 uri += uri .contains ("?" ) ? "&" : "?" ;
@@ -924,26 +919,11 @@ private String formatQueryParameterValue(Object val) {
924919 }
925920 }
926921
927- private okhttp3 .Request .Builder put (okhttp3 .Request .Builder reqBuilder , Request req ) {
928- try {
929- String json = this .mapper .writeValueAsString (req .getBodyParameters ());
930- final MediaType JSON = MediaType .parse ("application/json; charset=utf-8" );
931- reqBuilder .put (RequestBody .create (JSON , json ))
932- .addHeader ("Content-Type" , "application/json; charset=utf-8" );
933- return reqBuilder ;
934- } catch (JsonProcessingException e ) {
935- e .printStackTrace ();
936- }
937- return null ;
938- }
939-
940- private okhttp3 .Request .Builder post (okhttp3 .Request .Builder reqBuilder , Request req ) {
922+ private RequestBody getBody (Request req ) {
941923 try {
942924 String json = this .mapper .writeValueAsString (req .getBodyParameters ());
943925 final MediaType JSON = MediaType .parse ("application/json; charset=utf-8" );
944- reqBuilder .post (RequestBody .create (JSON , json ))
945- .addHeader ("Content-Type" , "application/json; charset=utf-8" );
946- return reqBuilder ;
926+ return RequestBody .create (JSON , json );
947927 } catch (JsonProcessingException e ) {
948928 e .printStackTrace ();
949929 }
0 commit comments