Refactor PrestashopClient and PrestashopProxyController to improve query handling and simplify request processing

This commit is contained in:
Vincent Guillet
2025-11-29 09:33:10 +01:00
parent 9d2f89f805
commit 9ae60a087a
2 changed files with 37 additions and 44 deletions

View File

@@ -1,25 +1,36 @@
package fr.gameovergne.api.controller; package fr.gameovergne.api.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import fr.gameovergne.api.service.PrestashopClient; import fr.gameovergne.api.service.PrestashopClient;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.util.AntPathMatcher;
@RestController @RestController
@RequestMapping("/api/ps") @RequestMapping("/api/ps")
@RequiredArgsConstructor
public class PrestashopProxyController { public class PrestashopProxyController {
private final PrestashopClient prestashopClient; private final PrestashopClient prestashopClient;
@GetMapping("/{resource}") public PrestashopProxyController(PrestashopClient prestashopClient) {
public ResponseEntity<String> proxyGet( this.prestashopClient = prestashopClient;
@PathVariable String resource, }
@RequestParam MultiValueMap<String, String> params
) { @GetMapping("/**")
String body = prestashopClient.get(resource, params); public ResponseEntity<String> proxyGet(HttpServletRequest request) {
return ResponseEntity.ok(body);
String fullPath = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(
HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String relativePath = new AntPathMatcher()
.extractPathWithinPattern(bestMatchPattern, fullPath);
String path = relativePath.isEmpty() ? "/" : "/" + relativePath;
String rawQuery = request.getQueryString();
return prestashopClient.getWithRawQuery(path, rawQuery);
} }
} }

View File

@@ -1,65 +1,47 @@
package fr.gameovergne.api.service; package fr.gameovergne.api.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI; import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@Service @Service
@Slf4j
public class PrestashopClient { public class PrestashopClient {
private final RestTemplate restTemplate = new RestTemplate();
private final String baseUrl; private final String baseUrl;
private final String apiKey; private final String apiKey;
private final org.springframework.web.client.RestTemplate restTemplate =
new org.springframework.web.client.RestTemplate();
public PrestashopClient( public PrestashopClient(
@Value("${prestashop.base-url}") String baseUrl, @Value("${prestashop.api.base-url}") String baseUrl,
@Value("${prestashop.api-key}") String apiKey @Value("${prestashop.api.key}") String apiKey
) { ) {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.apiKey = apiKey; this.apiKey = apiKey;
log.info("[PrestaShop] API key length = {}", apiKey != null ? apiKey.length() : 0);
if (apiKey != null && apiKey.length() >= 4) {
log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
}
log.info("[PrestaShop] Base URL = {}", this.baseUrl);
} }
public String get(String resource, MultiValueMap<String, String> params) { public ResponseEntity<String> getWithRawQuery(String relativePath, String rawQuery) {
UriComponentsBuilder builder = UriComponentsBuilder UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(baseUrl) .fromHttpUrl(baseUrl)
.pathSegment(resource); .path(relativePath);
if (params != null) { if (StringUtils.hasText(rawQuery)) {
params.forEach((key, values) -> values.forEach(v -> builder.queryParam(key, v))); builder.query(rawQuery);
} }
URI uri = builder.build(true).encode().toUri(); URI uri = builder.build(true).toUri();
log.info("[PrestaShop] GET {}", uri);
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
headers.setBasicAuth(apiKey, "");
// 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); HttpEntity<Void> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); return restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
return response.getBody();
} }
} }