Refactor PrestashopClient and PrestashopProxyController to enhance API request handling and improve error logging
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
// File: src/main/java/fr/gameovergne/api/controller/PrestashopProxyController.java
|
||||
package fr.gameovergne.api.controller;
|
||||
|
||||
import fr.gameovergne.api.service.PrestashopClient;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ps")
|
||||
@@ -27,9 +28,10 @@ public class PrestashopProxyController {
|
||||
|
||||
String relativePath = new AntPathMatcher()
|
||||
.extractPathWithinPattern(bestMatchPattern, fullPath);
|
||||
|
||||
String path = relativePath.isEmpty() ? "/" : "/" + relativePath;
|
||||
|
||||
String rawQuery = request.getQueryString();
|
||||
String rawQuery = request.getQueryString(); // déjà encodée
|
||||
|
||||
return prestashopClient.getWithRawQuery(path, rawQuery);
|
||||
}
|
||||
|
||||
@@ -1,47 +1,79 @@
|
||||
package fr.gameovergne.api.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
@Service
|
||||
public class PrestashopClient {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PrestashopClient.class);
|
||||
|
||||
private final String baseUrl;
|
||||
private final String apiKey;
|
||||
private final org.springframework.web.client.RestTemplate restTemplate =
|
||||
new org.springframework.web.client.RestTemplate();
|
||||
private final RestTemplate restTemplate = new 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.baseUrl = baseUrl; // ex: https://shop.gameovergne.fr/api
|
||||
this.apiKey = apiKey;
|
||||
log.info("[PrestaShop] Base URL = {}", baseUrl);
|
||||
log.info("[PrestaShop] API key length = {}", apiKey != null ? apiKey.length() : 0);
|
||||
}
|
||||
|
||||
public ResponseEntity<String> getWithRawQuery(String relativePath, String rawQuery) {
|
||||
|
||||
UriComponentsBuilder builder = UriComponentsBuilder
|
||||
.fromHttpUrl(baseUrl)
|
||||
.path(relativePath);
|
||||
|
||||
if (StringUtils.hasText(rawQuery)) {
|
||||
builder.query(rawQuery);
|
||||
// Normalisation du path
|
||||
String path = (relativePath == null) ? "" : relativePath;
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
|
||||
URI uri = builder.build(true).toUri();
|
||||
// Construction manuelle de l’URL
|
||||
StringBuilder urlBuilder = new StringBuilder();
|
||||
urlBuilder.append(baseUrl);
|
||||
urlBuilder.append(path);
|
||||
|
||||
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||
urlBuilder.append('?').append(rawQuery);
|
||||
}
|
||||
|
||||
String urlString = urlBuilder.toString();
|
||||
log.info("[PrestaShop] GET {}", urlString);
|
||||
|
||||
URI uri = URI.create(urlString);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
|
||||
// Presta: Basic Auth avec apiKey comme user et mot de passe vide
|
||||
headers.setBasicAuth(apiKey, "");
|
||||
|
||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||
|
||||
return restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
|
||||
try {
|
||||
ResponseEntity<String> response =
|
||||
restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
|
||||
|
||||
log.info("[PrestaShop] Response {} {}", response.getStatusCode().value(),
|
||||
response.getBody());
|
||||
|
||||
return response;
|
||||
} catch (RestClientException ex) {
|
||||
log.error("[PrestaShop] Error calling {} : {}", urlString, ex.toString(), ex);
|
||||
// On renvoie quelque chose de propre au client Angular
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_GATEWAY,
|
||||
"Error while calling PrestaShop API",
|
||||
ex
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,5 +12,5 @@ spring.jpa.show-sql=true
|
||||
|
||||
jwt.secret=a23ac96ce968bf13099d99410b951dd498118851bdfc996a3f844bd68b1b2afd
|
||||
|
||||
prestashop.base-url=https://shop.gameovergne.fr/api
|
||||
prestashop.api.base-url=https://shop.gameovergne.fr/api
|
||||
prestashop.api-key=${PRESTASHOP_API_KEY}
|
||||
Reference in New Issue
Block a user