Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.getstream.core.StreamPersonalization Maven / Gradle / Ivy
Go to download
Stream Feeds Java Client for backend and android integrations
package io.getstream.core;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.getstream.core.utils.Request.*;
import static io.getstream.core.utils.Routes.buildPersonalizationURL;
import static io.getstream.core.utils.Serialization.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Token;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.RequestOption;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java8.util.concurrent.CompletableFuture;
import java8.util.concurrent.CompletionException;
import java8.util.stream.StreamSupport;
public final class StreamPersonalization {
private final String key;
private final URL baseURL;
private final HTTPClient httpClient;
StreamPersonalization(String key, URL baseURL, HTTPClient httpClient) {
this.key = key;
this.baseURL = baseURL;
this.httpClient = httpClient;
}
public CompletableFuture> get(
Token token, String userID, String resource, Map params)
throws StreamException {
checkNotNull(resource, "Resource can't be empty");
checkArgument(!resource.isEmpty(), "Resource can't be empty");
checkNotNull(params, "Missing params");
try {
final URL url = buildPersonalizationURL(baseURL, resource + '/');
final RequestOption[] options =
StreamSupport.stream(params.entrySet())
.map(entry -> new CustomQueryParameter(entry.getKey(), entry.getValue().toString()))
.toArray(RequestOption[]::new);
return httpClient
.execute(buildGet(url, key, token, options))
.thenApply(
response -> {
try {
return deserialize(response, new TypeReference>() {});
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture post(
Token token,
String userID,
String resource,
Map params,
Map payload)
throws StreamException {
checkNotNull(resource, "Resource can't be empty");
checkArgument(!resource.isEmpty(), "Resource can't be empty");
checkNotNull(params, "Missing params");
checkNotNull(params, "Missing payload");
try {
final byte[] jsonPayload =
toJSON(
new Object() {
public final Map data = payload;
});
final URL url = buildPersonalizationURL(baseURL, resource + '/');
final RequestOption[] options =
StreamSupport.stream(params.entrySet())
.map(entry -> new CustomQueryParameter(entry.getKey(), entry.getValue().toString()))
.toArray(RequestOption[]::new);
return httpClient
.execute(buildPost(url, key, token, jsonPayload, options))
.thenApply(
response -> {
try {
return deserializeError(response);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture delete(
Token token, String userID, String resource, Map params)
throws StreamException {
checkNotNull(resource, "Resource can't be empty");
checkArgument(!resource.isEmpty(), "Resource can't be empty");
checkNotNull(params, "Missing params");
try {
final URL url = buildPersonalizationURL(baseURL, resource + '/');
final RequestOption[] options =
params.entrySet().stream()
.map(entry -> new CustomQueryParameter(entry.getKey(), entry.getValue().toString()))
.toArray(RequestOption[]::new);
return httpClient
.execute(buildDelete(url, key, token, options))
.thenApply(
response -> {
try {
return deserializeError(response);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
}