Enhance PrestashopClient and PrestashopProxyController with new proxy method and improved response handling

This commit is contained in:
Vincent Guillet
2025-11-29 11:32:41 +01:00
parent aead87b1bc
commit 007cb34c81
2 changed files with 32 additions and 2 deletions

View File

@@ -32,6 +32,7 @@ public class PrestashopProxyController {
String relativePath = new AntPathMatcher()
.extractPathWithinPattern(bestMatchPattern, fullPath);
// Path Presta : "/categories" ou "/manufacturers/3" etc.
String path = relativePath.isEmpty() ? "/" : "/" + relativePath;
// Query string brute, déjà encodée (display=%5Bid,name,active%5D&output_format=JSON)
@@ -39,10 +40,15 @@ public class PrestashopProxyController {
var prestaResponse = prestashopClient.getWithRawQuery(path, rawQuery);
// On renvoie EXACTEMENT le même status + body, en forçant JSON
// On propage le status + body, et on garde le Content-Type renvoyé par Presta
MediaType contentType = prestaResponse.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_JSON;
}
return ResponseEntity
.status(prestaResponse.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.contentType(contentType)
.body(prestaResponse.getBody());
}
}

View File

@@ -1,3 +1,4 @@
// File: src/main/java/fr/gameovergne/api/service/PrestashopClient.java
package fr.gameovergne.api.service;
import lombok.extern.slf4j.Slf4j;
@@ -44,6 +45,8 @@ public class PrestashopClient {
return builder.build(true).toUriString();
}
// -------- Méthodes "typed" JSON / XML utilisées par ps-admin --------
public String getJson(String path, MultiValueMap<String, String> params) {
String uri = buildUri(path, params);
log.info("[PrestaShop] GET JSON {}", uri);
@@ -94,4 +97,25 @@ public class PrestashopClient {
.retrieve()
.toBodilessEntity();
}
// -------- Méthode générique utilisée par le proxy /api/ps/** --------
/**
* Proxy brut : on lui donne le path Presta (ex: "/categories") et la query string déjà encodée.
* On récupère un ResponseEntity<String> pour pouvoir propager le status code.
*/
public ResponseEntity<String> getWithRawQuery(String path, String rawQuery) {
String uri = baseUrl + path;
if (rawQuery != null && !rawQuery.isBlank()) {
uri = uri + "?" + rawQuery;
}
log.info("[PrestaShop] GET (proxy) {}", uri);
return client.get()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(String.class);
}
}