Refactor PrestashopProxyController to streamline API endpoint handling and improve code readability
This commit is contained in:
@@ -2,139 +2,94 @@ package fr.gameovergne.api.controller.prestashop;
|
||||
|
||||
import fr.gameovergne.api.service.prestashop.PrestashopClient;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ps")
|
||||
@RequestMapping("/api") // important : ton context-path est /gameovergne-api, donc ici on reste sur /api
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class PrestashopProxyController {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(PrestashopProxyController.class);
|
||||
|
||||
private final PrestashopClient prestashopClient;
|
||||
|
||||
public PrestashopProxyController(PrestashopClient prestashopClient) {
|
||||
this.prestashopClient = prestashopClient;
|
||||
}
|
||||
// ---------- GET générique /api/ps/** ----------
|
||||
|
||||
// --- Helpers communs ---
|
||||
|
||||
private String extractPath(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);
|
||||
|
||||
// On renvoie toujours avec un "/" devant
|
||||
return "/" + relativePath;
|
||||
}
|
||||
|
||||
private String extractDecodedQuery(HttpServletRequest request) {
|
||||
String rawQuery = request.getQueryString();
|
||||
if (rawQuery != null) {
|
||||
rawQuery = URLDecoder.decode(rawQuery, StandardCharsets.UTF_8);
|
||||
}
|
||||
return rawQuery;
|
||||
}
|
||||
|
||||
// ---------- GET ----------
|
||||
@GetMapping("/**")
|
||||
@GetMapping(path = "/ps/**", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||
String path = extractPath(request);
|
||||
String rawQuery = extractDecodedQuery(request);
|
||||
String requestUri = request.getRequestURI(); // ex: /gameovergne-api/api/ps/categories
|
||||
String path = requestUri.substring(request.getContextPath().length() + "/api".length()); // -> /ps/categories
|
||||
String rawQuery = request.getQueryString();
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.getWithRawQuery(path, rawQuery);
|
||||
|
||||
return ResponseEntity
|
||||
.status(prestaResponse.getStatusCode())
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(prestaResponse.getBody());
|
||||
return prestashopClient.getWithRawQuery(path, rawQuery);
|
||||
}
|
||||
|
||||
// ---------- POST ----------
|
||||
@PostMapping("/**")
|
||||
// ---------- POST générique XML /api/ps/** (catégories, produits, etc.) ----------
|
||||
|
||||
@PostMapping(
|
||||
path = "/ps/**",
|
||||
consumes = MediaType.APPLICATION_XML_VALUE,
|
||||
produces = MediaType.APPLICATION_XML_VALUE
|
||||
)
|
||||
public ResponseEntity<String> proxyPost(HttpServletRequest request,
|
||||
@RequestBody String xmlBody) {
|
||||
String path = extractPath(request);
|
||||
String rawQuery = extractDecodedQuery(request);
|
||||
|
||||
String requestUri = request.getRequestURI();
|
||||
String path = requestUri.substring(request.getContextPath().length() + "/api".length()); // ex: /ps/categories
|
||||
String rawQuery = request.getQueryString();
|
||||
|
||||
log.info("XML envoyé à Presta:\n{}", xmlBody);
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.postWithRawQuery(path, rawQuery, xmlBody);
|
||||
|
||||
return ResponseEntity
|
||||
.status(prestaResponse.getStatusCode())
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(prestaResponse.getBody());
|
||||
return prestashopClient.postWithRawQuery(path, rawQuery, xmlBody);
|
||||
}
|
||||
|
||||
// ---------- PUT ----------
|
||||
@PutMapping("/**")
|
||||
// ---------- PUT générique XML /api/ps/** ----------
|
||||
|
||||
@PutMapping(
|
||||
path = "/ps/**",
|
||||
consumes = MediaType.APPLICATION_XML_VALUE,
|
||||
produces = MediaType.APPLICATION_XML_VALUE
|
||||
)
|
||||
public ResponseEntity<String> proxyPut(HttpServletRequest request,
|
||||
@RequestBody String xmlBody) {
|
||||
String path = extractPath(request);
|
||||
String rawQuery = extractDecodedQuery(request);
|
||||
|
||||
log.info("XML envoyé à Presta:\n{}", xmlBody);
|
||||
String requestUri = request.getRequestURI();
|
||||
String path = requestUri.substring(request.getContextPath().length() + "/api".length());
|
||||
String rawQuery = request.getQueryString();
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.putWithRawQuery(path, rawQuery, xmlBody);
|
||||
log.info("XML envoyé (proxy PUT):\n{}", xmlBody);
|
||||
|
||||
return ResponseEntity
|
||||
.status(prestaResponse.getStatusCode())
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(prestaResponse.getBody());
|
||||
return prestashopClient.putWithRawQuery(path, rawQuery, xmlBody);
|
||||
}
|
||||
|
||||
// ---------- DELETE ----------
|
||||
@DeleteMapping("/**")
|
||||
// ---------- DELETE générique /api/ps/** ----------
|
||||
|
||||
@DeleteMapping(path = "/ps/**", produces = MediaType.APPLICATION_XML_VALUE)
|
||||
public ResponseEntity<String> proxyDelete(HttpServletRequest request) {
|
||||
String path = extractPath(request);
|
||||
String rawQuery = extractDecodedQuery(request);
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.deleteWithRawQuery(path, rawQuery);
|
||||
String requestUri = request.getRequestURI();
|
||||
String path = requestUri.substring(request.getContextPath().length() + "/api".length());
|
||||
String rawQuery = request.getQueryString();
|
||||
|
||||
return ResponseEntity
|
||||
.status(prestaResponse.getStatusCode())
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(prestaResponse.getBody());
|
||||
return prestashopClient.deleteWithRawQuery(path, rawQuery);
|
||||
}
|
||||
|
||||
// ---------- UPLOAD IMAGE PRODUIT /api/ps/images/products/{productId} ----------
|
||||
|
||||
/**
|
||||
* Upload d'une image produit :
|
||||
* Front → (multipart/form-data) → /api/ps/images/products/{productId}
|
||||
* Backend → (bytes) → https://shop.gameovergne.fr/api/images/products/{productId}
|
||||
*/
|
||||
@PostMapping(
|
||||
path = "/api/ps/images/products/{productId}",
|
||||
path = "/ps/images/products/{productId}",
|
||||
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
|
||||
produces = MediaType.APPLICATION_XML_VALUE
|
||||
)
|
||||
public ResponseEntity<String> uploadProductImage(
|
||||
jakarta.servlet.http.HttpServletRequest request,
|
||||
public ResponseEntity<String> uploadProductImage(HttpServletRequest request,
|
||||
@PathVariable("productId") String productId,
|
||||
@RequestPart("image") MultipartFile imageFile
|
||||
) {
|
||||
String rawQuery = request.getQueryString(); // si jamais tu ajoutes des options côté Presta
|
||||
@RequestPart("image") MultipartFile imageFile) {
|
||||
|
||||
String rawQuery = request.getQueryString(); // si un jour tu veux passer des options
|
||||
log.info("[Proxy] Upload image produit {} (size={} bytes, ct={})",
|
||||
productId, imageFile.getSize(), imageFile.getContentType());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user