content: /*
   * Copyright (c) 2024 Broadcom, Inc. or its affiliates
   *
   * 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 com.broadcom.tanzu.newsfactory.impl;
  
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.web.client.ClientHttpRequestFactories;
  import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  import org.springframework.web.client.RestClient;
  
  import java.time.Duration;
  
  @Configuration(proxyBeanMethods = false)
  class RestConfig {
      @Bean
      RestClient restClient(RestClient.Builder clientBuilder,
                            @Value("${spring.application.name}") String appName) {
          final var reqSettings =
                  ClientHttpRequestFactorySettings.DEFAULTS
                          .withConnectTimeout(Duration.ofSeconds(30))
                          .withReadTimeout(Duration.ofSeconds(30));
          final var reqFactory =
                  ClientHttpRequestFactories.get(HttpComponentsClientHttpRequestFactory.class, reqSettings);
  
          return clientBuilder.defaultHeader(HttpHeaders.USER_AGENT, appName)
                  .requestFactory(reqFactory)
                  .build();
      }
  }
  