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

com.flowthings.client.api.Api Maven / Gradle / Ivy

There is a newer version: 0.9.3.2
Show newest version
package com.flowthings.client.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.Map;
import java.util.logging.Logger;

public abstract class Api {

  protected static Logger logger = Logger.getLogger("com.flow.client.api.Api");

  protected String collectResponse(InputStream response) throws IOException {
    BufferedReader r = new BufferedReader(new InputStreamReader(response));
    StringBuilder out = new StringBuilder();
    String s = null;
    while ((s = r.readLine()) != null) {
      out.append(s + '\n');
    }
    return out.toString();
  }

  protected void setRequestProperties(URLConnection connection, Map headers) {
    for (Map.Entry e : headers.entrySet()) {
      connection.setRequestProperty(e.getKey(), e.getValue().toString());
    }
    // if (logger.isLoggable(Level.INFO)) {
    // for (Map.Entry e : headers.entrySet()) {
    // logger.log(Level.INFO, String.format("Header:\t%s\t->\t%s", e.getKey(),
    // e.getValue()));
    // }
    // }
  }

  /**
   * A consistent interface for sending requests via multiple APIs
   */
  public abstract  FlowthingsFuture sendAsync(final Request request);

  public abstract boolean supportsSubscribe();

  public abstract void close();

}