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;
|
this.prestashopClient = prestashopClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractPath(HttpServletRequest request) {
|
// ---------- utilitaire pour extraire le path Presta ----------
|
||||||
return request.getRequestURI().replaceFirst("^/api/ps", ""); // => /products, /categories, ...
|
|
||||||
|
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("/**")
|
@GetMapping("/**")
|
||||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||||
String path = extractPath(request);
|
String path = extractPrestaPath(request); // ex: "/categories"
|
||||||
String query = request.getQueryString();
|
String query = request.getQueryString(); // ex: "display=[id,name]&output_format=JSON"
|
||||||
|
|
||||||
String body = prestashopClient.get(path, query);
|
String body = prestashopClient.get(path, query);
|
||||||
|
|
||||||
|
// Presta renvoie du JSON (output_format=JSON), donc on force application/json
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.body(body);
|
.body(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- POST ----------
|
// ---------- POST : /api/ps/** -> Presta POST ----------
|
||||||
|
|
||||||
@PostMapping("/**")
|
@PostMapping("/**")
|
||||||
public ResponseEntity<String> proxyPost(HttpServletRequest request,
|
public ResponseEntity<String> proxyPost(HttpServletRequest request,
|
||||||
@RequestBody String xmlBody) {
|
@RequestBody(required = false) String xmlBody) {
|
||||||
String path = extractPath(request);
|
String path = extractPrestaPath(request);
|
||||||
String query = request.getQueryString();
|
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
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
.body(body);
|
.body(responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- PUT ----------
|
// ---------- PUT : /api/ps/** -> Presta PUT ----------
|
||||||
|
|
||||||
@PutMapping("/**")
|
@PutMapping("/**")
|
||||||
public ResponseEntity<String> proxyPut(HttpServletRequest request,
|
public ResponseEntity<String> proxyPut(HttpServletRequest request,
|
||||||
@RequestBody String xmlBody) {
|
@RequestBody(required = false) String xmlBody) {
|
||||||
String path = extractPath(request);
|
String path = extractPrestaPath(request);
|
||||||
String query = request.getQueryString();
|
String query = request.getQueryString();
|
||||||
String body = prestashopClient.put(path, query, xmlBody);
|
|
||||||
|
String responseBody = prestashopClient.put(path, query, xmlBody != null ? xmlBody : "");
|
||||||
|
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
.body(body);
|
.body(responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- DELETE ----------
|
// ---------- DELETE : /api/ps/** -> Presta DELETE ----------
|
||||||
|
|
||||||
@DeleteMapping("/**")
|
@DeleteMapping("/**")
|
||||||
public ResponseEntity<String> proxyDelete(HttpServletRequest request) {
|
public ResponseEntity<String> proxyDelete(HttpServletRequest request) {
|
||||||
String path = extractPath(request);
|
String path = extractPrestaPath(request);
|
||||||
String query = request.getQueryString();
|
String query = request.getQueryString();
|
||||||
String body = prestashopClient.delete(path, query);
|
|
||||||
|
String responseBody = prestashopClient.delete(path, query);
|
||||||
|
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
.body(body);
|
.body(responseBody);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,20 +28,33 @@ public class PrestashopClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ========== GET ==========
|
// ========== 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
|
UriComponentsBuilder builder = UriComponentsBuilder
|
||||||
.fromHttpUrl(baseUrl)
|
.fromHttpUrl(baseUrl)
|
||||||
.path("/api")
|
.path("/api")
|
||||||
.path(path)
|
.path(path);
|
||||||
.queryParam("output_format", "JSON")
|
|
||||||
.queryParam("display", "full");
|
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();
|
String url = builder.build(true).toUriString();
|
||||||
log.info("[PrestaShop] GET {}");
|
log.info("[PrestaShop] GET {}", url);
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
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);
|
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
@@ -51,7 +64,9 @@ public class PrestashopClient {
|
|||||||
entity,
|
entity,
|
||||||
String.class
|
String.class
|
||||||
);
|
);
|
||||||
log.info("[PrestaShop] Réponse GET {} pour {}");
|
|
||||||
|
log.info("[PrestaShop] Réponse GET {} pour {}", response.getStatusCode(), url);
|
||||||
|
|
||||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||||
+ response.getStatusCode() + " for URL " + url);
|
+ response.getStatusCode() + " for URL " + url);
|
||||||
@@ -68,16 +83,16 @@ public class PrestashopClient {
|
|||||||
.path(path);
|
.path(path);
|
||||||
|
|
||||||
if (query != null && !query.isBlank()) {
|
if (query != null && !query.isBlank()) {
|
||||||
builder.query(query); // on laisse Angular décider si besoin
|
builder.query(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = builder.build(true).toUriString();
|
String url = builder.build(true).toUriString();
|
||||||
log.info("[PrestaShop] POST {}");
|
log.info("[PrestaShop] POST {}", url);
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||||
headers.setContentType(MediaType.APPLICATION_XML); // <-- XML obligatoire
|
headers.setContentType(MediaType.APPLICATION_XML); // XML obligatoire
|
||||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
|
||||||
|
|
||||||
HttpEntity<String> entity = new HttpEntity<>(xmlBody, headers);
|
HttpEntity<String> entity = new HttpEntity<>(xmlBody, headers);
|
||||||
|
|
||||||
@@ -87,7 +102,8 @@ public class PrestashopClient {
|
|||||||
entity,
|
entity,
|
||||||
String.class
|
String.class
|
||||||
);
|
);
|
||||||
log.info("[PrestaShop] Réponse POST {} pour {}");
|
log.info("[PrestaShop] Réponse POST {} pour {}", response.getStatusCode(), url);
|
||||||
|
|
||||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||||
+ response.getStatusCode() + " for URL " + url);
|
+ response.getStatusCode() + " for URL " + url);
|
||||||
@@ -108,12 +124,12 @@ public class PrestashopClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String url = builder.build(true).toUriString();
|
String url = builder.build(true).toUriString();
|
||||||
log.info("[PrestaShop] PUT {}");
|
log.info("[PrestaShop] PUT {}", url);
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||||
headers.setContentType(MediaType.APPLICATION_XML); // <-- XML
|
headers.setContentType(MediaType.APPLICATION_XML);
|
||||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
|
||||||
|
|
||||||
HttpEntity<String> entity = new HttpEntity<>(xmlBody, headers);
|
HttpEntity<String> entity = new HttpEntity<>(xmlBody, headers);
|
||||||
|
|
||||||
@@ -123,7 +139,8 @@ public class PrestashopClient {
|
|||||||
entity,
|
entity,
|
||||||
String.class
|
String.class
|
||||||
);
|
);
|
||||||
log.info("[PrestaShop] Réponse PUT {} pour {}");
|
log.info("[PrestaShop] Réponse PUT {} pour {}", response.getStatusCode(), url);
|
||||||
|
|
||||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||||
+ response.getStatusCode() + " for URL " + url);
|
+ response.getStatusCode() + " for URL " + url);
|
||||||
@@ -144,7 +161,7 @@ public class PrestashopClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String url = builder.build(true).toUriString();
|
String url = builder.build(true).toUriString();
|
||||||
log.info("[PrestaShop] DELETE {}");
|
log.info("[PrestaShop] DELETE {}", url);
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||||
@@ -157,7 +174,8 @@ public class PrestashopClient {
|
|||||||
entity,
|
entity,
|
||||||
String.class
|
String.class
|
||||||
);
|
);
|
||||||
log.info("[PrestaShop] Réponse DELETE {} pour {}");
|
log.info("[PrestaShop] Réponse DELETE {} pour {}", response.getStatusCode(), url);
|
||||||
|
|
||||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||||
+ response.getStatusCode() + " for URL " + url);
|
+ response.getStatusCode() + " for URL " + url);
|
||||||
|
|||||||
Reference in New Issue
Block a user