Refactor PrestashopClient to improve URI building and parameter encoding

This commit is contained in:
Vincent Guillet
2025-12-02 16:28:27 +01:00
parent 02387e9a50
commit 28faf2ed2b

View File

@@ -7,10 +7,11 @@ import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.List;
import java.util.Map;
@Service @Service
@Slf4j @Slf4j
@@ -25,7 +26,7 @@ public class PrestashopClient {
) { ) {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
String basicAuth = Base64.getEncoder() String basicAuth = java.util.Base64.getEncoder()
.encodeToString((apiKey + ":").getBytes(StandardCharsets.UTF_8)); .encodeToString((apiKey + ":").getBytes(StandardCharsets.UTF_8));
this.client = RestClient.builder() this.client = RestClient.builder()
@@ -36,16 +37,64 @@ public class PrestashopClient {
log.info("[PrestaShop] API key length = {}", apiKey.length()); log.info("[PrestaShop] API key length = {}", apiKey.length());
} }
/**
* Construit lURL complète en encodant proprement les query params
* (y compris les crochets utilisés par PrestaShop : [id,name,...]).
*/
private String buildUri(String path, MultiValueMap<String, String> params) { private String buildUri(String path, MultiValueMap<String, String> params) {
UriComponentsBuilder builder = UriComponentsBuilder StringBuilder sb = new StringBuilder();
.fromHttpUrl(baseUrl + path);
if (params != null && !params.isEmpty()) { // baseUrl (ex: https://shop.gameovergne.fr/api)
builder.queryParams(params); sb.append(baseUrl);
// path (on gère proprement le /)
if (path != null && !path.isEmpty()) {
if (path.charAt(0) == '/') {
sb.append(path);
} else {
sb.append('/').append(path);
} }
return builder.build(true).toUriString();
} }
// -------- Méthodes "typed" JSON / XML utilisées par ps-admin -------- // query params
if (params != null && !params.isEmpty()) {
boolean first = true;
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
if (values == null || values.isEmpty()) {
continue;
}
// encode la clé
String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8);
for (String rawValue : values) {
if (first) {
sb.append('?');
first = false;
} else {
sb.append('&');
}
// encode la valeur (crochets, %, espaces, etc.)
String encodedValue = rawValue == null
? ""
: URLEncoder.encode(rawValue, StandardCharsets.UTF_8);
sb.append(encodedKey)
.append('=')
.append(encodedValue);
}
}
}
String finalUri = sb.toString();
log.info("[PrestaShop] built URI = {}", finalUri);
return finalUri;
}
public String getJson(String path, MultiValueMap<String, String> params) { public String getJson(String path, MultiValueMap<String, String> params) {
String uri = buildUri(path, params); String uri = buildUri(path, params);
@@ -97,25 +146,4 @@ public class PrestashopClient {
.retrieve() .retrieve()
.toBodilessEntity(); .toBodilessEntity();
} }
// -------- Méthode générique utilisée par le proxy /api/ps/** --------
/**
* Proxy brut : on lui donne le path Presta (ex: "/categories") et la query string déjà encodée.
* On récupère un ResponseEntity<String> pour pouvoir propager le status code.
*/
public ResponseEntity<String> getWithRawQuery(String path, String rawQuery) {
String uri = baseUrl + path;
if (rawQuery != null && !rawQuery.isBlank()) {
uri = uri + "?" + rawQuery;
}
log.info("[PrestaShop] GET (proxy) {}", uri);
return client.get()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(String.class);
}
} }