Update api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java
This commit is contained in:
@@ -1,61 +1,72 @@
|
|||||||
// FILE: api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java
|
// FILE: api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java
|
||||||
package fr.gameovergne.api.service.prestashop;
|
package fr.gameovergne.api.service.prestashop;
|
||||||
|
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.web.client.HttpStatusCodeException;
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class PrestashopClient {
|
public class PrestashopClient {
|
||||||
|
|
||||||
private final RestTemplate restTemplate = new RestTemplate();
|
private static final Logger log = LoggerFactory.getLogger(PrestashopClient.class);
|
||||||
private final String baseUrl;
|
|
||||||
private final String basicAuth;
|
|
||||||
|
|
||||||
public PrestashopClient(
|
private final RestTemplate restTemplate = new RestTemplate();
|
||||||
@Value("${prestashop.base-url}") String baseUrl,
|
|
||||||
@Value("${prestashop.basic-auth}") String basicAuth
|
@Value("${prestashop.base-url}")
|
||||||
) {
|
private String baseUrl;
|
||||||
this.baseUrl = baseUrl;
|
|
||||||
this.basicAuth = basicAuth;
|
@Value("${prestashop.basic-auth}")
|
||||||
}
|
private String basicAuth;
|
||||||
|
|
||||||
public ResponseEntity<String> get(String path, String query) {
|
public ResponseEntity<String> get(String path, String query) {
|
||||||
|
try {
|
||||||
|
String url = baseUrl + "/api" + path;
|
||||||
|
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder
|
UriComponentsBuilder builder = UriComponentsBuilder
|
||||||
.fromHttpUrl(baseUrl)
|
.fromHttpUrl(url);
|
||||||
.path("/api")
|
|
||||||
.path(path); // ex: /suppliers
|
|
||||||
|
|
||||||
if (query != null && !query.isBlank()) {
|
if (query != null && !query.isBlank()) {
|
||||||
// On parse la query reçue de l'appel Angular
|
// on réinjecte TELS QUELS les query params venant du front
|
||||||
MultiValueMap<String, String> params =
|
builder.query(query);
|
||||||
UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
|
|
||||||
|
|
||||||
// On supprime output_format côté Presta : on laisse l'en-tête Accept gérer le JSON
|
|
||||||
params.remove("output_format");
|
|
||||||
|
|
||||||
// On réapplique tous les autres paramètres (display, filter[...], etc.)
|
|
||||||
for (var entry : params.entrySet()) {
|
|
||||||
for (String v : entry.getValue()) {
|
|
||||||
builder.queryParam(entry.getKey(), v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
URI uri = builder.build(true).toUri(); // true = les valeurs sont déjà encodées si besoin
|
String finalUrl = builder.build(true).toUriString();
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set("Authorization", basicAuth);
|
headers.set("Authorization", basicAuth);
|
||||||
headers.setAccept(java.util.List.of(MediaType.APPLICATION_JSON));
|
headers.setAccept(MediaType.parseMediaTypes("application/json"));
|
||||||
|
|
||||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
return restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
|
log.debug("[Presta] GET {}", finalUrl);
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
finalUrl,
|
||||||
|
HttpMethod.GET,
|
||||||
|
entity,
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
log.debug("[Presta] status={} body={}", response.getStatusCode(), response.getBody());
|
||||||
|
return ResponseEntity.status(response.getStatusCode()).body(response.getBody());
|
||||||
|
|
||||||
|
} catch (HttpStatusCodeException e) {
|
||||||
|
// <-- ICI on garde le vrai status + body de Prestashop
|
||||||
|
log.error("[Presta] HTTP error {} body={}", e.getStatusCode(), e.getResponseBodyAsString());
|
||||||
|
return ResponseEntity
|
||||||
|
.status(e.getStatusCode())
|
||||||
|
.body(e.getResponseBodyAsString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("[Presta] Unexpected error while calling Presta", e);
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.body("{\"error\":\"Unexpected error while calling Prestashop\"}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user