Refactor PrestashopClient and PrestashopProxyController for improved API handling
This commit is contained in:
@@ -16,30 +16,38 @@ public class PrestashopProxyController {
|
|||||||
this.prestashopClient = prestashopClient;
|
this.prestashopClient = prestashopClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private String extractPath(HttpServletRequest request) {
|
||||||
* Proxy GET sur /api/ps/** -> /api/** sur PrestaShop
|
return request.getRequestURI().replaceFirst("^/api/ps", "");
|
||||||
* Exemple front :
|
}
|
||||||
* /api/ps/categories?display=[id,name,active]&output_format=JSON
|
|
||||||
* deviendra côté Presta :
|
// ---------- GET ----------
|
||||||
* https://shop.gameovergne.fr/api/categories?display=[id,name,active]&output_format=JSON
|
|
||||||
*/
|
|
||||||
@GetMapping("/**")
|
@GetMapping("/**")
|
||||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||||
String fullPath = request.getRequestURI(); // ex: /api/ps/categories
|
String path = extractPath(request);
|
||||||
String contextPath = request.getContextPath(); // souvent ""
|
return ResponseEntity.ok(prestashopClient.get(path));
|
||||||
String relative = fullPath.substring(contextPath.length()); // /api/ps/categories
|
}
|
||||||
|
|
||||||
// On enlève le préfixe /api/ps => /categories
|
// ---------- POST ----------
|
||||||
String path = relative.replaceFirst("^/api/ps", "");
|
@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);
|
// ---------- DELETE ----------
|
||||||
|
@DeleteMapping("/**")
|
||||||
// On renvoie du JSON brut comme Presta, en HTTP 200
|
public ResponseEntity<Void> proxyDelete(HttpServletRequest request) {
|
||||||
return ResponseEntity
|
String path = extractPath(request);
|
||||||
.ok()
|
prestashopClient.delete(path);
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
return ResponseEntity.noContent().build();
|
||||||
.body(body);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,9 +15,8 @@ public class PrestashopClient {
|
|||||||
private static final Logger log = LoggerFactory.getLogger(PrestashopClient.class);
|
private static final Logger log = LoggerFactory.getLogger(PrestashopClient.class);
|
||||||
|
|
||||||
private final RestTemplate restTemplate = new RestTemplate();
|
private final RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
private final String basicAuth; // base64 SANS le "Basic "
|
private final String basicAuth;
|
||||||
|
|
||||||
public PrestashopClient(
|
public PrestashopClient(
|
||||||
@Value("${prestashop.base-url}") String baseUrl,
|
@Value("${prestashop.base-url}") String baseUrl,
|
||||||
@@ -27,46 +26,100 @@ public class PrestashopClient {
|
|||||||
this.basicAuth = basicAuth;
|
this.basicAuth = basicAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private String buildUrl(String path) {
|
||||||
* Appel générique GET vers PrestaShop.
|
return UriComponentsBuilder
|
||||||
* 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
|
|
||||||
.fromHttpUrl(baseUrl)
|
.fromHttpUrl(baseUrl)
|
||||||
.path("/api")
|
.path("/api")
|
||||||
.path(path)
|
.path(path)
|
||||||
.queryParam("output_format", "JSON")
|
.queryParam("output_format", "JSON")
|
||||||
.queryParam("display", "full");
|
.queryParam("display", "full")
|
||||||
|
.build(true)
|
||||||
String url = builder.build(true).toUriString();
|
.toUriString();
|
||||||
|
}
|
||||||
log.info("[PrestaShop] Appel URL = {}", url);
|
|
||||||
|
|
||||||
|
private HttpHeaders buildHeaders() {
|
||||||
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); // 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 {
|
try {
|
||||||
ResponseEntity<String> response = restTemplate.exchange(
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
url,
|
url,
|
||||||
HttpMethod.GET,
|
HttpMethod.GET,
|
||||||
entity,
|
new HttpEntity<>(buildHeaders()),
|
||||||
String.class
|
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()) {
|
// ========== POST ==========
|
||||||
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
public String post(String path, String xmlBody) {
|
||||||
+ response.getStatusCode() + " for URL " + url);
|
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();
|
return response.getBody();
|
||||||
} catch (RestClientException e) {
|
} catch (RestClientException e) {
|
||||||
log.error("[PrestaShop] Erreur lors de l'appel à {}", url, e);
|
log.error("[PrestaShop] Erreur POST {}", url, e);
|
||||||
throw new RuntimeException("Erreur lors de l'appel à PrestaShop", 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