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