Refactor PrestashopClient to store basic auth header for improved readability and maintainability

This commit is contained in:
Vincent Guillet
2025-12-03 15:32:44 +01:00
parent 503cbee641
commit b15c331295

View File

@@ -30,6 +30,7 @@ public class PrestashopClient {
private final RestClient client; private final RestClient client;
private final String baseUrl; private final String baseUrl;
private final String basicAuthHeader;
public PrestashopClient( public PrestashopClient(
@Value("${prestashop.base-url}") String baseUrl, @Value("${prestashop.base-url}") String baseUrl,
@@ -40,8 +41,10 @@ public class PrestashopClient {
String basicAuth = Base64.getEncoder() String basicAuth = Base64.getEncoder()
.encodeToString((apiKey + ":").getBytes(StandardCharsets.UTF_8)); .encodeToString((apiKey + ":").getBytes(StandardCharsets.UTF_8));
this.basicAuthHeader = "Basic " + basicAuth; // <--- mémorisé pour HttpURLConnection
this.client = RestClient.builder() this.client = RestClient.builder()
.defaultHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth) .defaultHeader(HttpHeaders.AUTHORIZATION, basicAuthHeader)
.build(); .build();
log.info("[PrestaShop] Base URL = {}", baseUrl); log.info("[PrestaShop] Base URL = {}", baseUrl);
@@ -293,6 +296,7 @@ public class PrestashopClient {
byte[] multipartBytes = baos.toByteArray(); byte[] multipartBytes = baos.toByteArray();
// -------- Envoi via HttpURLConnection --------
// -------- Envoi via HttpURLConnection -------- // -------- Envoi via HttpURLConnection --------
URL targetUrl = URI.create(url).toURL(); URL targetUrl = URI.create(url).toURL();
HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection(); HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection();
@@ -300,6 +304,7 @@ public class PrestashopClient {
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("Accept", "application/xml, text/xml, */*;q=0.1"); conn.setRequestProperty("Accept", "application/xml, text/xml, */*;q=0.1");
conn.setRequestProperty("Authorization", basicAuthHeader);
// Important : pas de chunked, on envoie une taille fixe // Important : pas de chunked, on envoie une taille fixe
conn.setFixedLengthStreamingMode(multipartBytes.length); conn.setFixedLengthStreamingMode(multipartBytes.length);