Refactor PrestashopClient and PrestashopProxyController for improved API handling
This commit is contained in:
@@ -16,30 +16,38 @@ public class PrestashopProxyController {
|
||||
this.prestashopClient = prestashopClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy GET sur /api/ps/** -> /api/** sur PrestaShop
|
||||
* Exemple front :
|
||||
* /api/ps/categories?display=[id,name,active]&output_format=JSON
|
||||
* deviendra côté Presta :
|
||||
* https://shop.gameovergne.fr/api/categories?display=[id,name,active]&output_format=JSON
|
||||
*/
|
||||
private String extractPath(HttpServletRequest request) {
|
||||
return request.getRequestURI().replaceFirst("^/api/ps", "");
|
||||
}
|
||||
|
||||
// ---------- GET ----------
|
||||
@GetMapping("/**")
|
||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||
String fullPath = request.getRequestURI(); // ex: /api/ps/categories
|
||||
String contextPath = request.getContextPath(); // souvent ""
|
||||
String relative = fullPath.substring(contextPath.length()); // /api/ps/categories
|
||||
String path = extractPath(request);
|
||||
return ResponseEntity.ok(prestashopClient.get(path));
|
||||
}
|
||||
|
||||
// On enlève le préfixe /api/ps => /categories
|
||||
String path = relative.replaceFirst("^/api/ps", "");
|
||||
// ---------- POST ----------
|
||||
@PostMapping("/**")
|
||||
public ResponseEntity<String> proxyPost(HttpServletRequest request, @RequestBody String xmlBody) {
|
||||
String path = extractPath(request);
|
||||
String body = prestashopClient.post(path, xmlBody);
|
||||
return ResponseEntity.ok(body);
|
||||
}
|
||||
|
||||
String query = request.getQueryString(); // ex: display=%5Bid,name,active%5D&output_format=JSON
|
||||
// ---------- PUT ----------
|
||||
@PutMapping("/**")
|
||||
public ResponseEntity<String> proxyPut(HttpServletRequest request, @RequestBody String xmlBody) {
|
||||
String path = extractPath(request);
|
||||
String body = prestashopClient.put(path, xmlBody);
|
||||
return ResponseEntity.ok(body);
|
||||
}
|
||||
|
||||
String body = prestashopClient.get(path, query);
|
||||
|
||||
// On renvoie du JSON brut comme Presta, en HTTP 200
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(body);
|
||||
// ---------- DELETE ----------
|
||||
@DeleteMapping("/**")
|
||||
public ResponseEntity<Void> proxyDelete(HttpServletRequest request) {
|
||||
String path = extractPath(request);
|
||||
prestashopClient.delete(path);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,8 @@ public class PrestashopClient {
|
||||
private static final Logger log = LoggerFactory.getLogger(PrestashopClient.class);
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
private final String baseUrl;
|
||||
private final String basicAuth; // base64 SANS le "Basic "
|
||||
private final String basicAuth;
|
||||
|
||||
public PrestashopClient(
|
||||
@Value("${prestashop.base-url}") String baseUrl,
|
||||
@@ -27,46 +26,100 @@ public class PrestashopClient {
|
||||
this.basicAuth = basicAuth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appel générique GET vers PrestaShop.
|
||||
* On ignore complètement la query reçue du front et on force output_format=JSON.
|
||||
*/
|
||||
public String get(String path, String ignoredQuery) {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder
|
||||
private String buildUrl(String path) {
|
||||
return UriComponentsBuilder
|
||||
.fromHttpUrl(baseUrl)
|
||||
.path("/api")
|
||||
.path(path)
|
||||
.queryParam("output_format", "JSON")
|
||||
.queryParam("display", "full");
|
||||
|
||||
String url = builder.build(true).toUriString();
|
||||
|
||||
log.info("[PrestaShop] Appel URL = {}", url);
|
||||
.queryParam("display", "full")
|
||||
.build(true)
|
||||
.toUriString();
|
||||
}
|
||||
|
||||
private HttpHeaders buildHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||
headers.setContentType(MediaType.APPLICATION_XML); // Presta exige XML pour POST/PUT
|
||||
headers.setAccept(java.util.List.of(MediaType.APPLICATION_JSON));
|
||||
return headers;
|
||||
}
|
||||
|
||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||
// ========== GET ==========
|
||||
public String get(String path) {
|
||||
String url = buildUrl(path);
|
||||
log.info("[PrestaShop] GET {}", url);
|
||||
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
entity,
|
||||
new HttpEntity<>(buildHeaders()),
|
||||
String.class
|
||||
);
|
||||
|
||||
log.info("[PrestaShop] Réponse HTTP {} pour {}", response.getStatusCode(), url);
|
||||
return response.getBody();
|
||||
} catch (RestClientException e) {
|
||||
log.error("[PrestaShop] Erreur GET {}", url, e);
|
||||
throw new RuntimeException("Erreur GET PrestaShop", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||
+ response.getStatusCode() + " for URL " + url);
|
||||
}
|
||||
// ========== POST ==========
|
||||
public String post(String path, String xmlBody) {
|
||||
String url = buildUrl(path);
|
||||
log.info("[PrestaShop] POST {}", url);
|
||||
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
new HttpEntity<>(xmlBody, buildHeaders()),
|
||||
String.class
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (RestClientException e) {
|
||||
log.error("[PrestaShop] Erreur lors de l'appel à {}", url, e);
|
||||
throw new RuntimeException("Erreur lors de l'appel à PrestaShop", e);
|
||||
log.error("[PrestaShop] Erreur POST {}", url, e);
|
||||
throw new RuntimeException("Erreur POST PrestaShop", e);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== PUT ==========
|
||||
public String put(String path, String xmlBody) {
|
||||
String url = buildUrl(path);
|
||||
log.info("[PrestaShop] PUT {}", url);
|
||||
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.PUT,
|
||||
new HttpEntity<>(xmlBody, buildHeaders()),
|
||||
String.class
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (RestClientException e) {
|
||||
log.error("[PrestaShop] Erreur PUT {}", url, e);
|
||||
throw new RuntimeException("Erreur PUT PrestaShop", e);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== DELETE ==========
|
||||
public void delete(String path) {
|
||||
String url = buildUrl(path);
|
||||
log.info("[PrestaShop] DELETE {}", url);
|
||||
|
||||
try {
|
||||
restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.DELETE,
|
||||
new HttpEntity<>(buildHeaders()),
|
||||
Void.class
|
||||
);
|
||||
} catch (RestClientException e) {
|
||||
log.error("[PrestaShop] Erreur DELETE {}", url, e);
|
||||
throw new RuntimeException("Erreur DELETE PrestaShop", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user