Refactor URI building in PrestashopClient to simplify query parameter encoding

This commit is contained in:
Vincent Guillet
2025-12-02 16:54:49 +01:00
parent bceedc8620
commit 177eb2eb5c

View File

@@ -43,48 +43,36 @@ public class PrestashopClient {
*/
private String buildUri(String path, MultiValueMap<String, String> params) {
StringBuilder sb = new StringBuilder();
// baseUrl (ex: https://shop.gameovergne.fr/api)
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);
}
if (path.charAt(0) == '/') sb.append(path);
else sb.append('/').append(path);
}
// 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;
}
String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8);
if (values == null || values.isEmpty()) continue;
for (String rawValue : values) {
if (first) {
sb.append('?');
first = false;
} else {
sb.append('&');
}
if (first) { sb.append('?'); first = false; }
else sb.append('&');
String encodedValue = rawValue == null
? ""
: URLEncoder.encode(rawValue, StandardCharsets.UTF_8);
// encode la clé
sb.append(URLEncoder.encode(key, StandardCharsets.UTF_8));
sb.append('=');
sb.append(encodedKey)
.append('=')
.append(encodedValue);
// on encode seulement les parties sensibles, PAS les crochets ni virgules
String safeValue = rawValue
.replace("[", "%5B")
.replace("]", "%5D")
.replace(" ", "")
.replace(",", ","); // laisse la virgule brute
sb.append(safeValue);
}
}
}