Add WebClient configuration and update PrestashopProxyController for reactive support
This commit is contained in:
@@ -100,6 +100,15 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
14
api/src/main/java/fr/gameovergne/api/config/AppConfig.java
Normal file
14
api/src/main/java/fr/gameovergne/api/config/AppConfig.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,36 +1,69 @@
|
|||||||
package fr.gameovergne.api.controller.prestashop;
|
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.http.ResponseEntity;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/ps")
|
@RequestMapping("/api/ps")
|
||||||
public class PrestashopProxyController {
|
public class PrestashopProxyController {
|
||||||
|
|
||||||
private final PrestashopClient prestashopClient;
|
private final WebClient webClient;
|
||||||
|
|
||||||
public PrestashopProxyController(PrestashopClient prestashopClient) {
|
@Value("${prestashop.api.base-url}")
|
||||||
this.prestashopClient = prestashopClient;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// ----------- SUPPLIERS -----------
|
||||||
* Exemple simple : proxy GET sur /api/ps/** -> /api/** sur PrestaShop
|
@GetMapping("/suppliers")
|
||||||
*/
|
public ResponseEntity<String> getSuppliers(@RequestParam MultiValueMap<String, String> params) {
|
||||||
@GetMapping("/**")
|
return forwardGet("/suppliers", params);
|
||||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
}
|
||||||
// Exemple d’URL 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
|
|
||||||
|
|
||||||
// On enlève le préfixe /api/ps pour reconstruire le path à appeler sur PrestaShop
|
// ----------- CATEGORIES -----------
|
||||||
String path = relative.replaceFirst("^/api/ps", ""); // -> /products
|
@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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user