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

com.seq.api.Feed Maven / Gradle / Ivy

There is a newer version: 2.2
Show newest version
package com.seq.api;

import com.seq.exception.*;
import com.seq.http.*;
import com.google.gson.annotations.SerializedName;

import java.util.*;

public class Feed implements Iterable {
  /**
   * Unique identifier of the feed.
   */
  public String id;

  /**
   * Type of feed, "action" or "transaction".
   */
  public String type;

  /**
   * The query filter used in /stream-feed-items.
   */
  public String filter;

  /**
   * A list of parameters to be interpolated into the filter expression.
   */
  @SerializedName("filter_params")
  public List filterParams;

  /**
   * Indicates the last transaction consumed by this feed.
   */
  public String cursor;

  private Client _client;

  private T latestItem;
  private String latestCursor;

  class IterablePage {
    public List items;
    public List cursors;

    public IterablePage() {
      this.items = new ArrayList<>();
      this.cursors = new ArrayList<>();
    }
  }

  class ActionPage extends IterablePage {}
  class TransactionPage extends IterablePage {}

  public Iterator iterator() {
    return new Iterator() {
      private int pos = 0;
      private List items = new ArrayList<>();
      private List cursors = new ArrayList<>();

      private IterablePage getPage() throws ChainException {
        Map req = new HashMap<>();
        req.put("id", id);

        if (type.equals("action")) {
          return _client.request("stream-feed-items", req, ActionPage.class);
        } else {
          return _client.request("stream-feed-items", req, TransactionPage.class);
        }
      }

      /**
       * Returns the next item in the results items.
       * @return api object of type T
       */
      public T next() {
        latestItem = items.get(pos);
        latestCursor = cursors.get(pos);
        pos++;
        return latestItem;
      }

      /**
       * Returns true if there is another item in the results items.
       * @return boolean
       */
      public boolean hasNext() {
        if (pos < items.size()) {
          return true;
        } else {
          try {
            IterablePage page = getPage();
            this.pos = 0;
            this.items = page.items;
            this.cursors = page.cursors;
          } catch (ChainException e) {
            return false;
          }
        }

        return true;
      }

      /**
       * This method is unsupported.
       * @throws UnsupportedOperationException
       */
      public void remove() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
      }
    };
  }

  public void ack() throws ChainException {
    Map req = new HashMap<>();
    req.put("id", id);
    req.put("cursor", latestCursor);
    req.put("previous_cursor", cursor);
    _client.request("ack-feed", req, Feed.class);
    cursor = latestCursor;
  }

  public void delete() throws ChainException {
    Map req = new HashMap<>();
    req.put("id", id);
    _client.request("delete-feed", req, Feed.class);
  }

  public static class Action {
    public static class Builder extends Feed.Builder {
      public Builder() {
        super("action");
      }
    }

    /**
     * Retrieves an individual action feed.
     *
     * @param id the feed id
     * @param client ledger API connection object
     * @return a feed object
     * @throws ChainException
     */
    public static Feed get(String id, Client client) throws ChainException {
      return Feed.get(id, "action", client);
    }
  }

  public static class Transaction {
    public static class Builder extends Feed.Builder {
      public Builder() {
        super("transaction");
      }
    }

    /**
     * Retrieves an individual transaction feed.
     *
     * @param id the feed id
     * @param client ledger API connection object
     * @return a feed object
     * @throws ChainException
     */
    public static Feed get(String id, Client client) throws ChainException {
      return Feed.get(id, "transaction", client);
    }
  }

  abstract public static class Builder {
    private String id;
    protected String type;
    private String filter;

    @SerializedName("filter_params")
    private List filterParams;

    private Builder(String type) {
      this.type = type;
      this.filterParams = new ArrayList<>();
    };

    /**
     * Creates a new feed for the ledger.
     * @param client ledger API connection object
     * @return a feed
     * @throws ChainException
     */
    public Feed create(Client client) throws ChainException {
      Feed feed = client.request("create-feed", this, Feed.class);
      feed._client = client;
      return feed;
    }

    /**
     * Specifies the id for the new feed.
     * @param id unique identifier. Will be auto-generated if not provided.
     * @return updated builder
     */
    public Builder setId(String id) {
      this.id = id;
      return this;
    }

    /**
     * Sets the filter expression for the feed.
     * @param filter a filter expression
     * @return updated builder
     */
    public Builder setFilter(String filter) {
      this.filter = filter;
      return this;
    }

    /**
     * Adds a filter parameter that will be interpolated into the filter expression.
     * @param param a filter parameter
     * @return updated builder
     */
    public Builder addFilterParameter(Object param) {
      this.filterParams.add(param);
      return this;
    }

    /**
     * Specifies the parameters that will be interpolated into the filter expression.
     * @param params list of filter parameters
     */
    public Builder setFilterParameters(List params) {
      this.filterParams = new ArrayList<>(params);
      return this;
    }
  }

  /**
   * Retrieves an individual feed.
   *
   * @param id the feed id
   * @param client ledger API connection object
   * @return a feed object
   * @throws ChainException
   */
  private static  Feed get(String id, String type, Client client) throws ChainException {
    Map req = new HashMap<>();
    req.put("id", id);
    Feed feed = client.request("get-feed", req, Feed.class);
    feed._client = client;
    if (!feed.type.equals(type)) {
        throw new ChainException("Feed " + id + " is a " + feed.type + " feed, not "+ type);
    }
    return feed;
  }

  public static class Page extends BasePage {}

  public static class ItemIterable extends BaseItemIterable {
    public ItemIterable(Client client, String path, Query nextQuery) {
      super(client, path, nextQuery, Page.class);
    }
  }

  @Deprecated
  public static class PageIterable extends BasePageIterable {
    public PageIterable(Client client, String path, Query nextQuery) {
      super(client, path, nextQuery, Page.class);
    }
  }

  /**
   * A builder class for querying feeds in the ledger.
   * @deprecated Use {@link ListBuilder} instead
   */
  @Deprecated
  public static class QueryBuilder extends BaseQueryBuilder {
    /**
     * Executes the query, returning a page of feeds that match the query.
     * @param client ledger API connection object
     * @return a page of feeds
     * @throws ChainException
     */
    public Page getPage(Client client) throws ChainException {
      return client.request("list-feeds", this.next, Page.class);
    }

    /**
     * Executes the query, returning a page of feeds that match the query
     * beginning with provided cursor.
     * @param client ledger API connection object
     * @param cursor string representing encoded query object
     * @return a page of feeds
     * @throws ChainException
     */
    public Page getPage(Client client, String cursor) throws ChainException {
      Query next = new Query();
      next.cursor = cursor;
      return client.request("list-feeds", next, Page.class);
    }

    /**
     * Executes the query, returning an iterable over feeds that match the query.
     * @param client ledger API connection object
     * @return an iterable over feeds
     * @throws ChainException
     */
    public ItemIterable getIterable(Client client) throws ChainException {
      return new ItemIterable(client, "list-feeds", this.next);
    }

    /**
     * Executes the query, returning an iterable over pages of feeds that match
     * the query.
     * @param client ledger API connection object
     * @return an iterable over pages of feeds
     * @throws ChainException
     * @deprecated use {@link Feed.ListBuilder#getPage} instead
     */
    @Deprecated
    public PageIterable getPageIterable(Client client) throws ChainException {
      return new PageIterable(client, "list-feeds", this.next);
    }
  }

  /**
   * A builder class for listing feeds in the ledger.
   */
  public static class ListBuilder extends BaseQueryBuilder {
    /**
     * Executes the query, returning a page of feeds that match the query.
     * @param client ledger API connection object
     * @return a page of feeds
     * @throws ChainException
     */
    public Page getPage(Client client) throws ChainException {
      return client.request("list-feeds", this.next, Page.class);
    }

    /**
     * Executes the query, returning a page of feeds that match the query
     * beginning with provided cursor.
     * @param client ledger API connection object
     * @param cursor string representing encoded query object
     * @return a page of feeds
     * @throws ChainException
     */
    public Page getPage(Client client, String cursor) throws ChainException {
      Query next = new Query();
      next.cursor = cursor;
      return client.request("list-feeds", next, Page.class);
    }

    /**
     * Executes the query, returning an iterable over feeds that match the query.
     * @param client ledger API connection object
     * @return an iterable over feeds
     * @throws ChainException
     */
    public ItemIterable getIterable(Client client) throws ChainException {
      return new ItemIterable(client, "list-feeds", this.next);
    }

    /**
     * Not implemented, throws an exception.
     * @param client ledger API connection object
     * @throws ChainException
     * @deprecated use {@link #getPage} instead
     */
    @Deprecated
    public PageIterable getPageIterable(Client client) throws ChainException {
      throw new ChainException("not implemented");
    }
  }
}