Update api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java
This commit is contained in:
@@ -3,6 +3,8 @@ package fr.gameovergne.api.service.prestashop;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.HttpStatusCodeException;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
@@ -22,23 +24,47 @@ public class PrestashopClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<String> get(String path, String query) {
|
public ResponseEntity<String> get(String path, String query) {
|
||||||
// path = ex: "/products"
|
// path = ex: "/categories"
|
||||||
// query = ex: "display=full&limit=10" (facultatif)
|
// query = ex: "display=...[...]" (déjà encodé)
|
||||||
String url = UriComponentsBuilder
|
String url = UriComponentsBuilder
|
||||||
.fromHttpUrl(baseUrl)
|
.fromHttpUrl(baseUrl)
|
||||||
.path("/api")
|
.path("/api")
|
||||||
.path(path)
|
.path(path)
|
||||||
.query(query)
|
.query(query)
|
||||||
.build(true)
|
.build(true) // la query contient déjà les %5B..%5D
|
||||||
.toUriString();
|
.toUriString();
|
||||||
|
|
||||||
|
System.out.println("[Presta] GET " + url);
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
|
||||||
|
|
||||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
return restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
try {
|
||||||
}
|
ResponseEntity<String> response =
|
||||||
|
restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
||||||
|
|
||||||
// Si tu as besoin de POST/PUT plus tard tu ajoutes d'autres méthodes ici
|
System.out.println("[Presta] <- " + response.getStatusCode());
|
||||||
|
return ResponseEntity
|
||||||
|
.status(response.getStatusCode())
|
||||||
|
.headers(response.getHeaders())
|
||||||
|
.body(response.getBody());
|
||||||
|
|
||||||
|
} catch (HttpStatusCodeException ex) {
|
||||||
|
// Erreur HTTP renvoyée par Presta (401, 403, 404, 500, ...)
|
||||||
|
System.err.println("[Presta] ERROR " + ex.getStatusCode() + " for URL: " + url);
|
||||||
|
System.err.println("[Presta] BODY: " + ex.getResponseBodyAsString());
|
||||||
|
return ResponseEntity
|
||||||
|
.status(ex.getStatusCode())
|
||||||
|
.body(ex.getResponseBodyAsString());
|
||||||
|
} catch (RestClientException ex) {
|
||||||
|
// Erreur réseau/TLS/etc
|
||||||
|
System.err.println("[Presta] CLIENT ERROR for URL: " + url);
|
||||||
|
ex.printStackTrace();
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_GATEWAY)
|
||||||
|
.body("Error while calling Prestashop: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user