Refactor PrestashopClient and PrestashopProxyController to enhance API request handling and simplify query processing
This commit is contained in:
@@ -7,9 +7,6 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import fr.gameovergne.api.service.PrestashopClient;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ps")
|
||||
@RequiredArgsConstructor
|
||||
@@ -22,23 +19,7 @@ public class PrestashopProxyController {
|
||||
@PathVariable String resource,
|
||||
@RequestParam MultiValueMap<String, String> params
|
||||
) {
|
||||
String query = buildQuery(params);
|
||||
String path = "/" + resource + query;
|
||||
|
||||
String body = prestashopClient.get(path);
|
||||
String body = prestashopClient.get(resource, params);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -2,60 +2,64 @@ package fr.gameovergne.api.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
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
|
||||
@Slf4j
|
||||
public class PrestashopClient {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
private final String baseUrl;
|
||||
private final String apiKey;
|
||||
|
||||
public PrestashopClient(
|
||||
@Value("${prestashop.base-url}") String baseUrl,
|
||||
@Value("${prestashop.api-key}") String rawApiKey,
|
||||
RestTemplateBuilder builder
|
||||
@Value("${prestashop.api-key}") String apiKey
|
||||
) {
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
}
|
||||
this.baseUrl = baseUrl;
|
||||
this.apiKey = apiKey;
|
||||
|
||||
String apiKey = rawApiKey == null ? null : rawApiKey.trim();
|
||||
|
||||
if (apiKey == null || apiKey.isBlank()) {
|
||||
throw new IllegalStateException(
|
||||
"PrestaShop API key is null/blank (prestashop.api-key)."
|
||||
);
|
||||
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] API key length = {}", apiKey.length());
|
||||
log.info("[PrestaShop] API key prefix = {}****", apiKey.substring(0, 4));
|
||||
|
||||
this.restTemplate = builder
|
||||
.basicAuthentication(apiKey, "")
|
||||
.build();
|
||||
log.info("[PrestaShop] Base URL = {}", this.baseUrl);
|
||||
}
|
||||
|
||||
public String get(String pathAndQuery) {
|
||||
String url;
|
||||
public String get(String resource, MultiValueMap<String, String> params) {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder
|
||||
.fromHttpUrl(baseUrl)
|
||||
.pathSegment(resource);
|
||||
|
||||
if (pathAndQuery.startsWith("http://") || pathAndQuery.startsWith("https://")) {
|
||||
url = pathAndQuery;
|
||||
} else if (pathAndQuery.startsWith("/")) {
|
||||
url = baseUrl + pathAndQuery;
|
||||
} else {
|
||||
url = baseUrl + "/" + pathAndQuery;
|
||||
if (params != null) {
|
||||
params.forEach((key, values) -> values.forEach(v -> builder.queryParam(key, v)));
|
||||
}
|
||||
|
||||
log.info("[PrestaShop] GET {}", url);
|
||||
URI uri = builder.build(true).encode().toUri();
|
||||
|
||||
return restTemplate
|
||||
.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, String.class)
|
||||
.getBody();
|
||||
log.info("[PrestaShop] GET {}", uri);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -12,5 +12,5 @@ spring.jpa.show-sql=true
|
||||
|
||||
jwt.secret=a23ac96ce968bf13099d99410b951dd498118851bdfc996a3f844bd68b1b2afd
|
||||
|
||||
prestashop.base-url=https://shop.gameovergne.fr
|
||||
prestashop.base-url=https://shop.gameovergne.fr/api
|
||||
prestashop.api-key=${PRESTASHOP_API_KEY}
|
||||
Reference in New Issue
Block a user