diff --git a/api/pom.xml b/api/pom.xml index 1377aa6..bb77c35 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -100,6 +100,15 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-webflux + + + io.projectreactor + reactor-test + test + diff --git a/api/src/main/java/fr/gameovergne/api/config/AppConfig.java b/api/src/main/java/fr/gameovergne/api/config/AppConfig.java new file mode 100644 index 0000000..7702bdb --- /dev/null +++ b/api/src/main/java/fr/gameovergne/api/config/AppConfig.java @@ -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(); + } +} \ No newline at end of file diff --git a/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java b/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java index 5431f62..d0fa1ee 100644 --- a/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java +++ b/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java @@ -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 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 + // ----------- SUPPLIERS ----------- + @GetMapping("/suppliers") + public ResponseEntity getSuppliers(@RequestParam MultiValueMap 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 getCategories(@RequestParam MultiValueMap params) { + return forwardGet("/categories", params); + } - String query = request.getQueryString(); // ex: display=full + // ----------- MANUFACTURERS ----------- + @GetMapping("/manufacturers") + public ResponseEntity getManufacturers(@RequestParam MultiValueMap params) { + return forwardGet("/manufacturers", params); + } - return prestashopClient.get(path, query); + // ----------- Méthode commune de forward ----------- + + private ResponseEntity forwardGet(String resourcePath, MultiValueMap 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> 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(); } } \ No newline at end of file