Refactor PrestashopClient and PrestashopProxyController to enhance API request handling and simplify query processing

This commit is contained in:
Vincent Guillet
2025-11-29 09:04:47 +01:00
parent d802418c29
commit 9d2f89f805
3 changed files with 41 additions and 56 deletions

View File

@@ -7,9 +7,6 @@ import org.springframework.web.bind.annotation.*;
import fr.gameovergne.api.service.PrestashopClient; import fr.gameovergne.api.service.PrestashopClient;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/api/ps") @RequestMapping("/api/ps")
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -22,23 +19,7 @@ public class PrestashopProxyController {
@PathVariable String resource, @PathVariable String resource,
@RequestParam MultiValueMap<String, String> params @RequestParam MultiValueMap<String, String> params
) { ) {
String query = buildQuery(params); String body = prestashopClient.get(resource, params);
String path = "/" + resource + query;
String body = prestashopClient.get(path);
return ResponseEntity.ok(body); return ResponseEntity.ok(body);
} }
private String buildQuery(MultiValueMap<String, String> params) {
if (params == null || params.isEmpty()) {
return "";
}
String queryString = params.entrySet().stream()
.flatMap(e -> e.getValue().stream()
.map(v -> e.getKey() + "=" + v))
.collect(Collectors.joining("&"));
return "?" + queryString;
}
} }

View File

@@ -2,60 +2,64 @@ package fr.gameovergne.api.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@Slf4j
@Service @Service
@Slf4j
public class PrestashopClient { public class PrestashopClient {
private final RestTemplate restTemplate; private final RestTemplate restTemplate = new RestTemplate();
private final String baseUrl; private final String baseUrl;
private final String apiKey;
public PrestashopClient( public PrestashopClient(
@Value("${prestashop.base-url}") String baseUrl, @Value("${prestashop.base-url}") String baseUrl,
@Value("${prestashop.api-key}") String rawApiKey, @Value("${prestashop.api-key}") String apiKey
RestTemplateBuilder builder
) { ) {
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.apiKey = apiKey;
String apiKey = rawApiKey == null ? null : rawApiKey.trim(); log.info("[PrestaShop] API key length = {}", apiKey != null ? apiKey.length() : 0);
if (apiKey != null && apiKey.length() >= 4) {
if (apiKey == null || apiKey.isBlank()) { log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
throw new IllegalStateException(
"PrestaShop API key is null/blank (prestashop.api-key)."
);
} }
log.info("[PrestaShop] Base URL = {}", this.baseUrl);
log.info("[PrestaShop] API key length = {}", apiKey.length());
log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
this.restTemplate = builder
.basicAuthentication(apiKey, "")
.build();
} }
public String get(String pathAndQuery) { public String get(String resource, MultiValueMap<String, String> params) {
String url; UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(baseUrl)
.pathSegment(resource);
if (pathAndQuery.startsWith("http://") || pathAndQuery.startsWith("https://")) { if (params != null) {
url = pathAndQuery; params.forEach((key, values) -> values.forEach(v -> builder.queryParam(key, v)));
} else if (pathAndQuery.startsWith("/")) {
url = baseUrl + pathAndQuery;
} else {
url = baseUrl + "/" + pathAndQuery;
} }
log.info("[PrestaShop] GET {}", url); URI uri = builder.build(true).encode().toUri();
return restTemplate log.info("[PrestaShop] GET {}", uri);
.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, String.class)
.getBody(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
// Auth Basic : "KEY:" base64
String auth = apiKey + ":";
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth);
HttpEntity<Void> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
return response.getBody();
} }
} }

View File

@@ -12,5 +12,5 @@ spring.jpa.show-sql=true
jwt.secret=a23ac96ce968bf13099d99410b951dd498118851bdfc996a3f844bd68b1b2afd jwt.secret=a23ac96ce968bf13099d99410b951dd498118851bdfc996a3f844bd68b1b2afd
prestashop.base-url=https://shop.gameovergne.fr prestashop.base-url=https://shop.gameovergne.fr/api
prestashop.api-key=${PRESTASHOP_API_KEY} prestashop.api-key=${PRESTASHOP_API_KEY}