All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getstream.client.CollectionsClient Maven / Gradle / Ivy

There is a newer version: 3.15.0
Show newest version
package io.getstream.client;

import static io.getstream.core.utils.Auth.buildCollectionsToken;
import static io.getstream.core.utils.Serialization.convert;

import com.google.common.collect.Iterables;
import io.getstream.core.StreamCollections;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.Token;
import io.getstream.core.models.CollectionData;
import io.getstream.core.utils.Auth.TokenAction;
import io.getstream.core.utils.Streams;
import java.util.List;
import java8.util.J8Arrays;
import java8.util.concurrent.CompletableFuture;
import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;

public final class CollectionsClient {
  private final String secret;
  private final StreamCollections collections;

  CollectionsClient(String secret, StreamCollections collections) {
    this.secret = secret;
    this.collections = collections;
  }

  public  CompletableFuture addCustom(String collection, T item) throws StreamException {
    return addCustom(null, collection, item);
  }

  public  CompletableFuture addCustom(String userID, String collection, T item)
      throws StreamException {
    return add(userID, collection, convert(item, CollectionData.class))
        .thenApply(data -> convert(data, (Class) item.getClass()));
  }

  public CompletableFuture add(String collection, CollectionData item)
      throws StreamException {
    return add(null, collection, item);
  }

  public CompletableFuture add(
      String userID, String collection, CollectionData item) throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.WRITE);
    return collections.add(token, userID, collection, item);
  }

  public  CompletableFuture updateCustom(String collection, T item) throws StreamException {
    return updateCustom(null, collection, item);
  }

  public  CompletableFuture updateCustom(String userID, String collection, T item)
      throws StreamException {
    return update(userID, collection, convert(item, CollectionData.class))
        .thenApply(data -> convert(data, (Class) item.getClass()));
  }

  public CompletableFuture update(String collection, CollectionData item)
      throws StreamException {
    return update(null, collection, item);
  }

  public CompletableFuture update(
      String userID, String collection, CollectionData item) throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.WRITE);
    return collections.update(token, userID, collection, item);
  }

  public  CompletableFuture upsertCustom(String collection, Iterable items)
      throws StreamException {
    final CollectionData[] custom =
        Streams.stream(items)
            .map(item -> CollectionData.buildFrom(item))
            .toArray(CollectionData[]::new);
    return upsert(collection, custom);
  }

  public  CompletableFuture upsertCustom(String collection, T... items)
      throws StreamException {
    final CollectionData[] custom =
        J8Arrays.stream(items)
            .map(item -> CollectionData.buildFrom(item))
            .toArray(CollectionData[]::new);
    return upsert(collection, custom);
  }

  public CompletableFuture upsert(String collection, Iterable items)
      throws StreamException {
    return upsert(collection, Iterables.toArray(items, CollectionData.class));
  }

  public CompletableFuture upsert(String collection, CollectionData... items)
      throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.WRITE);
    return collections.upsert(token, collection, items);
  }

  public  CompletableFuture getCustom(Class type, String collection, String id)
      throws StreamException {
    return get(collection, id).thenApply(data -> convert(data, type));
  }

  public CompletableFuture get(String collection, String id)
      throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.READ);
    return collections.get(token, collection, id);
  }

  public  CompletableFuture> selectCustom(
      Class type, String collection, Iterable ids) throws StreamException {
    return selectCustom(type, collection, Iterables.toArray(ids, String.class));
  }

  public  CompletableFuture> selectCustom(
      Class type, String collection, String... ids) throws StreamException {
    return select(collection, ids)
        .thenApply(
            data ->
                StreamSupport.stream(data)
                    .map(item -> convert(item, type))
                    .collect(Collectors.toList()));
  }

  public CompletableFuture> select(String collection, Iterable ids)
      throws StreamException {
    return select(collection, Iterables.toArray(ids, String.class));
  }

  public CompletableFuture> select(String collection, String... ids)
      throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.READ);
    return collections.select(token, collection, ids);
  }

  public CompletableFuture delete(String collection, String id) throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.DELETE);
    return collections.delete(token, collection, id);
  }

  public CompletableFuture deleteMany(String collection, Iterable ids)
      throws StreamException {
    return deleteMany(collection, Iterables.toArray(ids, String.class));
  }

  public CompletableFuture deleteMany(String collection, String... ids)
      throws StreamException {
    final Token token = buildCollectionsToken(secret, TokenAction.DELETE);
    return collections.deleteMany(token, collection, ids);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy