@@ -77,6 +77,8 @@ public class RecombeeClient {
77
77
78
78
final int BATCH_MAX_SIZE = 10000 ; //Maximal number of requests within one batch request
79
79
80
+ final String USER_AGENT = "recombee-java-api-client/1.4.0" ;
81
+
80
82
public RecombeeClient (String databaseId , String token ) {
81
83
this .databaseId = databaseId ;
82
84
this .token = token ;
@@ -96,16 +98,6 @@ public void setDefaultProtocol(NetworkApplicationProtocol defaultProtocol) {
96
98
this .defaultProtocol = defaultProtocol ;
97
99
}
98
100
/* Start of the generated code */
99
- public Item [] send (ListItems request ) throws ApiException {
100
- String responseStr = sendRequest (request );
101
- try {
102
- return this .mapper .readValue (responseStr , Item [].class );
103
- } catch (IOException e ) {
104
- e .printStackTrace ();
105
- }
106
- return null ;
107
- }
108
-
109
101
public PropertyInfo send (GetItemPropertyInfo request ) throws ApiException {
110
102
String responseStr = sendRequest (request );
111
103
try {
@@ -166,16 +158,6 @@ public GroupItem[] send(ListGroupItems request) throws ApiException {
166
158
return null ;
167
159
}
168
160
169
- public User [] send (ListUsers request ) throws ApiException {
170
- String responseStr = sendRequest (request );
171
- try {
172
- return this .mapper .readValue (responseStr , User [].class );
173
- } catch (IOException e ) {
174
- e .printStackTrace ();
175
- }
176
- return null ;
177
- }
178
-
179
161
public PropertyInfo send (GetUserPropertyInfo request ) throws ApiException {
180
162
String responseStr = sendRequest (request );
181
163
try {
@@ -344,15 +326,43 @@ public BatchResponse[] send(Batch batchRequest) throws ApiException {
344
326
parsedResponse = ar ;
345
327
}
346
328
}
347
- /* Start of the generated code */
348
329
else if (request instanceof ListItems )
349
330
{
350
- ArrayList <String > array = (ArrayList <String >) parsedResponse ;
351
- Item [] ar = new Item [array .size ()];
352
- for (int j =0 ;j <ar .length ;j ++) ar [j ] = new Item (array .get (j ));
353
- parsedResponse = ar ;
331
+ boolean returnProperties = ((ListItems ) request ).getReturnProperties ();
332
+ if (returnProperties )
333
+ {
334
+ ArrayList <Map <String , Object >> array = (ArrayList <Map <String , Object >>) parsedResponse ;
335
+ Item [] ar = new Item [array .size ()];
336
+ for (int j =0 ;j <ar .length ;j ++) ar [j ] = new Item ((String )array .get (j ).get ("itemId" ), array .get (j ));
337
+ parsedResponse = ar ;
338
+ }
339
+ else
340
+ {
341
+ ArrayList <String > array = (ArrayList <String >) parsedResponse ;
342
+ Item [] ar = new Item [array .size ()];
343
+ for (int j =0 ;j <ar .length ;j ++) ar [j ] = new Item (array .get (j ));
344
+ parsedResponse = ar ;
345
+ }
354
346
}
355
-
347
+ else if (request instanceof ListUsers )
348
+ {
349
+ boolean returnProperties = ((ListUsers ) request ).getReturnProperties ();
350
+ if (returnProperties )
351
+ {
352
+ ArrayList <Map <String , Object >> array = (ArrayList <Map <String , Object >>) parsedResponse ;
353
+ User [] ar = new User [array .size ()];
354
+ for (int j =0 ;j <ar .length ;j ++) ar [j ] = new User ((String )array .get (j ).get ("userId" ), array .get (j ));
355
+ parsedResponse = ar ;
356
+ }
357
+ else
358
+ {
359
+ ArrayList <String > array = (ArrayList <String >) parsedResponse ;
360
+ User [] ar = new User [array .size ()];
361
+ for (int j =0 ;j <ar .length ;j ++) ar [j ] = new User (array .get (j ));
362
+ parsedResponse = ar ;
363
+ }
364
+ }
365
+ /* Start of the generated code */
356
366
else if (request instanceof GetItemPropertyInfo )
357
367
{
358
368
Map <String , Object > obj = (Map <String , Object >) parsedResponse ;
@@ -399,14 +409,6 @@ else if (request instanceof ListGroupItems)
399
409
parsedResponse = ar ;
400
410
}
401
411
402
- else if (request instanceof ListUsers )
403
- {
404
- ArrayList <String > array = (ArrayList <String >) parsedResponse ;
405
- User [] ar = new User [array .size ()];
406
- for (int j =0 ;j <ar .length ;j ++) ar [j ] = new User (array .get (j ));
407
- parsedResponse = ar ;
408
- }
409
-
410
412
else if (request instanceof GetUserPropertyInfo )
411
413
{
412
414
Map <String , Object > obj = (Map <String , Object >) parsedResponse ;
@@ -616,6 +618,50 @@ protected Recommendation[] sendRecomm(Request request) throws ApiException {
616
618
return null ;
617
619
}
618
620
621
+ public Item [] send (ListItems request ) throws ApiException {
622
+ String responseStr = sendRequest (request );
623
+
624
+ try {
625
+ return this .mapper .readValue (responseStr , Item [].class );
626
+ } catch (IOException e ) {
627
+ //might have failed because it returned also the item properties
628
+ TypeReference <HashMap <String ,Object >[]> typeRef
629
+ = new TypeReference <HashMap <String ,Object >[]>() {};
630
+ try {
631
+ Map <String , Object >[] valsArray = this .mapper .readValue (responseStr , typeRef );
632
+ Item [] recomms = new Item [valsArray .length ];
633
+ for (int i =0 ;i <valsArray .length ;i ++)
634
+ recomms [i ] = new Item ((String )valsArray [i ].get ("itemId" ), valsArray [i ]);
635
+ return recomms ;
636
+ } catch (IOException e2 ) {
637
+ e2 .printStackTrace ();
638
+ }
639
+ }
640
+ return null ;
641
+ }
642
+
643
+
644
+ public User [] send (ListUsers request ) throws ApiException {
645
+ String responseStr = sendRequest (request );
646
+
647
+ try {
648
+ return this .mapper .readValue (responseStr , User [].class );
649
+ } catch (IOException e ) {
650
+ //might have failed because it returned also the user properties
651
+ TypeReference <HashMap <String ,Object >[]> typeRef
652
+ = new TypeReference <HashMap <String ,Object >[]>() {};
653
+ try {
654
+ Map <String , Object >[] valsArray = this .mapper .readValue (responseStr , typeRef );
655
+ User [] recomms = new User [valsArray .length ];
656
+ for (int i =0 ;i <valsArray .length ;i ++)
657
+ recomms [i ] = new User ((String )valsArray [i ].get ("userId" ), valsArray [i ]);
658
+ return recomms ;
659
+ } catch (IOException e2 ) {
660
+ e2 .printStackTrace ();
661
+ }
662
+ }
663
+ return null ;
664
+ }
619
665
620
666
public String send (Request request ) throws ApiException {
621
667
return sendRequest (request );
@@ -636,7 +682,7 @@ protected String sendRequest(Request request) throws ApiException {
636
682
httpRequest = post (uri , request );
637
683
break ;
638
684
case PUT :
639
- httpRequest = put (uri );
685
+ httpRequest = put (uri , request );
640
686
break ;
641
687
case DELETE :
642
688
httpRequest = delete (uri );
@@ -700,21 +746,30 @@ private String formatQueryParameterValue(Object val) {
700
746
}
701
747
702
748
private HttpRequest get (String url ) {
703
- return Unirest .get (url );
749
+ return Unirest .get (url ). header ( "User-Agent" , this . USER_AGENT ) ;
704
750
}
705
751
706
- private HttpRequest put (String url ) {
707
- return Unirest .put (url );
752
+ private HttpRequest put (String url , Request req ) {
753
+ try {
754
+ String json = this .mapper .writeValueAsString (req .getBodyParameters ());
755
+ return Unirest .put (url ).header ("Content-Type" , "application/json" ).
756
+ header ("User-Agent" , this .USER_AGENT ).
757
+ body (json .getBytes ()).getHttpRequest ();
758
+ } catch (JsonProcessingException e ) {
759
+ e .printStackTrace ();
760
+ }
761
+ return null ;
708
762
}
709
763
710
764
private HttpRequest delete (String url ) {
711
- return Unirest .delete (url );
765
+ return Unirest .delete (url ). header ( "User-Agent" , this . USER_AGENT ) ;
712
766
}
713
767
714
768
private HttpRequest post (String url , Request req ) {
715
769
try {
716
770
String json = this .mapper .writeValueAsString (req .getBodyParameters ());
717
771
return Unirest .post (url ).header ("Content-Type" , "application/json" ).
772
+ header ("User-Agent" , this .USER_AGENT ).
718
773
body (json .getBytes ()).getHttpRequest ();
719
774
} catch (JsonProcessingException e ) {
720
775
e .printStackTrace ();
@@ -727,4 +782,4 @@ private void checkErrors(HttpResponse<String> response, Request request) throws
727
782
throw new ResponseException (request , response .getStatus (), response .getBody ());
728
783
729
784
}
730
- }
785
+ }
0 commit comments