浏览代码

remove feign dependency

Daniel Bohry 3 周之前
父节点
当前提交
8762ca469b

+ 0 - 1
build.gradle

@@ -21,7 +21,6 @@ dependencies {
     implementation 'org.springframework.boot:spring-boot-starter-cache'
     implementation 'org.springframework.boot:spring-boot-starter-actuator'
 
-    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.+'
     implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.+'
     implementation 'com.github.ben-manes.caffeine:caffeine'
     implementation 'net.javacrumbs.shedlock:shedlock-spring:6.+'

+ 1 - 3
src/main/java/com/danielbohry/stocks/App.java

@@ -2,15 +2,13 @@ package com.danielbohry.stocks;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
 @EnableScheduling
-@EnableFeignClients
 @SpringBootApplication
 public class App {
 
-    public static void main(String[] args) {
+    static void main(String[] args) {
         SpringApplication.run(App.class, args);
     }
 

+ 0 - 11
src/main/java/com/danielbohry/stocks/api/GlobalExceptionHandler.java

@@ -4,7 +4,6 @@ package com.danielbohry.stocks.api;
 import com.danielbohry.stocks.exception.DecryptionKeyException;
 import com.danielbohry.stocks.exception.Error;
 import com.danielbohry.stocks.exception.UnauthorizedException;
-import feign.FeignException;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -14,16 +13,6 @@ import org.springframework.web.context.request.WebRequest;
 @ControllerAdvice
 public class GlobalExceptionHandler {
 
-    @ExceptionHandler(FeignException.NotFound.class)
-    public ResponseEntity<Error> handleNotFoundException(FeignException e, WebRequest request) {
-        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new Error("Not found"));
-    }
-
-    @ExceptionHandler(FeignException.TooManyRequests.class)
-    public ResponseEntity<Error> handleTooManyRequestsException(FeignException e, WebRequest request) {
-        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Error("Too many requests"));
-    }
-
     @ExceptionHandler(UnauthorizedException.class)
     public ResponseEntity<Error> handleUnauthorizedException(UnauthorizedException e, WebRequest request) {
         return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new Error(e.getMessage()));

+ 47 - 8
src/main/java/com/danielbohry/stocks/client/ExchangeRateClient.java

@@ -1,15 +1,54 @@
 package com.danielbohry.stocks.client;
 
 import com.danielbohry.stocks.service.ExchangeService.ExchangeRateResponse;
-import org.springframework.cloud.openfeign.FeignClient;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
 
-@FeignClient(name = "exchangeClient", url = "${clients.exchange.url}")
-public interface ExchangeRateClient {
+import static org.springframework.http.HttpMethod.GET;
+import static org.springframework.http.HttpStatus.OK;
+import static org.springframework.http.MediaType.APPLICATION_JSON;
 
-    @GetMapping("{token}/latest/{symbol}")
-    ExchangeRateResponse getRate(@PathVariable("symbol") String symbol,
-                                 @PathVariable("token") String apiKey);
+@Component
+@RequiredArgsConstructor
+public class ExchangeRateClient {
+
+    private final RestTemplate rest;
+
+    @Value("${clients.exchange.url}")
+    private String baseUrl;
+
+    public ExchangeRateResponse getRate(String symbol, String apiKey) {
+        String url = baseUrl + "/" + apiKey + "/latest/" + symbol;
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(APPLICATION_JSON);
+
+        HttpEntity<Void> entity = new HttpEntity<>(headers);
+
+        try {
+            ResponseEntity<ExchangeRateResponse> response = rest.exchange(
+                    url,
+                    GET,
+                    entity,
+                    ExchangeRateResponse.class
+            );
+
+            if (response.getStatusCode() == OK) {
+                return response.getBody();
+            } else {
+                throw new RuntimeException("Unexpected response status: " + response.getStatusCode());
+            }
+        } catch (HttpClientErrorException e) {
+            throw new RuntimeException("Error calling exchange API: " + e.getStatusCode() + " - " + e.getResponseBodyAsString());
+        } catch (Exception e) {
+            throw new RuntimeException("An error occurred calling exchange API: " + e.getMessage());
+        }
+    }
 
 }

+ 0 - 24
src/main/java/com/danielbohry/stocks/config/FeignConfig.java

@@ -1,24 +0,0 @@
-package com.danielbohry.stocks.config;
-
-import feign.RequestInterceptor;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.util.List;
-
-@Configuration
-public class FeignConfig {
-
-    @Bean
-    public RequestInterceptor customTickerInterceptor() {
-        return template -> {
-            if (template.url().contains("/fundamentals/meta") &&
-                template.queries().containsKey("tickers")) {
-
-                List<String> tickerList = template.queries().get("tickers").stream().map(i -> i.replaceAll("%2C", ",")).toList();
-                template.query("tickers", (String[]) null);
-                template.query("tickers", String.join(",", tickerList));
-            }
-        };
-    }
-}