feat: add PrestashopProxyController and PrestashopClient for API proxying
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
package fr.gameovergne.api.controller.prestashop;
|
||||||
|
|
||||||
|
import fr.gameovergne.api.service.prestashop.PrestashopClient;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/ps")
|
||||||
|
public class PrestashopProxyController {
|
||||||
|
|
||||||
|
private final PrestashopClient prestashopClient;
|
||||||
|
|
||||||
|
public PrestashopProxyController(PrestashopClient prestashopClient) {
|
||||||
|
this.prestashopClient = prestashopClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exemple simple : proxy GET sur /api/ps/** -> /api/** sur PrestaShop
|
||||||
|
*/
|
||||||
|
@GetMapping("/**")
|
||||||
|
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||||
|
// Exemple d’URL front : /api/ps/products?display=full
|
||||||
|
String fullPath = request.getRequestURI(); // ex: /api/ps/products
|
||||||
|
String contextPath = request.getContextPath(); // souvent ""
|
||||||
|
String relative = fullPath.substring(contextPath.length()); // /api/ps/products
|
||||||
|
|
||||||
|
// On enlève le préfixe /api/ps pour reconstruire le path à appeler sur PrestaShop
|
||||||
|
String path = relative.replaceFirst("^/api/ps", ""); // -> /products
|
||||||
|
|
||||||
|
String query = request.getQueryString(); // ex: display=full
|
||||||
|
|
||||||
|
return prestashopClient.get(path, query);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package fr.gameovergne.api.service.prestashop;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PrestashopClient {
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
private final String baseUrl;
|
||||||
|
private final String basicAuth; // valeur déjà encodée Base64 (Authorization: Basic ...)
|
||||||
|
|
||||||
|
public PrestashopClient(
|
||||||
|
@Value("${prestashop.base-url}") String baseUrl,
|
||||||
|
@Value("${prestashop.basic-auth}") String basicAuth) {
|
||||||
|
this.baseUrl = baseUrl;
|
||||||
|
this.basicAuth = basicAuth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResponseEntity<String> get(String path, String query) {
|
||||||
|
// path = ex: "/products"
|
||||||
|
// query = ex: "display=full&limit=10" (facultatif)
|
||||||
|
String url = UriComponentsBuilder
|
||||||
|
.fromHttpUrl(baseUrl)
|
||||||
|
.path("/api")
|
||||||
|
.path(path)
|
||||||
|
.query(query)
|
||||||
|
.build(true)
|
||||||
|
.toUriString();
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||||
|
|
||||||
|
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
|
return restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si tu as besoin de POST/PUT plus tard tu ajoutes d'autres méthodes ici
|
||||||
|
}
|
||||||
@@ -11,3 +11,6 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
|
|||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
|
|
||||||
jwt.secret=a23ac96ce968bf13099d99410b951dd498118851bdfc996a3f844bd68b1b2afd
|
jwt.secret=a23ac96ce968bf13099d99410b951dd498118851bdfc996a3f844bd68b1b2afd
|
||||||
|
|
||||||
|
prestashop.base-url=https://shop.gameovergne.fr
|
||||||
|
prestashop.basic-auth=MkFRUEcxM01KOFgxMTdVNkZKNU5HSFBTOTNIRTM0QUI=
|
||||||
Reference in New Issue
Block a user