Add WebClient configuration and update PrestashopProxyController for reactive support

This commit is contained in:
Vincent Guillet
2025-11-25 18:33:53 +01:00
parent 019b8f4c01
commit e839aae4dd
3 changed files with 75 additions and 19 deletions

View File

@@ -100,6 +100,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,14 @@
package fr.gameovergne.api.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class AppConfig {
@Bean
public WebClient prestashopWebClient(WebClient.Builder builder) {
return builder.build();
}
}

View File

@@ -1,36 +1,69 @@
package fr.gameovergne.api.controller.prestashop;
import fr.gameovergne.api.service.prestashop.PrestashopClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/api/ps")
public class PrestashopProxyController {
private final PrestashopClient prestashopClient;
private final WebClient webClient;
public PrestashopProxyController(PrestashopClient prestashopClient) {
this.prestashopClient = prestashopClient;
@Value("${prestashop.api.base-url}")
private String prestaBaseUrl;
@Value("${prestashop.api.key}")
private String prestaApiKey; // ta clé déjà encodée en Base64
public PrestashopProxyController(WebClient prestashopWebClient) {
this.webClient = prestashopWebClient;
}
/**
* Exemple simple : proxy GET sur /api/ps/** -> /api/** sur PrestaShop
*/
@GetMapping("/**")
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
// Exemple dURL front : /api/ps/products?display=full
String fullPath = request.getRequestURI(); // ex: /api/ps/products
String contextPath = request.getContextPath(); // souvent ""
String relative = fullPath.substring(contextPath.length()); // /api/ps/products
// ----------- SUPPLIERS -----------
@GetMapping("/suppliers")
public ResponseEntity<String> getSuppliers(@RequestParam MultiValueMap<String, String> params) {
return forwardGet("/suppliers", params);
}
// On enlève le préfixe /api/ps pour reconstruire le path à appeler sur PrestaShop
String path = relative.replaceFirst("^/api/ps", ""); // -> /products
// ----------- CATEGORIES -----------
@GetMapping("/categories")
public ResponseEntity<String> getCategories(@RequestParam MultiValueMap<String, String> params) {
return forwardGet("/categories", params);
}
String query = request.getQueryString(); // ex: display=full
// ----------- MANUFACTURERS -----------
@GetMapping("/manufacturers")
public ResponseEntity<String> getManufacturers(@RequestParam MultiValueMap<String, String> params) {
return forwardGet("/manufacturers", params);
}
return prestashopClient.get(path, query);
// ----------- Méthode commune de forward -----------
private ResponseEntity<String> forwardGet(String resourcePath, MultiValueMap<String, String> params) {
// IMPORTANT : build(false) => ne PAS ré-encoder les crochets
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(prestaBaseUrl + resourcePath);
params.forEach((key, values) -> values.forEach(v -> builder.queryParam(key, v)));
String targetUrl = builder.build(false).toUriString(); // false => pas de double encodage
Mono<ResponseEntity<String>> monoResponse = webClient
.get()
.uri(targetUrl)
.header(HttpHeaders.AUTHORIZATION, "Basic " + prestaApiKey)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(String.class);
// On bloque car ton contrôleur est synchrone (MVC)
return monoResponse.block();
}
}