Refactor PrestashopProxyController to simplify endpoint handling and enhance query processing

This commit is contained in:
Vincent Guillet
2025-11-29 08:52:59 +01:00
parent 504fb4fe8e
commit d802418c29
2 changed files with 20 additions and 33 deletions

View File

@@ -1,48 +1,44 @@
package fr.gameovergne.api.controller;// package fr.gameovergne.api.controller;
package fr.gameovergne.api.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import fr.gameovergne.api.service.PrestashopClient;
import java.util.Map;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/prestashop")
@RequestMapping("/api/ps")
@RequiredArgsConstructor
public class PrestashopProxyController {
private final PrestashopClient prestashopClient;
@GetMapping("/categories")
public String getCategories(@RequestParam Map<String, String> params) {
@GetMapping("/{resource}")
public ResponseEntity<String> proxyGet(
@PathVariable String resource,
@RequestParam MultiValueMap<String, String> params
) {
String query = buildQuery(params);
return prestashopClient.get("/categories" + query);
String path = "/" + resource + query;
String body = prestashopClient.get(path);
return ResponseEntity.ok(body);
}
@GetMapping("/manufacturers")
public String getManufacturers(@RequestParam Map<String, String> params) {
String query = buildQuery(params);
return prestashopClient.get("/manufacturers" + query);
}
@GetMapping("/suppliers")
public String getSuppliers(@RequestParam Map<String, String> params) {
String query = buildQuery(params);
return prestashopClient.get("/suppliers" + query);
}
private String buildQuery(Map<String, String> params) {
private String buildQuery(MultiValueMap<String, String> params) {
if (params == null || params.isEmpty()) {
return "";
}
String queryString = params.entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue())
.flatMap(e -> e.getValue().stream()
.map(v -> e.getKey() + "=" + v))
.collect(Collectors.joining("&"));
return "?" + queryString;
}
}

View File

@@ -20,7 +20,6 @@ public class PrestashopClient {
@Value("${prestashop.api-key}") String rawApiKey,
RestTemplateBuilder builder
) {
// Normalisation baseUrl (pas de / final)
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
@@ -34,25 +33,17 @@ public class PrestashopClient {
);
}
// Logs pour contrôle visuel
log.info("[PrestaShop] API key length = {}", apiKey.length());
log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
// IMPORTANT : Basic Auth = username = clé, password = vide
this.restTemplate = builder
.basicAuthentication(apiKey, "")
.build();
}
/**
* Appel simple GET PrestaShop.
* Exemple d'appel depuis le controller :
* client.get("/categories?display=[id,name,active]&output_format=JSON");
*/
public String get(String pathAndQuery) {
String url;
// On accepte soit un chemin relatif, soit une URL complète (au cas où)
if (pathAndQuery.startsWith("http://") || pathAndQuery.startsWith("https://")) {
url = pathAndQuery;
} else if (pathAndQuery.startsWith("/")) {