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

com.google.sitebricks.client.WebClientBuilder Maven / Gradle / Ivy

package com.google.sitebricks.client;

import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.sitebricks.client.transport.Text;
import net.jcip.annotations.NotThreadSafe;

import java.util.Map;

/**
 * @author Dhanji R. Prasanna ([email protected])
 */
@NotThreadSafe
class WebClientBuilder implements Web.FormatBuilder {

  private final Injector injector;

  private String url;
  private Map headers;

  private Web.Auth authType;
  private String username;
  private String password;

  @Inject
  public WebClientBuilder(Injector injector) {
    this.injector = injector;
  }

  public Web.FormatBuilder clientOf(String url) {
    this.url = url;
    this.headers = null;

    return this;
  }

  public Web.FormatBuilder clientOf(String url, Map headers) {
    this.url = url;
    this.headers = headers;

    return this;
  }

  public  Web.ReadAsBuilder transports(Class clazz) {
    return new InternalReadAsBuilder(clazz);
  }

  @Override
  @SuppressWarnings("unchecked")
  public  WebClient transportsText() {
    return (WebClient) transports(String.class).over(Text.class);
  }

  public Web.FormatBuilder auth(Web.Auth auth, String username, String password) {
    Preconditions.checkArgument(null != auth, "Invalid auth type, null.");
    Preconditions.checkArgument(null != username, "Username cannot be null.");
    Preconditions.checkArgument(null != password, "Password cannot be null.");

    this.authType = auth;
    this.username = username;
    this.password = password;
    return this;
  }

  private class InternalReadAsBuilder implements Web.ReadAsBuilder {
    private final Class transporting;

    private InternalReadAsBuilder(Class transporting) {
      this.transporting = transporting;
    }

    public WebClient over(Class transport) {
      return new AHCWebClient(injector, injector.getInstance(transport), authType, username, password, url,
          headers, transporting);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy