package org.gbif.ws.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.gbif.ws.json.JacksonJsonObjectMapperProvider;
import org.gbif.ws.security.Md5EncodeService;
import org.gbif.ws.security.Md5EncodeServiceImpl;
import org.gbif.ws.security.SecretKeySigningService;
import org.gbif.ws.security.SigningService;

public class ClientFactory {

  private String url;
  private GbifAuthRequestInterceptor requestInterceptor;
  private Decoder decoder;
  private Encoder encoder;
  private ErrorDecoder errorDecoder;
  private Contract contract;

  public ClientFactory(String username, String url, String appKey, String secretKey) {
    this.url = url;
    ObjectMapper objectMapper = JacksonJsonObjectMapperProvider.getObjectMapper();
    this.requestInterceptor =
        new GbifAuthRequestInterceptor(username, appKey, secretKey, new SecretKeySigningService(),
            new Md5EncodeServiceImpl(objectMapper));
    this.encoder = new JacksonEncoder(objectMapper);
    this.decoder = new JacksonDecoder(objectMapper);
    this.errorDecoder = new ClientErrorDecoder();
    this.contract = new ClientContract();
  }

  public ClientFactory(String username, String url, String appKey, String secretKey,
      SigningService signingService,
      Md5EncodeService md5EncodeService, ObjectMapper objectMapper) {
    this.url = url;
    this.requestInterceptor =
        new GbifAuthRequestInterceptor(username, appKey, secretKey, signingService,
            md5EncodeService);
    this.encoder = new JacksonEncoder(objectMapper);
    this.decoder = new JacksonDecoder(objectMapper);
    this.errorDecoder = new ClientErrorDecoder();
    this.contract = new ClientContract();
  }

  public <T> T newInstance(Class<T> clazz) {
    return Feign.builder()
        .encoder(encoder)
        .decoder(decoder)
        .errorDecoder(errorDecoder)
        .contract(contract)
        .requestInterceptor(requestInterceptor)
        .target(clazz, url);
  }
}

@@ -117,16 +117,44 @@
 .Feign;%0A
+import feign.Feign.Builder;%0A
 import f
@@ -165,32 +165,32 @@
 .codec.Decoder;%0A
-
 import feign.cod
@@ -789,16 +789,395 @@
 tract;%0A%0A
+  public ClientFactory(String url) %7B%0A    this.url = url;%0A    ObjectMapper objectMapper = JacksonJsonObjectMapperProvider.getObjectMapper();%0A    this.requestInterceptor = null;%0A    this.encoder = new JacksonEncoder(objectMapper);%0A    this.decoder = new JacksonDecoder(objectMapper);%0A    this.errorDecoder = new ClientErrorDecoder();%0A    this.contract = new ClientContract();%0A  %7D%0A%0A
   public
@@ -2356,14 +2356,31 @@
     
-return
+Feign.Builder builder =
  Fei
@@ -2507,25 +2507,70 @@
 ontract)
+;%0A
 %0A    
-    
+if (requestInterceptor != null) %7B%0A      builder
 .request
@@ -2600,25 +2600,43 @@
 rceptor)
+;
 %0A    
+%7D%0A%0A
     
+return builder
 .target(

Add simple ClientFactory for cases without authentication