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

io.getstream.core.StreamBatch Maven / Gradle / Ivy

There is a newer version: 3.15.0
Show newest version
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.buildGet;
import static io.getstream.core.utils.Request.buildPost;
import static io.getstream.core.utils.Routes.*;
import static io.getstream.core.utils.Serialization.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Joiner;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Token;
import io.getstream.core.models.*;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.EnrichmentFlags;
import io.getstream.core.options.RequestOption;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
import java8.util.J8Arrays;
import java8.util.concurrent.CompletableFuture;
import java8.util.concurrent.CompletionException;

public final class StreamBatch {
  private final String key;
  private final URL baseURL;
  private final HTTPClient httpClient;

  public StreamBatch(String key, URL baseURL, HTTPClient httpClient) {
    this.key = key;
    this.baseURL = baseURL;
    this.httpClient = httpClient;
  }

  public CompletableFuture addToMany(Token token, Activity activity, FeedID... feeds)
      throws StreamException {
    checkNotNull(activity, "Missing activity");
    checkNotNull(feeds, "No feeds to add to");
    checkArgument(feeds.length > 0, "No feeds to add to");

    // XXX: renaming the variable so we can unambiguously name payload field 'activity'
    Activity data = activity;
    String[] feedIDs = J8Arrays.stream(feeds).map(feed -> feed.toString()).toArray(String[]::new);
    try {
      final byte[] payload =
          toJSON(
              new Object() {
                public final Activity activity = data;
                public final String[] feeds = feedIDs;
              });
      final URL url = buildAddToManyURL(baseURL);
      return httpClient
          .execute(buildPost(url, key, token, payload))
          .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 followMany(
      Token token, int activityCopyLimit, FollowRelation... follows) throws StreamException {
    checkArgument(activityCopyLimit >= 0, "Activity copy limit must be non negative");
    checkNotNull(follows, "No feeds to follow");
    checkArgument(follows.length > 0, "No feeds to follow");

    try {
      final byte[] payload = toJSON(follows);
      final URL url = buildFollowManyURL(baseURL);
      return httpClient
          .execute(
              buildPost(
                  url,
                  key,
                  token,
                  payload,
                  new CustomQueryParameter(
                      "activity_copy_limit", Integer.toString(activityCopyLimit))))
          .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 unfollowMany(Token token, UnfollowOperation... unfollows)
      throws StreamException {
    checkNotNull(unfollows, "No feeds to unfollow");
    checkArgument(unfollows.length > 0, "No feeds to unfollow");

    try {
      final byte[] payload = toJSON(unfollows);
      final URL url = buildUnfollowManyURL(baseURL);
      return httpClient
          .execute(buildPost(url, key, token, payload))
          .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> getActivitiesByID(Token token, String... activityIDs)
      throws StreamException {
    checkNotNull(activityIDs, "No activities to get");
    checkArgument(activityIDs.length > 0, "No activities to get");

    try {
      final URL url = buildActivitiesURL(baseURL);
      return httpClient
          .execute(
              buildGet(
                  url,
                  key,
                  token,
                  new CustomQueryParameter("ids", Joiner.on(",").join(activityIDs))))
          .thenApply(
              response -> {
                try {
                  return deserializeContainer(response, Activity.class);
                } catch (StreamException | IOException e) {
                  throw new CompletionException(e);
                }
              });
    } catch (MalformedURLException | URISyntaxException e) {
      throw new StreamException(e);
    }
  }

  public CompletableFuture> getEnrichedActivitiesByID(
      Token token, EnrichmentFlags flags, String... activityIDs) throws StreamException {
    checkNotNull(activityIDs, "No activities to get");
    checkArgument(activityIDs.length > 0, "No activities to get");

    try {
      final URL url = buildEnrichedActivitiesURL(baseURL);
      return httpClient
          .execute(
              buildGet(
                  url,
                  key,
                  token,
                  flags,
                  new CustomQueryParameter("ids", Joiner.on(",").join(activityIDs))))
          .thenApply(
              response -> {
                try {
                  return deserializeContainer(response, EnrichedActivity.class);
                } catch (StreamException | IOException e) {
                  throw new CompletionException(e);
                }
              });
    } catch (MalformedURLException | URISyntaxException e) {
      throw new StreamException(e);
    }
  }

  public CompletableFuture> getActivitiesByForeignID(
      Token token, ForeignIDTimePair... activityIDTimePairs) throws StreamException {
    checkNotNull(activityIDTimePairs, "No activities to get");
    checkArgument(activityIDTimePairs.length > 0, "No activities to get");

    SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    timestampFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    timestampFormat.setLenient(false);

    String[] foreignIDs =
        J8Arrays.stream(activityIDTimePairs)
            .map(pair -> pair.getForeignID())
            .toArray(String[]::new);
    String[] timestamps =
        J8Arrays.stream(activityIDTimePairs)
            .map(pair -> timestampFormat.format(pair.getTime()))
            .toArray(String[]::new);
    try {
      final URL url = buildActivitiesURL(baseURL);
      final RequestOption[] options =
          new RequestOption[] {
            new CustomQueryParameter("foreign_ids", Joiner.on(",").join(foreignIDs)),
            new CustomQueryParameter("timestamps", Joiner.on(",").join(timestamps))
          };
      return httpClient
          .execute(buildGet(url, key, token, options))
          .thenApply(
              response -> {
                try {
                  return deserializeContainer(response, Activity.class);
                } catch (StreamException | IOException e) {
                  throw new CompletionException(e);
                }
              });
    } catch (MalformedURLException | URISyntaxException e) {
      throw new StreamException(e);
    }
  }

  public CompletableFuture> getEnrichedActivitiesByForeignID(
      Token token, ForeignIDTimePair... activityIDTimePairs) throws StreamException {
    checkNotNull(activityIDTimePairs, "No activities to get");
    checkArgument(activityIDTimePairs.length > 0, "No activities to get");

    SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    timestampFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    timestampFormat.setLenient(false);

    String[] foreignIDs =
        J8Arrays.stream(activityIDTimePairs)
            .map(pair -> pair.getForeignID())
            .toArray(String[]::new);
    String[] timestamps =
        J8Arrays.stream(activityIDTimePairs)
            .map(pair -> timestampFormat.format(pair.getTime()))
            .toArray(String[]::new);
    try {
      final URL url = buildEnrichedActivitiesURL(baseURL);
      final RequestOption[] options =
          new RequestOption[] {
            new CustomQueryParameter("foreign_ids", Joiner.on(",").join(foreignIDs)),
            new CustomQueryParameter("timestamps", Joiner.on(",").join(timestamps))
          };
      return httpClient
          .execute(buildGet(url, key, token, options))
          .thenApply(
              response -> {
                try {
                  return deserializeContainer(response, EnrichedActivity.class);
                } catch (StreamException | IOException e) {
                  throw new CompletionException(e);
                }
              });
    } catch (MalformedURLException | URISyntaxException e) {
      throw new StreamException(e);
    }
  }

  public CompletableFuture updateActivities(Token token, Activity... activities)
      throws StreamException {
    checkNotNull(activities, "No activities to update");
    checkArgument(activities.length > 0, "No activities to update");

    try {
      // XXX: renaming the variable so we can unambiguously name payload field 'activities'
      Activity[] data = activities;
      final byte[] payload =
          toJSON(
              new Object() {
                public final Activity[] activities = data;
              });
      final URL url = buildActivitiesURL(baseURL);
      return httpClient
          .execute(buildPost(url, key, token, payload))
          .thenApply(
              response -> {
                try {
                  return deserializeError(response);
                } catch (StreamException | IOException e) {
                  throw new CompletionException(e);
                }
              });
    } catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
      throw new StreamException(e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy