package nakadi;

import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Supports API operations related to the registry.
 */
public class RegistryResource {

  private static final String PATH = "registry";
  private static final String PATH_VALIDATION_STRATEGIES = "validation-strategies";
  private static final String PATH_ENRICHMENT_STRATEGIES = "enrichment-strategies";
  private static final String APPLICATION_JSON = "application/json";
  private static final Type TYPE = new TypeToken<List<String>>() {
  }.getType();

  private final NakadiClient client;

  public RegistryResource(NakadiClient client) {
    this.client = client;
  }

  /**
   * @return the validation strategies on the server.
   */
  public ValidationStrategyCollection listValidationStrategies() {
    return loadValidationPage(collection(PATH, PATH_VALIDATION_STRATEGIES).buildString());
  }

  /**
   * @return the enrichment strategies on the server.
   */
  public EnrichmentStrategyCollection listEnrichmentStrategies() {
    return loadEnrichmentPage(collection(PATH, PATH_ENRICHMENT_STRATEGIES).buildString());
  }

  ValidationStrategyCollection loadValidationPage(String url) {
    return new ValidationStrategyCollection(loadCollection(url), Lists.newArrayList(), this);
  }

  EnrichmentStrategyCollection loadEnrichmentPage(String url) {
    return new EnrichmentStrategyCollection(loadCollection(url), Lists.newArrayList(), this);
  }

  private List<String> loadCollection(String url) {
    Response response = client.resourceProvider().newResource()
        .requestThrowing(Resource.GET, url,
            ResourceSupport.options(APPLICATION_JSON)
                .tokenProvider(client.resourceTokenProvider()));

    return client.jsonSupport().fromJson(response.responseBody().asString(), TYPE);
  }

  private UriBuilder collection(String basePath, String path) {
    return UriBuilder.builder(client.baseURI()).path(basePath).path(path);
  }
}

@@ -145,16 +145,54 @@
 il.List;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A/**%0A *
@@ -1612,25 +1612,96 @@
  Res
-ponse response = 
+ourceOptions options = ResourceSupport.options(APPLICATION_JSON)%0A        .tokenProvider(
 clie
@@ -1711,16 +1711,21 @@
 resource
+Token
 Provider
@@ -1730,268 +1730,530 @@
 er()
-.newResource()%0A        .requestThrowing(Resource.GET, url,%0A            ResourceSupport.options(APPLICATION_JSON)%0A                .tokenProvider(client.resourceTokenProvider()));%0A%0A    return client.jsonSupport().fromJson(response.responseBody().asString(), TYPE
+);%0A    Response response = client.resourceProvider()%0A        .newResource()%0A        .policyBackoff(policyBackoffForCollectionPage())%0A        .requestThrowing(Resource.GET, url, options);%0A%0A    return client.jsonSupport().fromJson(response.responseBody().asString(), TYPE);%0A  %7D%0A%0A  private PolicyBackoff policyBackoffForCollectionPage() %7B%0A    return ExponentialBackoff.newBuilder()%0A        .initialInterval(1000, TimeUnit.MILLISECONDS)%0A        .maxAttempts(5)%0A        .maxInterval(6000, TimeUnit.MILLISECONDS)%0A        .build(
 );%0A 

Add backoffs to registry calls