Update api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java
This commit is contained in:
@@ -2,10 +2,10 @@ package fr.gameovergne.api.controller.prestashop;
|
||||
|
||||
import fr.gameovergne.api.service.prestashop.PrestashopClient;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ps")
|
||||
public class PrestashopProxyController {
|
||||
@@ -16,17 +16,30 @@ public class PrestashopProxyController {
|
||||
this.prestashopClient = prestashopClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy GET sur /api/ps/** -> /api/** sur PrestaShop
|
||||
* Exemple front :
|
||||
* /api/ps/categories?display=[id,name,active]&output_format=JSON
|
||||
* deviendra côté Presta :
|
||||
* https://shop.gameovergne.fr/api/categories?display=[id,name,active]&output_format=JSON
|
||||
*/
|
||||
@GetMapping("/**")
|
||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||
String fullPath = request.getRequestURI(); // ex: /api/ps/suppliers
|
||||
String fullPath = request.getRequestURI(); // ex: /api/ps/categories
|
||||
String contextPath = request.getContextPath(); // souvent ""
|
||||
String relative = fullPath.substring(contextPath.length()); // /api/ps/suppliers
|
||||
String relative = fullPath.substring(contextPath.length()); // /api/ps/categories
|
||||
|
||||
// On enlève le préfixe /api/ps -> /suppliers
|
||||
// On enlève le préfixe /api/ps => /categories
|
||||
String path = relative.replaceFirst("^/api/ps", "");
|
||||
|
||||
String query = request.getQueryString(); // ex: display=%5Bid,name,active%5D&output_format=JSON
|
||||
|
||||
return prestashopClient.get(path, query);
|
||||
String body = prestashopClient.get(path, query);
|
||||
|
||||
// On renvoie du JSON brut comme Presta, en HTTP 200
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(body);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user