|
| 1 | +package fr.dossierfacile.scheduler.tasks.ademe; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import fr.dossierfacile.common.model.AdemeApiResultModel; |
| 6 | +import fr.dossierfacile.common.utils.MapperUtil; |
| 7 | +import fr.dossierfacile.scheduler.LoggingContext; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.scheduling.annotation.Scheduled; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | + |
| 13 | +import java.net.URI; |
| 14 | +import java.net.URISyntaxException; |
| 15 | +import java.net.http.HttpClient; |
| 16 | +import java.net.http.HttpRequest; |
| 17 | +import java.net.http.HttpResponse; |
| 18 | +import java.time.Instant; |
| 19 | +import java.util.Date; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +import static fr.dossierfacile.scheduler.tasks.TaskName.CHECK_API_ADEME; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@Service |
| 26 | +@RequiredArgsConstructor |
| 27 | +public class CheckAdemeApiTask { |
| 28 | + private final ObjectMapper objectMapper = MapperUtil.newObjectMapper(); |
| 29 | + |
| 30 | + // Used to check if the API is down and log details |
| 31 | + @Scheduled(fixedDelayString = "${scheduled.process.check.api.ademe:10}", initialDelayString = "${scheduled.process.check.api.ademe:10}", timeUnit = TimeUnit.MINUTES) |
| 32 | + public void checkAdemeApi() { |
| 33 | + LoggingContext.startTask(CHECK_API_ADEME); |
| 34 | + URI uri; |
| 35 | + HttpResponse<String> response; |
| 36 | + try { |
| 37 | + uri = new URI("https://observatoire-dpe-audit.ademe.fr/pub/dpe/2392E2001612S"); |
| 38 | + HttpRequest request = HttpRequest.newBuilder() |
| 39 | + .uri(uri) |
| 40 | + .GET() |
| 41 | + .build(); |
| 42 | + try (HttpClient client = HttpClient.newHttpClient()) { |
| 43 | + response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 44 | + String json = response.body(); |
| 45 | + if (response.statusCode() == 404) { |
| 46 | + log.error("ADEME API ERROR 404 : DPE NOT FOUND"); |
| 47 | + } |
| 48 | + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 49 | + AdemeApiResultModel ademeApiResultModel = objectMapper.readValue(json, AdemeApiResultModel.class); |
| 50 | + |
| 51 | + if(!ademeApiResultModel.getNumero().equals("2392E2001612S")) { |
| 52 | + log.error("ADEME API ERROR : Error with number : {}", ademeApiResultModel.getNumero()); |
| 53 | + } |
| 54 | + if (!ademeApiResultModel.getDateRealisation().equals("2023-06-14T22:00:00Z")) { |
| 55 | + log.error("ADEME API ERROR : Error with date : {}", ademeApiResultModel.getDateRealisation()); |
| 56 | + } |
| 57 | + } catch (Exception e) { |
| 58 | + log.error("ADEME API ERROR : An error occurred while processing the request", e); |
| 59 | + } |
| 60 | + } catch (URISyntaxException e) { |
| 61 | + log.error("ADEME API ERROR : An URISyntaxException occured", e); |
| 62 | + } |
| 63 | + LoggingContext.endTask(); |
| 64 | + } |
| 65 | +} |
0 commit comments