Refactor PrestashopProxyController to simplify endpoint handling and enhance query processing
This commit is contained in:
@@ -1,48 +1,44 @@
|
|||||||
package fr.gameovergne.api.controller;// package fr.gameovergne.api.controller;
|
package fr.gameovergne.api.controller;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import fr.gameovergne.api.service.PrestashopClient;
|
import fr.gameovergne.api.service.PrestashopClient;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/prestashop")
|
@RequestMapping("/api/ps")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PrestashopProxyController {
|
public class PrestashopProxyController {
|
||||||
|
|
||||||
private final PrestashopClient prestashopClient;
|
private final PrestashopClient prestashopClient;
|
||||||
|
|
||||||
@GetMapping("/categories")
|
@GetMapping("/{resource}")
|
||||||
public String getCategories(@RequestParam Map<String, String> params) {
|
public ResponseEntity<String> proxyGet(
|
||||||
|
@PathVariable String resource,
|
||||||
|
@RequestParam MultiValueMap<String, String> params
|
||||||
|
) {
|
||||||
String query = buildQuery(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")
|
private String buildQuery(MultiValueMap<String, String> params) {
|
||||||
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) {
|
|
||||||
if (params == null || params.isEmpty()) {
|
if (params == null || params.isEmpty()) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
String queryString = params.entrySet().stream()
|
String queryString = params.entrySet().stream()
|
||||||
.map(e -> e.getKey() + "=" + e.getValue())
|
.flatMap(e -> e.getValue().stream()
|
||||||
|
.map(v -> e.getKey() + "=" + v))
|
||||||
.collect(Collectors.joining("&"));
|
.collect(Collectors.joining("&"));
|
||||||
|
|
||||||
return "?" + queryString;
|
return "?" + queryString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,6 @@ public class PrestashopClient {
|
|||||||
@Value("${prestashop.api-key}") String rawApiKey,
|
@Value("${prestashop.api-key}") String rawApiKey,
|
||||||
RestTemplateBuilder builder
|
RestTemplateBuilder builder
|
||||||
) {
|
) {
|
||||||
// Normalisation baseUrl (pas de / final)
|
|
||||||
if (baseUrl.endsWith("/")) {
|
if (baseUrl.endsWith("/")) {
|
||||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
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 length = {}", apiKey.length());
|
||||||
log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
|
log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
|
||||||
|
|
||||||
// IMPORTANT : Basic Auth = username = clé, password = vide
|
|
||||||
this.restTemplate = builder
|
this.restTemplate = builder
|
||||||
.basicAuthentication(apiKey, "")
|
.basicAuthentication(apiKey, "")
|
||||||
.build();
|
.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) {
|
public String get(String pathAndQuery) {
|
||||||
String url;
|
String url;
|
||||||
|
|
||||||
// On accepte soit un chemin relatif, soit une URL complète (au cas où)
|
|
||||||
if (pathAndQuery.startsWith("http://") || pathAndQuery.startsWith("https://")) {
|
if (pathAndQuery.startsWith("http://") || pathAndQuery.startsWith("https://")) {
|
||||||
url = pathAndQuery;
|
url = pathAndQuery;
|
||||||
} else if (pathAndQuery.startsWith("/")) {
|
} else if (pathAndQuery.startsWith("/")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user