|
| 1 | +package fr.insee.pogues.metadata.client; |
| 2 | + |
| 3 | +import fr.insee.pogues.metadata.model.magma.Operation; |
| 4 | +import fr.insee.pogues.metadata.model.magma.Serie; |
| 5 | +import fr.insee.pogues.webservice.rest.PoguesException; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | +import org.springframework.web.reactive.function.client.WebClient; |
| 12 | +import org.springframework.web.reactive.function.client.WebClientResponseException; |
| 13 | +import org.springframework.web.util.UriComponentsBuilder; |
| 14 | + |
| 15 | +import java.net.URI; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +@Service |
| 19 | +@Slf4j |
| 20 | +public class MagmaClientImpl implements MagmaClient { |
| 21 | + |
| 22 | + private static final String SERIES_PATH = "/operations/series"; |
| 23 | + private static final String OPERATIONS_SERIE_PATH = "/operations/serie"; |
| 24 | + private static final String OPERATIONS_PATH = "operations"; |
| 25 | + private static final String SINGLE_OPERATION_PATH = "/operations/operation"; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private WebClient webClient; |
| 29 | + |
| 30 | + @Value("${application.metadata.magma}") |
| 31 | + String magmaHost; |
| 32 | + |
| 33 | + @Override |
| 34 | + public List<Serie> getSeries() throws Exception { |
| 35 | + URI uri = UriComponentsBuilder |
| 36 | + .fromHttpUrl(magmaHost) |
| 37 | + .path(SERIES_PATH) |
| 38 | + .queryParam("survey",true) |
| 39 | + .build().toUri(); |
| 40 | + log.info("Call Magma with URI : {}", uri); |
| 41 | + try { |
| 42 | + Serie[] series = webClient.get() |
| 43 | + .uri(uri) |
| 44 | + .accept(MediaType.APPLICATION_JSON) |
| 45 | + .retrieve() |
| 46 | + .bodyToMono(Serie[].class) |
| 47 | + .block(); |
| 48 | + return List.of(series); |
| 49 | + } catch (WebClientResponseException e) { |
| 50 | + log.error(e.getMessage()); |
| 51 | + throw new PoguesException(e.getStatusCode().value(), e.getStatusText(), e.getResponseBodyAsString()); |
| 52 | + } catch (Exception e){ |
| 53 | + log.error(e.getMessage()); |
| 54 | + throw new PoguesException(500, "Unknow error during metadata call", ""); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Serie getSerieById(String id) throws Exception { |
| 60 | + URI uri = UriComponentsBuilder |
| 61 | + .fromHttpUrl(magmaHost) |
| 62 | + .path(OPERATIONS_SERIE_PATH) |
| 63 | + .pathSegment(id) |
| 64 | + .build().toUri(); |
| 65 | + log.info("Call Magma with URI : {}", uri); |
| 66 | + try { |
| 67 | + Serie serie = webClient.get() |
| 68 | + .uri(uri) |
| 69 | + .accept(MediaType.APPLICATION_JSON) |
| 70 | + .retrieve() |
| 71 | + .bodyToMono(Serie.class) |
| 72 | + .block(); |
| 73 | + return serie; |
| 74 | + } catch (WebClientResponseException e) { |
| 75 | + log.error(e.getMessage()); |
| 76 | + throw new PoguesException(e.getStatusCode().value(), e.getStatusText(), e.getResponseBodyAsString()); |
| 77 | + } catch (Exception e){ |
| 78 | + throw new PoguesException(500, "Unknow error during metadata call", ""); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public List<Operation> getOperationsByIdSerie(String id) throws Exception { |
| 84 | + URI uri = UriComponentsBuilder |
| 85 | + .fromHttpUrl(magmaHost) |
| 86 | + .path(OPERATIONS_SERIE_PATH) |
| 87 | + .pathSegment(id) |
| 88 | + .pathSegment(OPERATIONS_PATH) |
| 89 | + .build().toUri(); |
| 90 | + log.info("Call Magma with URI : {}", uri); |
| 91 | + try { |
| 92 | + Operation[] operations = webClient.get() |
| 93 | + .uri(uri) |
| 94 | + .accept(MediaType.APPLICATION_JSON) |
| 95 | + .retrieve() |
| 96 | + .bodyToMono(Operation[].class) |
| 97 | + .block(); |
| 98 | + return List.of(operations); |
| 99 | + } catch (WebClientResponseException e) { |
| 100 | + log.error(e.getMessage()); |
| 101 | + throw new PoguesException(e.getStatusCode().value(), e.getStatusText(), e.getResponseBodyAsString()); |
| 102 | + } catch (Exception e){ |
| 103 | + throw new PoguesException(500, "Unknow error during metadata call", ""); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Operation getOperationById(String idOperation) throws Exception { |
| 109 | + URI uri = UriComponentsBuilder |
| 110 | + .fromHttpUrl(magmaHost) |
| 111 | + .path(SINGLE_OPERATION_PATH) |
| 112 | + .pathSegment(idOperation) |
| 113 | + .build().toUri(); |
| 114 | + log.info("Call Magma with URI : {}", uri); |
| 115 | + try { |
| 116 | + Operation operation = webClient.get() |
| 117 | + .uri(uri) |
| 118 | + .accept(MediaType.APPLICATION_JSON) |
| 119 | + .retrieve() |
| 120 | + .bodyToMono(Operation.class) |
| 121 | + .block(); |
| 122 | + return operation; |
| 123 | + } catch (WebClientResponseException e) { |
| 124 | + log.error(e.getMessage()); |
| 125 | + throw new PoguesException(e.getStatusCode().value(), e.getStatusText(), e.getResponseBodyAsString()); |
| 126 | + } catch (Exception e){ |
| 127 | + throw new PoguesException(500, "Unknow error during metadata call", ""); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | +} |
0 commit comments