restore previous version
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
package fr.gameovergne.api.controller;
|
||||
|
||||
import fr.gameovergne.api.dto.ps.*;
|
||||
import fr.gameovergne.api.model.ps.SimpleResource;
|
||||
import fr.gameovergne.api.service.PrestashopAdminService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ps-admin")
|
||||
@RequiredArgsConstructor
|
||||
public class PrestashopAdminController {
|
||||
|
||||
private final PrestashopAdminService service;
|
||||
|
||||
// --- Simple resources ---
|
||||
|
||||
@GetMapping("/{resource}")
|
||||
public List<PsItemDto> listSimple(@PathVariable SimpleResource resource) {
|
||||
return service.listSimple(resource);
|
||||
}
|
||||
|
||||
@PostMapping("/{resource}")
|
||||
public long createSimple(@PathVariable SimpleResource resource,
|
||||
@RequestParam String name) {
|
||||
return service.createSimple(resource, name);
|
||||
}
|
||||
|
||||
@PutMapping("/{resource}/{id}")
|
||||
public void updateSimple(@PathVariable SimpleResource resource,
|
||||
@PathVariable long id,
|
||||
@RequestParam String name) {
|
||||
service.updateSimple(resource, id, name);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{resource}/{id}")
|
||||
public void deleteSimple(@PathVariable SimpleResource resource,
|
||||
@PathVariable long id) {
|
||||
service.deleteSimple(resource, id);
|
||||
}
|
||||
|
||||
// --- Produits liste + flags + condition values ---
|
||||
|
||||
@GetMapping("/products")
|
||||
public List<ProductListItemDto> listProducts(@RequestParam(required = false) String q) {
|
||||
return service.listProducts(q);
|
||||
}
|
||||
|
||||
@GetMapping("/products/{id}/flags")
|
||||
public ProductFlagsDto getProductFlags(@PathVariable long id) {
|
||||
return service.getProductFlags(id);
|
||||
}
|
||||
|
||||
@GetMapping("/meta/condition-values")
|
||||
public List<String> getConditionValues() {
|
||||
return service.getConditionValues();
|
||||
}
|
||||
|
||||
// --- Create / update / delete produit ---
|
||||
|
||||
@PostMapping("/products")
|
||||
public long createProduct(@RequestBody PsProductDto dto) {
|
||||
return service.createProduct(dto);
|
||||
}
|
||||
|
||||
@PutMapping("/products/{id}")
|
||||
public void updateProduct(@PathVariable long id,
|
||||
@RequestBody PsProductDto dto) {
|
||||
service.updateProduct(id, dto);
|
||||
}
|
||||
|
||||
@DeleteMapping("/products/{id}")
|
||||
public void deleteProduct(@PathVariable long id) {
|
||||
service.deleteProduct(id);
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,13 @@ package fr.gameovergne.api.controller;
|
||||
|
||||
import fr.gameovergne.api.service.PrestashopClient;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ps")
|
||||
@@ -15,13 +20,24 @@ public class PrestashopProxyController {
|
||||
this.prestashopClient = prestashopClient;
|
||||
}
|
||||
|
||||
@GetMapping("/{resource}")
|
||||
public ResponseEntity<String> proxyGet(
|
||||
@PathVariable String resource,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
String rawQuery = request.getQueryString(); // ex: "display=%5Bid,name,active%5D&output_format=JSON"
|
||||
String body = prestashopClient.getWithRawQuery(resource, rawQuery);
|
||||
return ResponseEntity.ok(body);
|
||||
@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 rawQuery = request.getQueryString();
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.getWithRawQuery("/" + relativePath, rawQuery);
|
||||
|
||||
return ResponseEntity
|
||||
.status(prestaResponse.getStatusCode())
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(prestaResponse.getBody());
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package fr.gameovergne.api.dto.ps;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
public class ProductFlagsDto {
|
||||
boolean complete;
|
||||
boolean hasManual;
|
||||
String conditionLabel;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package fr.gameovergne.api.dto.ps;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
public class ProductListItemDto {
|
||||
Long id;
|
||||
String name;
|
||||
Long manufacturerId;
|
||||
Long supplierId;
|
||||
Long categoryId;
|
||||
Double priceHt;
|
||||
Integer quantity;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package fr.gameovergne.api.dto.ps;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
public class PsItemDto {
|
||||
Long id;
|
||||
String name;
|
||||
Boolean active;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package fr.gameovergne.api.dto.ps;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
public class PsProductDto {
|
||||
Long id; // optionnel pour update
|
||||
String name;
|
||||
|
||||
Long manufacturerId;
|
||||
Long supplierId;
|
||||
Long categoryId;
|
||||
|
||||
Double priceTtc;
|
||||
Double vatRate;
|
||||
|
||||
Integer quantity;
|
||||
|
||||
Boolean complete; // Complet: Oui/Non
|
||||
Boolean hasManual; // Notice: Avec/Sans
|
||||
String conditionLabel; // État: libellé
|
||||
|
||||
String description; // description libre
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package fr.gameovergne.api.model.ps;
|
||||
|
||||
public enum SimpleResource {
|
||||
categories("categories", "category", true, true),
|
||||
manufacturers("manufacturers", "manufacturer", false, false),
|
||||
suppliers("suppliers", "supplier", false, false);
|
||||
|
||||
public final String path;
|
||||
public final String root;
|
||||
public final boolean needsDefaultLang;
|
||||
public final boolean nameIsMultilang;
|
||||
|
||||
SimpleResource(String path, String root, boolean needsDefaultLang, boolean nameIsMultilang) {
|
||||
this.path = path;
|
||||
this.root = root;
|
||||
this.needsDefaultLang = needsDefaultLang;
|
||||
this.nameIsMultilang = nameIsMultilang;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,19 +1,17 @@
|
||||
// package à adapter si besoin
|
||||
package fr.gameovergne.api.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Base64;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -21,176 +19,104 @@ public class PrestashopClient {
|
||||
|
||||
private final RestClient client;
|
||||
private final String baseUrl;
|
||||
private final String apiKey;
|
||||
|
||||
public PrestashopClient(
|
||||
@Value("${prestashop.api.base-url}") String baseUrl,
|
||||
@Value("${prestashop.api.key}") String apiKey
|
||||
@Value("${prestashop.base-url}") String baseUrl,
|
||||
@Value("${prestashop.api-key}") String apiKey
|
||||
) {
|
||||
this.baseUrl = baseUrl; // ex: https://shop.gameovergne.fr/api
|
||||
this.apiKey = apiKey;
|
||||
this.baseUrl = baseUrl;
|
||||
|
||||
String basicAuth = Base64.getEncoder()
|
||||
.encodeToString((apiKey + ":").getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
this.client = RestClient.builder()
|
||||
.defaultHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth)
|
||||
.build();
|
||||
|
||||
log.info("[PrestaShop] Base URL = {}", baseUrl);
|
||||
log.info("[PrestaShop] API key length = {}", apiKey.length());
|
||||
|
||||
this.client = RestClient.builder()
|
||||
.defaultHeaders(headers -> {
|
||||
headers.set(HttpHeaders.USER_AGENT, "curl/8.10.1");
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.ALL));
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Outil interne : construit l’URL complète baseUrl + path + ?ws_key=...¶ms...
|
||||
// ------------------------------------------------------------------------
|
||||
private String buildUrl(String path, MultiValueMap<String, String> params) {
|
||||
StringBuilder full = new StringBuilder();
|
||||
|
||||
// baseUrl
|
||||
full.append(baseUrl);
|
||||
|
||||
// path
|
||||
if (path != null && !path.isBlank()) {
|
||||
boolean baseEndsWithSlash = baseUrl.endsWith("/");
|
||||
boolean pathStartsWithSlash = path.startsWith("/");
|
||||
|
||||
if (baseEndsWithSlash && pathStartsWithSlash) {
|
||||
full.append(path.substring(1));
|
||||
} else if (!baseEndsWithSlash && !pathStartsWithSlash) {
|
||||
full.append("/").append(path);
|
||||
} else {
|
||||
full.append(path);
|
||||
}
|
||||
}
|
||||
|
||||
// ws_key en premier param
|
||||
full.append("?ws_key=").append(URLEncoder.encode(apiKey, StandardCharsets.UTF_8));
|
||||
|
||||
private String buildUri(String path, MultiValueMap<String, String> params) {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder
|
||||
.fromHttpUrl(baseUrl + path);
|
||||
if (params != null && !params.isEmpty()) {
|
||||
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
for (String value : entry.getValue()) {
|
||||
full.append("&")
|
||||
.append(URLEncoder.encode(key, StandardCharsets.UTF_8))
|
||||
.append("=")
|
||||
.append(URLEncoder.encode(value, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
builder.queryParams(params);
|
||||
}
|
||||
|
||||
String url = full.toString();
|
||||
log.debug("[PrestaShop] Built URL = {}", url);
|
||||
return url;
|
||||
return builder.build(true).toUriString();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// GET JSON (utilisé partout dans PrestashopAdminService)
|
||||
// ------------------------------------------------------------------------
|
||||
public String getJson(String path, MultiValueMap<String, String> params) {
|
||||
String url = buildUrl(path, params);
|
||||
log.info("[PrestaShop] GET JSON {}", url);
|
||||
// -------- Méthodes "typed" JSON / XML utilisées par ps-admin --------
|
||||
|
||||
public String getJson(String path, MultiValueMap<String, String> params) {
|
||||
String uri = buildUri(path, params);
|
||||
log.info("[PrestaShop] GET JSON {}", uri);
|
||||
return client.get()
|
||||
.uri(URI.create(url))
|
||||
.uri(uri)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// GET XML (utilisé pour certains appels de stock, produit, etc.)
|
||||
// ------------------------------------------------------------------------
|
||||
public String getXml(String path, MultiValueMap<String, String> params) {
|
||||
String url = buildUrl(path, params);
|
||||
log.info("[PrestaShop] GET XML {}", url);
|
||||
|
||||
String uri = buildUri(path, params);
|
||||
log.info("[PrestaShop] GET XML {}", uri);
|
||||
return client.get()
|
||||
.uri(URI.create(url))
|
||||
.uri(uri)
|
||||
.accept(MediaType.APPLICATION_XML)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// POST XML (création catégorie/produit/etc.)
|
||||
// ------------------------------------------------------------------------
|
||||
public String postXml(String path, MultiValueMap<String, String> params, String xmlBody) {
|
||||
String url = buildUrl(path, params);
|
||||
log.info("[PrestaShop] POST XML {} (body length={})", url, xmlBody != null ? xmlBody.length() : 0);
|
||||
|
||||
String uri = buildUri(path, params);
|
||||
log.info("[PrestaShop] POST XML {}", uri);
|
||||
return client.post()
|
||||
.uri(URI.create(url))
|
||||
.uri(uri)
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.accept(MediaType.APPLICATION_XML)
|
||||
.body(xmlBody != null ? xmlBody : "")
|
||||
.body(xmlBody)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// PUT XML (update catégorie/produit/stock/etc.)
|
||||
// ------------------------------------------------------------------------
|
||||
public String putXml(String path, MultiValueMap<String, String> params, String xmlBody) {
|
||||
String url = buildUrl(path, params);
|
||||
log.info("[PrestaShop] PUT XML {} (body length={})", url, xmlBody != null ? xmlBody.length() : 0);
|
||||
|
||||
String uri = buildUri(path, params);
|
||||
log.info("[PrestaShop] PUT XML {}", uri);
|
||||
return client.put()
|
||||
.uri(URI.create(url))
|
||||
.uri(uri)
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.accept(MediaType.APPLICATION_XML)
|
||||
.body(xmlBody != null ? xmlBody : "")
|
||||
.body(xmlBody)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// DELETE (suppression ressource)
|
||||
// ------------------------------------------------------------------------
|
||||
public void delete(String path, MultiValueMap<String, String> params) {
|
||||
String url = buildUrl(path, params);
|
||||
log.info("[PrestaShop] DELETE {}", url);
|
||||
|
||||
String uri = buildUri(path, params);
|
||||
log.info("[PrestaShop] DELETE {}", uri);
|
||||
client.delete()
|
||||
.uri(URI.create(url))
|
||||
.uri(uri)
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Spécial proxy GET brut : on garde la query telle quelle (déjà encodée)
|
||||
// ------------------------------------------------------------------------
|
||||
public String getWithRawQuery(String resource, String rawQuery) {
|
||||
try {
|
||||
StringBuilder fullUrl = new StringBuilder();
|
||||
fullUrl.append(baseUrl);
|
||||
// baseUrl : https://shop.gameovergne.fr/api
|
||||
if (!baseUrl.endsWith("/")) {
|
||||
fullUrl.append("/");
|
||||
}
|
||||
// resource : "categories", "products", ...
|
||||
fullUrl.append(resource);
|
||||
// -------- Méthode générique utilisée par le proxy /api/ps/** --------
|
||||
|
||||
// ws_key
|
||||
String encodedKey = URLEncoder.encode(apiKey, StandardCharsets.UTF_8);
|
||||
fullUrl.append("?ws_key=").append(encodedKey);
|
||||
|
||||
// rawQuery = "display=%5Bid,name,active%5D&output_format=JSON"
|
||||
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||
fullUrl.append("&").append(rawQuery); // surtout ne pas réencoder
|
||||
}
|
||||
|
||||
String urlString = fullUrl.toString();
|
||||
log.info("[PrestaShop] RAW GET via ws_key = {}", urlString);
|
||||
|
||||
return client.get()
|
||||
.uri(URI.create(urlString))
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("[PrestaShop] getWithRawQuery error for resource={} rawQuery={}", resource, rawQuery, e);
|
||||
throw e;
|
||||
/**
|
||||
* Proxy brut : on lui donne le path Presta (ex: "/categories") et la query string déjà encodée.
|
||||
* On récupère un ResponseEntity<String> pour pouvoir propager le status code.
|
||||
*/
|
||||
public ResponseEntity<String> getWithRawQuery(String path, String rawQuery) {
|
||||
String uri = baseUrl + path;
|
||||
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||
uri = uri + "?" + rawQuery;
|
||||
}
|
||||
|
||||
log.info("[PrestaShop] GET (proxy) {}", uri);
|
||||
|
||||
return client.get()
|
||||
.uri(uri)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user