28
28
import java .util .Map ;
29
29
import java .util .HashMap ;
30
30
import java .util .concurrent .TimeUnit ;
31
-
32
31
import com .recombee .api_client .api_requests .Request ;
33
32
import com .recombee .api_client .exceptions .ApiException ;
34
33
import com .recombee .api_client .exceptions .ApiTimeoutException ;
35
34
import com .recombee .api_client .exceptions .ResponseException ;
36
35
import com .recombee .api_client .util .NetworkApplicationProtocol ;
36
+ import com .recombee .api_client .util .Region ;
37
+ import com .recombee .api_client .util .HTTPMethod ;
37
38
38
39
import com .recombee .api_client .bindings .Recommendation ;
39
40
import com .recombee .api_client .api_requests .Batch ;
44
45
import com .recombee .api_client .api_requests .ListItems ;
45
46
import com .recombee .api_client .api_requests .GetItemPropertyInfo ;
46
47
import 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 ;
47
50
import com .recombee .api_client .api_requests .ListSeries ;
48
51
import com .recombee .api_client .api_requests .ListSeriesItems ;
49
52
import com .recombee .api_client .api_requests .ListGroups ;
69
72
import com .recombee .api_client .api_requests .RecommendNextItems ;
70
73
import com .recombee .api_client .api_requests .RecommendUsersToUser ;
71
74
import 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 ;
74
75
import com .recombee .api_client .api_requests .SearchItems ;
75
76
import com .recombee .api_client .api_requests .AddSearchSynonym ;
76
77
import com .recombee .api_client .api_requests .ListSearchSynonyms ;
@@ -90,7 +91,7 @@ public class RecombeeClient {
90
91
91
92
final int BATCH_MAX_SIZE = 10000 ; //Maximal number of requests within one batch request
92
93
93
- final String USER_AGENT = "recombee-java-api-client/3.2.1 " ;
94
+ final String USER_AGENT = "recombee-java-api-client/4.0.0 " ;
94
95
95
96
private final OkHttpClient httpClient = new OkHttpClient ();
96
97
@@ -114,11 +115,37 @@ public RecombeeClient setDefaultProtocol(NetworkApplicationProtocol defaultProto
114
115
this .defaultProtocol = defaultProtocol ;
115
116
return this ;
116
117
}
117
- public RecombeeClient setBaseUri ( String baseUri )
118
- {
118
+
119
+ public RecombeeClient setBaseUri ( String baseUri ) {
119
120
this .baseUri = baseUri ;
120
121
return this ;
121
122
}
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
+ }
122
149
/* Start of the generated code */
123
150
public PropertyInfo send (GetItemPropertyInfo request ) throws ApiException {
124
151
String responseStr = sendRequest (request );
@@ -140,6 +167,26 @@ public PropertyInfo[] send(ListItemProperties request) throws ApiException {
140
167
return null ;
141
168
}
142
169
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
+
143
190
public Series [] send (ListSeries request ) throws ApiException {
144
191
String responseStr = sendRequest (request );
145
192
try {
@@ -427,28 +474,7 @@ public BatchResponse[] send(Batch batchRequest) throws ApiException {
427
474
}
428
475
else
429
476
{
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 )
452
478
{
453
479
boolean returnProperties = ((ListItems ) request ).getReturnProperties ();
454
480
if (returnProperties )
@@ -504,6 +530,14 @@ else if (request instanceof SearchItems)
504
530
{
505
531
parsedResponse = mapper .convertValue (parsedResponse , SearchResponse .class );
506
532
}
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
+ }
507
541
/* Start of the generated code */
508
542
else if (request instanceof GetItemPropertyInfo )
509
543
{
@@ -745,41 +779,6 @@ public Map<String, Object> send(GetUserValues request) throws ApiException {
745
779
return null ;
746
780
}
747
781
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
-
783
782
public Item [] send (ListItems request ) throws ApiException {
784
783
String responseStr = sendRequest (request );
785
784
@@ -832,7 +831,6 @@ public User[] send(ListUsers request) throws ApiException {
832
831
}
833
832
return null ;
834
833
}
835
-
836
834
public String send (Request request ) throws ApiException {
837
835
return sendRequest (request );
838
836
}
@@ -854,21 +852,24 @@ private String sendRequest(Request request) throws ApiException {
854
852
.addHeader ("User-Agent" , this .USER_AGENT );
855
853
856
854
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
+ }
869
871
}
870
872
871
-
872
873
try {
873
874
Response response = tempClient .newCall (httpRequestBuilder .build ()).execute ();
874
875
checkErrors (response , request );
@@ -901,12 +902,6 @@ private String signUrl(String url) {
901
902
return null ;
902
903
}
903
904
904
- private String processRequestUri (Request request ) {
905
- String uri = "/" + this .databaseId + request .getPath ();
906
- uri = appendQueryParameters (uri , request );
907
- return uri ;
908
- }
909
-
910
905
private String appendQueryParameters (String uri , Request request ) {
911
906
for (Map .Entry <String , Object > pair : request .getQueryParameters ().entrySet ()) {
912
907
uri += uri .contains ("?" ) ? "&" : "?" ;
@@ -924,26 +919,11 @@ private String formatQueryParameterValue(Object val) {
924
919
}
925
920
}
926
921
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 ) {
941
923
try {
942
924
String json = this .mapper .writeValueAsString (req .getBodyParameters ());
943
925
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 );
947
927
} catch (JsonProcessingException e ) {
948
928
e .printStackTrace ();
949
929
}
0 commit comments