Refactor PrestashopClient and PrestashopProxyController to enhance query handling and response logging
This commit is contained in:
@@ -16,61 +16,80 @@ public class PrestashopProxyController {
|
||||
this.prestashopClient = prestashopClient;
|
||||
}
|
||||
|
||||
private String extractPath(HttpServletRequest request) {
|
||||
return request.getRequestURI().replaceFirst("^/api/ps", ""); // => /products, /categories, ...
|
||||
// ---------- utilitaire pour extraire le path Presta ----------
|
||||
|
||||
private String extractPrestaPath(HttpServletRequest request) {
|
||||
// Traefik strip déjà /gameovergne-api, donc Spring voit /api/ps/...
|
||||
String uri = request.getRequestURI(); // ex: /api/ps/categories
|
||||
String prefix = "/api/ps";
|
||||
String path = uri.startsWith(prefix) ? uri.substring(prefix.length()) : uri;
|
||||
if (path.isEmpty()) {
|
||||
path = "/";
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
// ---------- GET ----------
|
||||
// ---------- GET : /api/ps/** -> Presta GET ----------
|
||||
|
||||
@GetMapping("/**")
|
||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||
String path = extractPath(request);
|
||||
String query = request.getQueryString();
|
||||
String path = extractPrestaPath(request); // ex: "/categories"
|
||||
String query = request.getQueryString(); // ex: "display=[id,name]&output_format=JSON"
|
||||
|
||||
String body = prestashopClient.get(path, query);
|
||||
|
||||
// Presta renvoie du JSON (output_format=JSON), donc on force application/json
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(body);
|
||||
}
|
||||
|
||||
// ---------- POST ----------
|
||||
// ---------- POST : /api/ps/** -> Presta POST ----------
|
||||
|
||||
@PostMapping("/**")
|
||||
public ResponseEntity<String> proxyPost(HttpServletRequest request,
|
||||
@RequestBody String xmlBody) {
|
||||
String path = extractPath(request);
|
||||
@RequestBody(required = false) String xmlBody) {
|
||||
String path = extractPrestaPath(request);
|
||||
String query = request.getQueryString();
|
||||
String body = prestashopClient.post(path, query, xmlBody);
|
||||
|
||||
String responseBody = prestashopClient.post(path, query, xmlBody != null ? xmlBody : "");
|
||||
|
||||
// Les POST/PUT/DELETE Presta renvoient typiquement de l’XML
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(body);
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(responseBody);
|
||||
}
|
||||
|
||||
// ---------- PUT ----------
|
||||
// ---------- PUT : /api/ps/** -> Presta PUT ----------
|
||||
|
||||
@PutMapping("/**")
|
||||
public ResponseEntity<String> proxyPut(HttpServletRequest request,
|
||||
@RequestBody String xmlBody) {
|
||||
String path = extractPath(request);
|
||||
@RequestBody(required = false) String xmlBody) {
|
||||
String path = extractPrestaPath(request);
|
||||
String query = request.getQueryString();
|
||||
String body = prestashopClient.put(path, query, xmlBody);
|
||||
|
||||
String responseBody = prestashopClient.put(path, query, xmlBody != null ? xmlBody : "");
|
||||
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(body);
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(responseBody);
|
||||
}
|
||||
|
||||
// ---------- DELETE ----------
|
||||
// ---------- DELETE : /api/ps/** -> Presta DELETE ----------
|
||||
|
||||
@DeleteMapping("/**")
|
||||
public ResponseEntity<String> proxyDelete(HttpServletRequest request) {
|
||||
String path = extractPath(request);
|
||||
String path = extractPrestaPath(request);
|
||||
String query = request.getQueryString();
|
||||
String body = prestashopClient.delete(path, query);
|
||||
|
||||
String responseBody = prestashopClient.delete(path, query);
|
||||
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(body);
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(responseBody);
|
||||
}
|
||||
}
|
||||
@@ -28,20 +28,33 @@ public class PrestashopClient {
|
||||
}
|
||||
|
||||
// ========== GET ==========
|
||||
public String get(String path, String ignoredQuery) {
|
||||
|
||||
/**
|
||||
* @param path ex: "/categories", "/products/12"
|
||||
* @param rawQuery ex: "display=[id,name]&output_format=JSON&filter[active]=1", ou null
|
||||
*/
|
||||
public String get(String path, String rawQuery) {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder
|
||||
.fromHttpUrl(baseUrl)
|
||||
.path("/api")
|
||||
.path(path)
|
||||
.queryParam("output_format", "JSON")
|
||||
.queryParam("display", "full");
|
||||
.path(path);
|
||||
|
||||
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||
// On laisse le caller (notre proxy / Angular via Spring) décider des paramètres
|
||||
builder.query(rawQuery);
|
||||
} else {
|
||||
// fallback par défaut si aucun paramètre reçu
|
||||
builder
|
||||
.queryParam("output_format", "JSON")
|
||||
.queryParam("display", "full");
|
||||
}
|
||||
|
||||
String url = builder.build(true).toUriString();
|
||||
log.info("[PrestaShop] GET {}");
|
||||
log.info("[PrestaShop] GET {}", url);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
|
||||
|
||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||
|
||||
@@ -51,7 +64,9 @@ public class PrestashopClient {
|
||||
entity,
|
||||
String.class
|
||||
);
|
||||
log.info("[PrestaShop] Réponse GET {} pour {}");
|
||||
|
||||
log.info("[PrestaShop] Réponse GET {} pour {}", response.getStatusCode(), url);
|
||||
|
||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||
+ response.getStatusCode() + " for URL " + url);
|
||||
@@ -68,16 +83,16 @@ public class PrestashopClient {
|
||||
.path(path);
|
||||
|
||||
if (query != null && !query.isBlank()) {
|
||||
builder.query(query); // on laisse Angular décider si besoin
|
||||
builder.query(query);
|
||||
}
|
||||
|
||||
String url = builder.build(true).toUriString();
|
||||
log.info("[PrestaShop] POST {}");
|
||||
log.info("[PrestaShop] POST {}", url);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||
headers.setContentType(MediaType.APPLICATION_XML); // <-- XML obligatoire
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
||||
headers.setContentType(MediaType.APPLICATION_XML); // XML obligatoire
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>(xmlBody, headers);
|
||||
|
||||
@@ -87,7 +102,8 @@ public class PrestashopClient {
|
||||
entity,
|
||||
String.class
|
||||
);
|
||||
log.info("[PrestaShop] Réponse POST {} pour {}");
|
||||
log.info("[PrestaShop] Réponse POST {} pour {}", response.getStatusCode(), url);
|
||||
|
||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||
+ response.getStatusCode() + " for URL " + url);
|
||||
@@ -108,12 +124,12 @@ public class PrestashopClient {
|
||||
}
|
||||
|
||||
String url = builder.build(true).toUriString();
|
||||
log.info("[PrestaShop] PUT {}");
|
||||
log.info("[PrestaShop] PUT {}", url);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||
headers.setContentType(MediaType.APPLICATION_XML); // <-- XML
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
||||
headers.setContentType(MediaType.APPLICATION_XML);
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>(xmlBody, headers);
|
||||
|
||||
@@ -123,7 +139,8 @@ public class PrestashopClient {
|
||||
entity,
|
||||
String.class
|
||||
);
|
||||
log.info("[PrestaShop] Réponse PUT {} pour {}");
|
||||
log.info("[PrestaShop] Réponse PUT {} pour {}", response.getStatusCode(), url);
|
||||
|
||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||
+ response.getStatusCode() + " for URL " + url);
|
||||
@@ -144,7 +161,7 @@ public class PrestashopClient {
|
||||
}
|
||||
|
||||
String url = builder.build(true).toUriString();
|
||||
log.info("[PrestaShop] DELETE {}");
|
||||
log.info("[PrestaShop] DELETE {}", url);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||
@@ -157,7 +174,8 @@ public class PrestashopClient {
|
||||
entity,
|
||||
String.class
|
||||
);
|
||||
log.info("[PrestaShop] Réponse DELETE {} pour {}");
|
||||
log.info("[PrestaShop] Réponse DELETE {} pour {}", response.getStatusCode(), url);
|
||||
|
||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||
+ response.getStatusCode() + " for URL " + url);
|
||||
|
||||
Reference in New Issue
Block a user