package org.gbif.ws.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.jackson.JacksonDecoder;
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 ErrorDecoder errorDecoder;
  private Contract contract;

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

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

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

@@ -137,32 +137,60 @@
 .codec.Decoder;%0A
+import feign.codec.Encoder;%0A
 import feign.cod
@@ -239,24 +239,61 @@
 sonDecoder;%0A
+import feign.jackson.JacksonEncoder;%0A
 import org.g
@@ -659,32 +659,59 @@
 ecoder decoder;%0A
+  private Encoder encoder;%0A
   private ErrorD
@@ -1106,16 +1106,69 @@
 pper));%0A
+    this.encoder = new JacksonEncoder(objectMapper);%0A
     this
@@ -1621,16 +1621,69 @@
 rvice);%0A
+    this.encoder = new JacksonEncoder(objectMapper);%0A
     this
@@ -1896,16 +1896,42 @@
 ilder()%0A
+        .encoder(encoder)%0A
         

Add Client jackson encoder