Update api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java
This commit is contained in:
@@ -17,30 +17,28 @@ public class PrestashopClient {
|
|||||||
private final RestTemplate restTemplate = new RestTemplate();
|
private final RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
private final String basicAuth; // valeur déjà encodée Base64 (sans le "Basic ")
|
private final String basicAuth; // base64 SANS le "Basic "
|
||||||
|
|
||||||
public PrestashopClient(
|
public PrestashopClient(
|
||||||
@Value("${prestashop.base-url}") String baseUrl,
|
@Value("${prestashop.base-url}") String baseUrl,
|
||||||
@Value("${prestashop.basic-auth}") String basicAuth) {
|
@Value("${prestashop.basic-auth}") String basicAuth
|
||||||
|
) {
|
||||||
this.baseUrl = baseUrl;
|
this.baseUrl = baseUrl;
|
||||||
this.basicAuth = basicAuth;
|
this.basicAuth = basicAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appel GET générique sur l'API PrestaShop.
|
* Appel générique GET vers PrestaShop.
|
||||||
*
|
* On ignore complètement la query reçue du front et on force output_format=JSON.
|
||||||
* @param path ex: "/categories"
|
|
||||||
* @param query ex: "display=%5Bid,name,active%5D&output_format=JSON" ou null
|
|
||||||
* @return corps de la réponse JSON brute
|
|
||||||
*/
|
*/
|
||||||
public String get(String path, String query) {
|
public String get(String path, String ignoredQuery) {
|
||||||
String url = UriComponentsBuilder
|
UriComponentsBuilder builder = UriComponentsBuilder
|
||||||
.fromHttpUrl(baseUrl)
|
.fromHttpUrl(baseUrl)
|
||||||
.path("/api")
|
.path("/api")
|
||||||
.path(path)
|
.path(path)
|
||||||
.query(query)
|
.queryParam("output_format", "JSON");
|
||||||
.build(true) // true = ne ré-encode pas les caractères déjà encodés (%5B ...)
|
|
||||||
.toUriString();
|
String url = builder.build(true).toUriString();
|
||||||
|
|
||||||
log.info("[PrestaShop] Appel URL = {}", url);
|
log.info("[PrestaShop] Appel URL = {}", url);
|
||||||
|
|
||||||
@@ -59,9 +57,9 @@ public class PrestashopClient {
|
|||||||
|
|
||||||
log.info("[PrestaShop] Réponse HTTP {} pour {}", response.getStatusCode(), url);
|
log.info("[PrestaShop] Réponse HTTP {} pour {}", response.getStatusCode(), url);
|
||||||
|
|
||||||
// Si Presta renvoie autre chose que 2xx, on propage une 502 au front
|
|
||||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("PrestaShop returned non-2xx status: " + response.getStatusCode());
|
throw new RuntimeException("PrestaShop returned non-2xx status: "
|
||||||
|
+ response.getStatusCode() + " for URL " + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.getBody();
|
return response.getBody();
|
||||||
|
|||||||
Reference in New Issue
Block a user