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;
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 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
@RequestMapping("/api/ps")
@RequiredArgsConstructor
public class PrestashopProxyController {
private final PrestashopClient prestashopClient;
@GetMapping("/{resource}")
public ResponseEntity<String> proxyGet(
@PathVariable String resource,
@RequestParam MultiValueMap<String, String> params
) {
String body = prestashopClient.get(resource, params);
return ResponseEntity.ok(body);
public PrestashopProxyController(PrestashopClient prestashopClient) {
this.prestashopClient = prestashopClient;
}
@GetMapping("/**")
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
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;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@Service
@Slf4j
public class PrestashopClient {
private final RestTemplate restTemplate = new RestTemplate();
private final String baseUrl;
private final String apiKey;
private final org.springframework.web.client.RestTemplate restTemplate =
new org.springframework.web.client.RestTemplate();
public PrestashopClient(
@Value("${prestashop.base-url}") String baseUrl,
@Value("${prestashop.api-key}") String apiKey
@Value("${prestashop.api.base-url}") String baseUrl,
@Value("${prestashop.api.key}") String apiKey
) {
this.baseUrl = baseUrl;
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
.fromHttpUrl(baseUrl)
.pathSegment(resource);
.path(relativePath);
if (params != null) {
params.forEach((key, values) -> values.forEach(v -> builder.queryParam(key, v)));
if (StringUtils.hasText(rawQuery)) {
builder.query(rawQuery);
}
URI uri = builder.build(true).encode().toUri();
log.info("[PrestaShop] GET {}", uri);
URI uri = builder.build(true).toUri();
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);
headers.setBasicAuth(apiKey, "");
HttpEntity<Void> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
return response.getBody();
return restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
}
}