@@ -77,6 +77,8 @@ public class RecombeeClient {
7777
7878 final int BATCH_MAX_SIZE = 10000 ; //Maximal number of requests within one batch request
7979
80+ final String USER_AGENT = "recombee-java-api-client/1.4.0" ;
81+
8082 public RecombeeClient (String databaseId , String token ) {
8183 this .databaseId = databaseId ;
8284 this .token = token ;
@@ -96,16 +98,6 @@ public void setDefaultProtocol(NetworkApplicationProtocol defaultProtocol) {
9698 this .defaultProtocol = defaultProtocol ;
9799 }
98100 /* 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-
109101 public PropertyInfo send (GetItemPropertyInfo request ) throws ApiException {
110102 String responseStr = sendRequest (request );
111103 try {
@@ -166,16 +158,6 @@ public GroupItem[] send(ListGroupItems request) throws ApiException {
166158 return null ;
167159 }
168160
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-
179161 public PropertyInfo send (GetUserPropertyInfo request ) throws ApiException {
180162 String responseStr = sendRequest (request );
181163 try {
@@ -344,15 +326,43 @@ public BatchResponse[] send(Batch batchRequest) throws ApiException {
344326 parsedResponse = ar ;
345327 }
346328 }
347- /* Start of the generated code */
348329 else if (request instanceof ListItems )
349330 {
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+ }
354346 }
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 */
356366 else if (request instanceof GetItemPropertyInfo )
357367 {
358368 Map <String , Object > obj = (Map <String , Object >) parsedResponse ;
@@ -399,14 +409,6 @@ else if (request instanceof ListGroupItems)
399409 parsedResponse = ar ;
400410 }
401411
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-
410412 else if (request instanceof GetUserPropertyInfo )
411413 {
412414 Map <String , Object > obj = (Map <String , Object >) parsedResponse ;
@@ -616,6 +618,50 @@ protected Recommendation[] sendRecomm(Request request) throws ApiException {
616618 return null ;
617619 }
618620
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+ }
619665
620666 public String send (Request request ) throws ApiException {
621667 return sendRequest (request );
@@ -636,7 +682,7 @@ protected String sendRequest(Request request) throws ApiException {
636682 httpRequest = post (uri , request );
637683 break ;
638684 case PUT :
639- httpRequest = put (uri );
685+ httpRequest = put (uri , request );
640686 break ;
641687 case DELETE :
642688 httpRequest = delete (uri );
@@ -700,21 +746,30 @@ private String formatQueryParameterValue(Object val) {
700746 }
701747
702748 private HttpRequest get (String url ) {
703- return Unirest .get (url );
749+ return Unirest .get (url ). header ( "User-Agent" , this . USER_AGENT ) ;
704750 }
705751
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 ;
708762 }
709763
710764 private HttpRequest delete (String url ) {
711- return Unirest .delete (url );
765+ return Unirest .delete (url ). header ( "User-Agent" , this . USER_AGENT ) ;
712766 }
713767
714768 private HttpRequest post (String url , Request req ) {
715769 try {
716770 String json = this .mapper .writeValueAsString (req .getBodyParameters ());
717771 return Unirest .post (url ).header ("Content-Type" , "application/json" ).
772+ header ("User-Agent" , this .USER_AGENT ).
718773 body (json .getBytes ()).getHttpRequest ();
719774 } catch (JsonProcessingException e ) {
720775 e .printStackTrace ();
@@ -727,4 +782,4 @@ private void checkErrors(HttpResponse<String> response, Request request) throws
727782 throw new ResponseException (request , response .getStatus (), response .getBody ());
728783
729784 }
730- }
785+ }
0 commit comments