/*
 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gbif.registry.directory.client.config;

import org.gbif.registry.directory.client.NodeClient;
import org.gbif.registry.directory.client.ParticipantClient;
import org.gbif.registry.directory.client.PersonClient;
import org.gbif.ws.client.ClientContract;
import org.gbif.ws.client.ClientErrorDecoder;
import org.gbif.ws.client.GbifAuthRequestInterceptor;
import org.gbif.ws.security.Md5EncodeService;
import org.gbif.ws.security.SigningService;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.ObjectMapper;

import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.jackson.JacksonDecoder;

@Configuration
public class DirectoryClientConfiguration {

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

  public DirectoryClientConfiguration(
      @Value("${directory.ws.url}") String url,
      @Value("${directory.app.key}") String appKey,
      @Value("${directory.app.secret}") String secretKey,
      @Qualifier("secretKeySigningService") SigningService signingService,
      Md5EncodeService md5EncodeService,
      @Qualifier("registryObjectMapper") 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();
  }

  @Bean
  public NodeClient nodeClient() {
    return Feign.builder()
        .decoder(decoder)
        .errorDecoder(errorDecoder)
        .contract(contract)
        .requestInterceptor(requestInterceptor)
        .target(NodeClient.class, url);
  }

  @Bean
  public ParticipantClient participantClient() {
    return Feign.builder()
        .decoder(decoder)
        .errorDecoder(errorDecoder)
        .contract(contract)
        .requestInterceptor(requestInterceptor)
        .target(ParticipantClient.class, url);
  }

  @Bean
  public PersonClient personClient() {
    return Feign.builder()
        .decoder(decoder)
        .errorDecoder(errorDecoder)
        .contract(contract)
        .requestInterceptor(requestInterceptor)
        .target(PersonClient.class, url);
  }
}

@@ -883,870 +883,288 @@
 ient
-Contract;%0Aimport org.gbif.ws.client.ClientErrorDecoder;%0Aimport org.gbif.ws.client.GbifAuthRequestInterceptor;%0Aimport org.gbif.ws.security.Md5EncodeService;%0Aimport org.gbif.ws.security.SigningService;%0A%0Aimport org.springframework.beans.factory.annotation.Qualifier;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.context.annotation.Bean;%0Aimport org.springframework.context.annotation.Configuration;%0A%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0Aimport feign.Contract;%0Aimport feign.Feign;%0Aimport feign.codec.Decoder;%0Aimport feign.codec.ErrorDecoder;%0Aimport feign.jackson.JacksonDecoder;%0A%0A@Configuration%0Apublic class DirectoryClientConfiguration %7B%0A%0A  private String url;%0A  private GbifAuthRequestInterceptor requestInterceptor;%0A  private Decoder decoder;%0A  private ErrorDecoder errorDecoder;%0A  private Contract contract
+Factory;%0A%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.context.annotation.Bean;%0Aimport org.springframework.context.annotation.Configuration;%0A%0A@Configuration%0Apublic class DirectoryClientConfiguration %7B%0A%0A  private ClientFactory clientFactory
 ;%0A%0A 
@@ -1361,284 +1361,54 @@
 tKey
-,%0A      @Qualifier(%22secretKeySigningService%22) SigningService signingService,%0A      Md5EncodeService md5EncodeService,%0A      @Qualifier(%22registryObjectMapper%22) ObjectMapper objectMapper) %7B%0A    this.url = url;%0A    this.requestIntercep
+) %7B%0A    this.clientFac
 tor
+y
  =
-%0A        new GbifAuthRequestIntercep
+ new ClientFac
 tor
+y
 (app
@@ -1416,427 +1416,117 @@
 ey, 
-secretKey, signingService, md5EncodeService);%0A    this.decoder = new JacksonDecoder(objectMapper);%0A    this.errorDecoder = new ClientErrorDecoder();%0A    this.contract = new ClientContract();%0A  %7D%0A%0A  @Bean%0A  public NodeClient nodeClient() %7B%0A    return Feign.builder()%0A        .decoder(decoder)%0A        .errorDecoder(errorDecoder)%0A        .contract(contract)%0A        .requestInterceptor(requestInterceptor)%0A        .target
+url, appKey, secretKey);%0A  %7D%0A%0A  @Bean%0A  public NodeClient nodeClient() %7B%0A    return clientFactory.newInstance
 (Nod
@@ -1530,37 +1530,32 @@
 NodeClient.class
-, url
 );%0A  %7D%0A%0A  @Bean%0A
@@ -1618,177 +1618,33 @@
 urn 
-Feign.builder()%0A        .decoder(decoder)%0A        .errorDecoder(errorDecoder)%0A        .contract(contract)%0A        .requestInterceptor(requestInterceptor)%0A        .target
+clientFactory.newInstance
 (Par
@@ -1659,29 +1659,24 @@
 Client.class
-, url
 );%0A  %7D%0A%0A  @B
@@ -1733,177 +1733,33 @@
 urn 
-Feign.builder()%0A        .decoder(decoder)%0A        .errorDecoder(errorDecoder)%0A        .contract(contract)%0A        .requestInterceptor(requestInterceptor)%0A        .target
+clientFactory.newInstance
 (Per
@@ -1773,21 +1773,16 @@
 nt.class
-, url
 );%0A  %7D%0A%7D

Use client factory for directory clients