Skip to content

Commit 918ba46

Browse files
committed
Support setting the region of your DB. Added Update / Delete More Items endpoints.
Removed deprecated endpoints Item Based Recommendation and User Based Recommendation.
1 parent 7eb53b0 commit 918ba46

22 files changed

+531
-1155
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The client is available in the [Maven Central Repository](https://mvnrepository.
1313
<dependency>
1414
<groupId>com.recombee</groupId>
1515
<artifactId>api-client</artifactId>
16-
<version>3.2.1</version>
16+
<version>4.0.0</version>
1717
</dependency>
1818
```
1919

@@ -27,6 +27,7 @@ Examples are located in [src/examples](https://github.com/Recombee/java-api-clie
2727
package com.recombee.api_client.examples;
2828

2929
import com.recombee.api_client.RecombeeClient;
30+
import com.recombee.api_client.util.Region;
3031
import com.recombee.api_client.api_requests.*;
3132
import com.recombee.api_client.bindings.RecommendationResponse;
3233
import com.recombee.api_client.bindings.Recommendation;
@@ -38,7 +39,7 @@ import java.util.Random;
3839
public class BasicExample {
3940
public static void main(String[] args) {
4041

41-
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--");
42+
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.US_WEST);
4243
try {
4344

4445
final int NUM = 100;
@@ -83,6 +84,7 @@ public class BasicExample {
8384
package com.recombee.api_client.examples;
8485

8586
import com.recombee.api_client.RecombeeClient;
87+
import com.recombee.api_client.util.Region;
8688
import com.recombee.api_client.api_requests.*;
8789
import com.recombee.api_client.bindings.RecommendationResponse;
8890
import com.recombee.api_client.bindings.Recommendation;
@@ -96,7 +98,7 @@ import java.util.Random;
9698
public class ItemPropertiesExample {
9799
public static void main(String[] args) {
98100

99-
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--");
101+
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.AP_SE);
100102

101103
try {
102104
client.send(new ResetDatabase()); // Clear everything from the database

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.recombee</groupId>
88
<artifactId>api-client</artifactId>
9-
<version>3.2.1</version>
9+
<version>4.0.0</version>
1010
<name>Recombee API Client</name>
1111
<description>A client library for easy use of the Recombee recommendation API</description>
1212
<url>http://recombee.com</url>
@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>com.squareup.okhttp3</groupId>
4242
<artifactId>okhttp</artifactId>
43-
<version>3.9.1</version>
43+
<version>4.9.3</version>
4444
</dependency>
4545
<dependency>
4646
<groupId>com.fasterxml.jackson.core</groupId>

src/main/java/com/recombee/api_client/RecombeeClient.java

Lines changed: 80 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
import java.util.Map;
2929
import java.util.HashMap;
3030
import java.util.concurrent.TimeUnit;
31-
3231
import com.recombee.api_client.api_requests.Request;
3332
import com.recombee.api_client.exceptions.ApiException;
3433
import com.recombee.api_client.exceptions.ApiTimeoutException;
3534
import com.recombee.api_client.exceptions.ResponseException;
3635
import com.recombee.api_client.util.NetworkApplicationProtocol;
36+
import com.recombee.api_client.util.Region;
37+
import com.recombee.api_client.util.HTTPMethod;
3738

3839
import com.recombee.api_client.bindings.Recommendation;
3940
import com.recombee.api_client.api_requests.Batch;
@@ -44,6 +45,8 @@
4445
import com.recombee.api_client.api_requests.ListItems;
4546
import com.recombee.api_client.api_requests.GetItemPropertyInfo;
4647
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;
4750
import com.recombee.api_client.api_requests.ListSeries;
4851
import com.recombee.api_client.api_requests.ListSeriesItems;
4952
import com.recombee.api_client.api_requests.ListGroups;
@@ -69,8 +72,6 @@
6972
import com.recombee.api_client.api_requests.RecommendNextItems;
7073
import com.recombee.api_client.api_requests.RecommendUsersToUser;
7174
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;
7475
import com.recombee.api_client.api_requests.SearchItems;
7576
import com.recombee.api_client.api_requests.AddSearchSynonym;
7677
import com.recombee.api_client.api_requests.ListSearchSynonyms;
@@ -90,7 +91,7 @@ public class RecombeeClient {
9091

9192
final int BATCH_MAX_SIZE = 10000; //Maximal number of requests within one batch request
9293

93-
final String USER_AGENT = "recombee-java-api-client/3.2.1";
94+
final String USER_AGENT = "recombee-java-api-client/4.0.0";
9495

9596
private final OkHttpClient httpClient = new OkHttpClient();
9697

@@ -114,11 +115,37 @@ public RecombeeClient setDefaultProtocol(NetworkApplicationProtocol defaultProto
114115
this.defaultProtocol = defaultProtocol;
115116
return this;
116117
}
117-
public RecombeeClient setBaseUri(String baseUri)
118-
{
118+
119+
public RecombeeClient setBaseUri(String baseUri) {
119120
this.baseUri = baseUri;
120121
return this;
121122
}
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+
}
122149
/* Start of the generated code */
123150
public PropertyInfo send(GetItemPropertyInfo request) throws ApiException {
124151
String responseStr = sendRequest(request);
@@ -140,6 +167,26 @@ public PropertyInfo[] send(ListItemProperties request) throws ApiException {
140167
return null;
141168
}
142169

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+
143190
public Series[] send(ListSeries request) throws ApiException {
144191
String responseStr = sendRequest(request);
145192
try {
@@ -427,28 +474,7 @@ public BatchResponse[] send(Batch batchRequest) throws ApiException {
427474
}
428475
else
429476
{
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)
452478
{
453479
boolean returnProperties = ((ListItems) request).getReturnProperties();
454480
if(returnProperties)
@@ -504,6 +530,14 @@ else if (request instanceof SearchItems)
504530
{
505531
parsedResponse = mapper.convertValue(parsedResponse, SearchResponse.class);
506532
}
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+
}
507541
/* Start of the generated code */
508542
else if (request instanceof GetItemPropertyInfo)
509543
{
@@ -745,41 +779,6 @@ public Map<String, Object> send(GetUserValues request) throws ApiException {
745779
return null;
746780
}
747781

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-
783782
public Item[] send(ListItems request) throws ApiException {
784783
String responseStr = sendRequest(request);
785784

@@ -832,7 +831,6 @@ public User[] send(ListUsers request) throws ApiException {
832831
}
833832
return null;
834833
}
835-
836834
public String send(Request request) throws ApiException {
837835
return sendRequest(request);
838836
}
@@ -854,21 +852,24 @@ private String sendRequest(Request request) throws ApiException {
854852
.addHeader("User-Agent", this.USER_AGENT);
855853

856854

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+
}
869871
}
870872

871-
872873
try {
873874
Response response = tempClient.newCall(httpRequestBuilder.build()).execute();
874875
checkErrors(response, request);
@@ -901,12 +902,6 @@ private String signUrl(String url) {
901902
return null;
902903
}
903904

904-
private String processRequestUri(Request request) {
905-
String uri = "/" + this.databaseId + request.getPath();
906-
uri = appendQueryParameters(uri, request);
907-
return uri;
908-
}
909-
910905
private String appendQueryParameters(String uri, Request request) {
911906
for (Map.Entry<String, Object> pair : request.getQueryParameters().entrySet()) {
912907
uri += uri.contains("?") ? "&" : "?";
@@ -924,26 +919,11 @@ private String formatQueryParameterValue(Object val) {
924919
}
925920
}
926921

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) {
941923
try {
942924
String json = this.mapper.writeValueAsString(req.getBodyParameters());
943925
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);
947927
} catch (JsonProcessingException e) {
948928
e.printStackTrace();
949929
}

src/main/java/com/recombee/api_client/api_requests/DeleteItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Deletes an item of given `itemId` from the catalog.
1616
* If there are any *purchases*, *ratings*, *bookmarks*, *cart additions* or *detail views* of the item present in the database, they will be deleted in cascade as well. Also, if the item is present in some *series*, it will be removed from all the *series* where present.
17-
* If an item becomes obsolete/no longer available, it is often meaningful to keep it in the catalog (along with all the interaction data, which are very useful), and only exclude the item from recommendations. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
17+
* If an item becomes obsolete/no longer available, it is meaningful to keep it in the catalog (along with all the interaction data, which are very useful), and **only exclude the item from recommendations**. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
1818
*/
1919
public class DeleteItem extends Request {
2020

0 commit comments

Comments
 (0)