2121import java .io .UnsupportedEncodingException ;
2222import java .net .URLEncoder ;
2323
24+ import java .util .List ;
2425import java .util .ArrayList ;
2526import java .util .Map ;
2627import java .util .HashMap ;
4445import com .recombee .api_client .api_requests .ListSeriesItems ;
4546import com .recombee .api_client .api_requests .ListGroups ;
4647import com .recombee .api_client .api_requests .ListGroupItems ;
48+ import com .recombee .api_client .api_requests .GetUserValues ;
4749import 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 ;
4852import com .recombee .api_client .api_requests .ListItemDetailViews ;
4953import com .recombee .api_client .api_requests .ListUserDetailViews ;
5054import com .recombee .api_client .api_requests .ListItemPurchases ;
@@ -71,6 +75,8 @@ public class RecombeeClient {
7175 String baseUri = "rapi.recombee.com" ;
7276 ObjectMapper mapper ;
7377
78+ final int BATCH_MAX_SIZE = 10000 ; //Maximal number of requests within one batch request
79+
7480 public RecombeeClient (String databaseId , String token ) {
7581 this .databaseId = databaseId ;
7682 this .token = token ;
@@ -170,6 +176,26 @@ public User[] send(ListUsers request) throws ApiException {
170176 return null ;
171177 }
172178
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+
173199 public DetailView [] send (ListItemDetailViews request ) throws ApiException {
174200 String responseStr = sendRequest (request );
175201 try {
@@ -273,6 +299,11 @@ public Bookmark[] send(ListUserBookmarks request) throws ApiException {
273299 /* End of the generated code */
274300
275301 public BatchResponse [] send (Batch batchRequest ) throws ApiException {
302+
303+ if (batchRequest .getRequests ().size () > this .BATCH_MAX_SIZE ) {
304+ return sendMultipartBatchRequest (batchRequest );
305+ }
306+
276307 String responseStr = sendRequest (batchRequest );
277308
278309 try {
@@ -376,6 +407,20 @@ else if (request instanceof ListUsers)
376407 parsedResponse = ar ;
377408 }
378409
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+
379424 else if (request instanceof ListItemDetailViews )
380425 {
381426 ArrayList <Map <String , Object >> array = (ArrayList <Map <String , Object >>) parsedResponse ;
@@ -467,7 +512,51 @@ else if (request instanceof ListUserBookmarks)
467512 }
468513 return null ;
469514 }
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 */
471560
472561 public Map <String , Object > send (GetItemValues request ) throws ApiException {
473562 String responseStr = sendRequest (request );
@@ -482,6 +571,21 @@ public Map<String, Object> send(GetItemValues request) throws ApiException {
482571 return null ;
483572 }
484573
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+
485589 public Recommendation [] send (UserBasedRecommendation request ) throws ApiException {
486590 return sendRecomm (request );
487591 }
0 commit comments