Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RouteClinet 로깅 및 리팩터링 누락 코드 추가 #903

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/main/java/com/ody/eta/service/EtaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.ody.util.DistanceCalculator;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -60,6 +61,7 @@ private void updateMateEta(MateEtaRequest mateEtaRequest, Eta mateEta, Meeting m
}

if (isRouteClientCallTime(mateEta)) {
MDC.put("mateId", mateEta.getMate().getId().toString());
RouteTime routeTime = routeService.calculateRouteTime(
mateEtaRequest.toCoordinates(),
meeting.getTargetCoordinates()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ody.route.config;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Getter
@ConfigurationProperties(prefix = "route")
public class RouteClientProperties {

private final Map<String, RouteClientProperty> properties;

public RouteClientProperties(List<RouteClientProperty> vendors) {
properties = vendors.stream()
.collect(Collectors.toMap(RouteClientProperty::name, property -> property));
}

public RouteClientProperty getProperty(String name) {
return properties.get(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ody.route.config;

public record RouteClientProperty(String name, String baseUrl, String apiKey) {

}
35 changes: 19 additions & 16 deletions backend/src/main/java/com/ody/route/config/RouteConfig.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
package com.ody.route.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ody.route.service.GoogleRouteClient;
import com.ody.route.service.OdsayRouteClient;
import com.ody.route.service.RouteClient;
import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient;

@Profile("!test")
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(RouteProperties.class)
@EnableConfigurationProperties(RouteClientProperties.class)
public class RouteConfig {

private final RouteProperties routeProperties;
private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ofSeconds(60);
private static final Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(30);

private final RouteClientProperties properties;

@Bean
@Order(1)
public RouteClient odysayRouteClient(RestClient.Builder routeRestClientBuilder) {
return new OdsayRouteClient(routeProperties, routeRestClientBuilder);
public RouteClient odysayRouteClient(ObjectMapper objectMapper) {
RouteClientProperty property = properties.getProperty("odsay");
return new OdsayRouteClient(property, builder(objectMapper));
}

@Bean
@Order(2)
public RouteClient googleRouteClient(
RestClient.Builder routeRestClientBuilder,
@Value("${google.maps.api-key}") String googleApiKey
) {
return new GoogleRouteClient(routeRestClientBuilder, googleApiKey);
public RouteClient googleRouteClient(ObjectMapper objectMapper) {
RouteClientProperty property = properties.getProperty("google");
return new GoogleRouteClient(property, builder(objectMapper));
}

@Bean
public RestClientCustomizer routeRestClientCustomizer() {
return builder -> builder.requestFactory(clientHttpRequestFactory())
.build();
public RestClient.Builder builder(ObjectMapper objectMapper) {
return RestClient.builder()
.requestFactory(new BufferingClientHttpRequestFactory(clientHttpRequestFactory()))
.requestInterceptor(new RouteClientLoggingInterceptor(objectMapper));
}

private ClientHttpRequestFactory clientHttpRequestFactory() {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.DEFAULTS
.withConnectTimeout(Duration.ofSeconds(60))
.withReadTimeout(Duration.ofSeconds(30)); //TODO: timeout 처리 로직 구현
.withConnectTimeout(DEFAULT_CONNECTION_TIMEOUT)
.withReadTimeout(DEFAULT_READ_TIMEOUT);
return ClientHttpRequestFactories.get(settings);
}
}
14 changes: 0 additions & 14 deletions backend/src/main/java/com/ody/route/config/RouteProperties.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
import com.ody.common.exception.OdyBadRequestException;
import com.ody.common.exception.OdyServerErrorException;
import com.ody.meeting.domain.Coordinates;
import com.ody.route.config.RouteClientProperty;
import com.ody.route.domain.ClientType;
import com.ody.route.domain.RouteTime;
import com.ody.route.dto.DistanceMatrixElementStatus;
import com.ody.route.dto.DistanceMatrixResponse;
import com.ody.route.dto.DistanceMatrixResponse.DistanceMatrixElement;
import com.ody.route.dto.DistanceMatrixStatus;
import java.time.Duration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;

@Slf4j
@RequiredArgsConstructor
public class GoogleRouteClient implements RouteClient {

private final String apiKey;
private final RouteClientProperty property;
private final RestClient restClient;

public GoogleRouteClient(RestClient.Builder routeRestClientBuilder, String apiKey) {
this.apiKey = apiKey;
this.restClient = routeRestClientBuilder.baseUrl("https://maps.googleapis.com").build();
public GoogleRouteClient(RouteClientProperty property, RestClient.Builder builder) {
this(property, builder.build());
}

@Override
Expand All @@ -33,12 +35,12 @@ public RouteTime calculateRouteTime(Coordinates origin, Coordinates target) {
}

private DistanceMatrixResponse getDistanceMatrixResponse(Coordinates origin, Coordinates target) {
String url = UriComponentsBuilder.fromPath("/maps/api/distancematrix/json")
String url = UriComponentsBuilder.fromHttpUrl(property.baseUrl())
.queryParam("destinations", mapCoordinatesToUrl(target))
.queryParam("origins", mapCoordinatesToUrl(origin))
.queryParam("mode", "transit")
.queryParam("transit_mode", "bus|subway")
.queryParam("key", apiKey)
.queryParam("key", property.apiKey())
.build(false)
.toUriString();

Expand Down
18 changes: 8 additions & 10 deletions backend/src/main/java/com/ody/route/service/OdsayRouteClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

import com.ody.common.exception.OdyServerErrorException;
import com.ody.meeting.domain.Coordinates;
import com.ody.route.config.RouteProperties;
import com.ody.route.config.RouteClientProperty;
import com.ody.route.domain.ClientType;
import com.ody.route.domain.RouteTime;
import com.ody.route.dto.OdsayResponse;
import com.ody.route.mapper.OdsayResponseMapper;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestClient;

@Slf4j
@RequiredArgsConstructor
public class OdsayRouteClient implements RouteClient {

private final RouteProperties routeProperties;
private final RouteClientProperty property;
private final RestClient restClient;

public OdsayRouteClient(
RouteProperties routeProperties,
RestClient.Builder routeRestClientBuilder
) {
this.routeProperties = routeProperties;
this.restClient = routeRestClientBuilder.build();
public OdsayRouteClient(RouteClientProperty property, RestClient.Builder builder) {
this(property, builder.build());
}

@Override
Expand All @@ -44,12 +42,12 @@ private OdsayResponse getOdsayResponse(Coordinates origin, Coordinates target) {
}

private URI makeURI(Coordinates origin, Coordinates target) {
String uri = routeProperties.getUrl()
String uri = property.baseUrl()
+ "?SX=" + origin.getLongitude()
+ "&SY=" + origin.getLatitude()
+ "&EX=" + target.getLongitude()
+ "&EY=" + target.getLatitude()
+ "&apiKey=" + routeProperties.getApiKey();
+ "&apiKey=" + property.apiKey();
try {
return new URI(uri);
} catch (URISyntaxException exception) {
Expand Down
15 changes: 8 additions & 7 deletions backend/src/main/resources/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ springdoc:
operations-sorter: alpha
enabled: true

odsay:
url: https://api.odsay.com/v1/api/searchPubTransPathT
api-key: ENC(7am2oBS6Y/CCfThV+Yv59ZpjjIu8vOxt8v0/cEvGFDfUemAmSpbz+vfR3bsoDrKoJ/EwmeV6VXo=)

google:
maps:
api-key: ENC(B/qgWb19BUQN+qdCHQKP7Ocx4xszftoRM4DfcjSIxCXlwgpfvZW4QpFBMGbNqVsf)
route:
vendors:
- name: odsay
base-url: https://api.odsay.com/v1/api/searchPubTransPathT
api-key: ENC(7am2oBS6Y/CCfThV+Yv59ZpjjIu8vOxt8v0/cEvGFDfUemAmSpbz+vfR3bsoDrKoJ/EwmeV6VXo=)
- name: google
base-url: https://maps.googleapis.com/maps/api/distancematrix/json
api-key: ENC(B/qgWb19BUQN+qdCHQKP7Ocx4xszftoRM4DfcjSIxCXlwgpfvZW4QpFBMGbNqVsf)

auth:
access-key: ENC(Ve3QKOE7PzbTOpQ6b58Oyi1Qrr9DojPgUA+jCwYcmDdSVdP34Z2eKw==)
Expand Down
12 changes: 9 additions & 3 deletions backend/src/test/java/com/ody/common/BaseRouteClientTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.ody.common;

import com.ody.route.config.RouteProperties;
import com.ody.route.config.RouteClientProperties;
import com.ody.route.config.RouteClientProperty;
import com.ody.route.service.RouteClient;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClient;

@EnableConfigurationProperties(RouteProperties.class)
@EnableConfigurationProperties(RouteClientProperties.class)
public abstract class BaseRouteClientTest {

@Autowired
Expand All @@ -18,15 +19,20 @@ public abstract class BaseRouteClientTest {
protected RestClient.Builder restClientBuilder;

@Autowired
protected RouteProperties routeProperties;
protected RouteClientProperties properties;

protected RouteClient routeClient;

protected RouteClientProperty property;

@BeforeEach
void setUp() {
this.mockServer = MockRestServiceServer.bindTo(restClientBuilder).build();
this.property = getProperty();
this.routeClient = createRouteClient();
}

protected abstract RouteClientProperty getProperty();

protected abstract RouteClient createRouteClient();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@
import com.ody.common.exception.OdyBadRequestException;
import com.ody.common.exception.OdyServerErrorException;
import com.ody.meeting.domain.Coordinates;
import com.ody.route.config.RouteClientProperty;
import com.ody.route.domain.RouteTime;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.web.util.UriComponentsBuilder;

@RestClientTest(GoogleRouteClient.class)
public class GoogleRouteClientTest extends BaseRouteClientTest {

private static final String BASE_URI = "https://maps.googleapis.com/maps/api/distancematrix/json?";

@Value("${google.maps.api-key}")
private String testApiKey;
class GoogleRouteClientTest extends BaseRouteClientTest {

@DisplayName("버스, 지하철을 이용한 소요시간 계산 요청 성공 시, 가장 빠른 소요 시간을 분으로 변환하여 반환한다.")
@Test
Expand Down Expand Up @@ -110,12 +105,12 @@ void calculateRouteTimeWhenElementStatusNotOK() {
}

private String makeUri(Coordinates origin, Coordinates target) {
return UriComponentsBuilder.fromHttpUrl(BASE_URI)
return UriComponentsBuilder.fromHttpUrl(property.baseUrl())
.queryParam("destinations", mapCoordinatesToUrl(target))
.queryParam("origins", mapCoordinatesToUrl(origin))
.queryParam("mode", "transit")
.queryParam("transit_mode", "bus%7Csubway")
.queryParam("key", testApiKey)
.queryParam("key", property.apiKey())
.build()
.toUriString();
}
Expand All @@ -130,8 +125,13 @@ private void setMockServer(Coordinates origin, Coordinates target, String respon
.andRespond(MockRestResponseCreators.withSuccess(response, MediaType.APPLICATION_JSON));
}

@Override
protected RouteClientProperty getProperty() {
return properties.getProperty("google");
}

@Override
protected RouteClient createRouteClient() {
return new GoogleRouteClient(restClientBuilder, testApiKey);
return new GoogleRouteClient(property, restClientBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ody.common.exception.OdyBadRequestException;
import com.ody.common.exception.OdyServerErrorException;
import com.ody.meeting.domain.Coordinates;
import com.ody.route.config.RouteClientProperty;
import com.ody.route.domain.RouteTime;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -91,12 +92,12 @@ private void setMockServer(Coordinates origin, Coordinates target, String respon
}

private URI makeUri(Coordinates origin, Coordinates target) {
String uri = routeProperties.getUrl()
String uri = property.baseUrl()
+ "?SX=" + origin.getLongitude()
+ "&SY=" + origin.getLatitude()
+ "&EX=" + target.getLongitude()
+ "&EY=" + target.getLatitude()
+ "&apiKey=" + routeProperties.getApiKey();
+ "&apiKey=" + property.apiKey();

try {
return new URI(uri);
Expand All @@ -111,8 +112,13 @@ private String makeResponseByPath(String path) throws IOException {
);
}

@Override
protected RouteClientProperty getProperty() {
return properties.getProperty("odsay");
}

@Override
protected RouteClient createRouteClient() {
return new OdsayRouteClient(routeProperties, restClientBuilder);
return new OdsayRouteClient(property, restClientBuilder);
}
}
Loading
Loading