Compare commits

..

2 Commits

Author SHA1 Message Date
Vincent Guillet
14a6f66742 Update Prestashop service to return ID for all valid responses 2025-12-02 16:28:38 +01:00
Vincent Guillet
28faf2ed2b Refactor PrestashopClient to improve URI building and parameter encoding 2025-12-02 16:28:27 +01:00
2 changed files with 61 additions and 33 deletions

View File

@@ -7,10 +7,11 @@ import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
@@ -25,7 +26,7 @@ public class PrestashopClient {
) {
this.baseUrl = baseUrl;
String basicAuth = Base64.getEncoder()
String basicAuth = java.util.Base64.getEncoder()
.encodeToString((apiKey + ":").getBytes(StandardCharsets.UTF_8));
this.client = RestClient.builder()
@@ -36,16 +37,64 @@ public class PrestashopClient {
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) {
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(baseUrl + path);
if (params != null && !params.isEmpty()) {
builder.queryParams(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);
}
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) {
String uri = buildUri(path, params);
@@ -97,25 +146,4 @@ public class PrestashopClient {
.retrieve()
.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);
}
}

View File

@@ -60,7 +60,7 @@ export class PrestashopService {
`${this.adminBase}/${resource}`,
{ name }
)
.pipe(map(res => (res && typeof res.id === 'number' ? res.id : null)));
.pipe(map(res => (res && true ? res.id : null)));
}
/**