21
21
import java .io .UnsupportedEncodingException ;
22
22
import java .net .URLEncoder ;
23
23
24
+ import java .util .List ;
24
25
import java .util .ArrayList ;
25
26
import java .util .Map ;
26
27
import java .util .HashMap ;
44
45
import com .recombee .api_client .api_requests .ListSeriesItems ;
45
46
import com .recombee .api_client .api_requests .ListGroups ;
46
47
import com .recombee .api_client .api_requests .ListGroupItems ;
48
+ import com .recombee .api_client .api_requests .GetUserValues ;
47
49
import com .recombee .api_client .api_requests .ListUsers ;
50
+ import com .recombee .api_client .api_requests .GetUserPropertyInfo ;
51
+ import com .recombee .api_client .api_requests .ListUserProperties ;
48
52
import com .recombee .api_client .api_requests .ListItemDetailViews ;
49
53
import com .recombee .api_client .api_requests .ListUserDetailViews ;
50
54
import com .recombee .api_client .api_requests .ListItemPurchases ;
@@ -71,6 +75,8 @@ public class RecombeeClient {
71
75
String baseUri = "rapi.recombee.com" ;
72
76
ObjectMapper mapper ;
73
77
78
+ final int BATCH_MAX_SIZE = 10000 ; //Maximal number of requests within one batch request
79
+
74
80
public RecombeeClient (String databaseId , String token ) {
75
81
this .databaseId = databaseId ;
76
82
this .token = token ;
@@ -170,6 +176,26 @@ public User[] send(ListUsers request) throws ApiException {
170
176
return null ;
171
177
}
172
178
179
+ public PropertyInfo send (GetUserPropertyInfo request ) throws ApiException {
180
+ String responseStr = sendRequest (request );
181
+ try {
182
+ return this .mapper .readValue (responseStr , PropertyInfo .class );
183
+ } catch (IOException e ) {
184
+ e .printStackTrace ();
185
+ }
186
+ return null ;
187
+ }
188
+
189
+ public PropertyInfo [] send (ListUserProperties request ) throws ApiException {
190
+ String responseStr = sendRequest (request );
191
+ try {
192
+ return this .mapper .readValue (responseStr , PropertyInfo [].class );
193
+ } catch (IOException e ) {
194
+ e .printStackTrace ();
195
+ }
196
+ return null ;
197
+ }
198
+
173
199
public DetailView [] send (ListItemDetailViews request ) throws ApiException {
174
200
String responseStr = sendRequest (request );
175
201
try {
@@ -273,6 +299,11 @@ public Bookmark[] send(ListUserBookmarks request) throws ApiException {
273
299
/* End of the generated code */
274
300
275
301
public BatchResponse [] send (Batch batchRequest ) throws ApiException {
302
+
303
+ if (batchRequest .getRequests ().size () > this .BATCH_MAX_SIZE ) {
304
+ return sendMultipartBatchRequest (batchRequest );
305
+ }
306
+
276
307
String responseStr = sendRequest (batchRequest );
277
308
278
309
try {
@@ -376,6 +407,20 @@ else if (request instanceof ListUsers)
376
407
parsedResponse = ar ;
377
408
}
378
409
410
+ else if (request instanceof GetUserPropertyInfo )
411
+ {
412
+ Map <String , Object > obj = (Map <String , Object >) parsedResponse ;
413
+ parsedResponse = new PropertyInfo (obj );
414
+ }
415
+
416
+ else if (request instanceof ListUserProperties )
417
+ {
418
+ ArrayList <Map <String , Object >> array = (ArrayList <Map <String , Object >>) parsedResponse ;
419
+ PropertyInfo [] ar = new PropertyInfo [array .size ()];
420
+ for (int j =0 ;j <ar .length ;j ++) ar [j ] = new PropertyInfo (array .get (j ));
421
+ parsedResponse = ar ;
422
+ }
423
+
379
424
else if (request instanceof ListItemDetailViews )
380
425
{
381
426
ArrayList <Map <String , Object >> array = (ArrayList <Map <String , Object >>) parsedResponse ;
@@ -467,7 +512,51 @@ else if (request instanceof ListUserBookmarks)
467
512
}
468
513
return null ;
469
514
}
470
- /* End of the generated code */
515
+
516
+
517
+
518
+ private BatchResponse [] sendMultipartBatchRequest (Batch batchRequest ) throws ApiException {
519
+
520
+ List <List <Request >> requestChunks = getRequestsChunks (batchRequest );
521
+ ArrayList <BatchResponse []> responses = new ArrayList <BatchResponse []>();
522
+
523
+ for (List <Request > rqs : requestChunks )
524
+ responses .add (send (new Batch (rqs )));
525
+
526
+ return concatenateResponses (responses );
527
+ }
528
+
529
+ private List <List <Request >> getRequestsChunks (Batch batchRequest ) {
530
+
531
+ ArrayList <List <Request >> result = new ArrayList <List <Request >>();
532
+ List <Request > requests = batchRequest .getRequests ();
533
+ int fullparts = requests .size () / this .BATCH_MAX_SIZE ;
534
+
535
+ for (int i =0 ;i <fullparts ;i ++)
536
+ result .add (requests .subList (i * this .BATCH_MAX_SIZE , (i +1 ) * this .BATCH_MAX_SIZE ));
537
+
538
+ if (fullparts * this .BATCH_MAX_SIZE < requests .size ())
539
+ result .add (requests .subList (fullparts * this .BATCH_MAX_SIZE , requests .size ()));
540
+
541
+ return result ;
542
+ }
543
+
544
+ private BatchResponse [] concatenateResponses (ArrayList <BatchResponse []> responses )
545
+ {
546
+ int size = 0 , i = 0 ;
547
+
548
+ for (BatchResponse [] rsps : responses ) {
549
+ size += rsps .length ;
550
+ }
551
+
552
+ BatchResponse [] result = new BatchResponse [size ];
553
+
554
+ for (BatchResponse [] rsps : responses ) {
555
+ for (BatchResponse rsp : rsps )
556
+ result [i ++] = rsp ;
557
+ }
558
+ return result ;
559
+ } /* End of the generated code */
471
560
472
561
public Map <String , Object > send (GetItemValues request ) throws ApiException {
473
562
String responseStr = sendRequest (request );
@@ -482,6 +571,21 @@ public Map<String, Object> send(GetItemValues request) throws ApiException {
482
571
return null ;
483
572
}
484
573
574
+
575
+ public Map <String , Object > send (GetUserValues request ) throws ApiException {
576
+ String responseStr = sendRequest (request );
577
+
578
+ TypeReference <HashMap <String ,Object >> typeRef
579
+ = new TypeReference <HashMap <String ,Object >>() {};
580
+ try {
581
+ return this .mapper .readValue (responseStr , typeRef );
582
+ } catch (IOException e ) {
583
+ e .printStackTrace ();
584
+ }
585
+ return null ;
586
+ }
587
+
588
+
485
589
public Recommendation [] send (UserBasedRecommendation request ) throws ApiException {
486
590
return sendRecomm (request );
487
591
}
0 commit comments