From b30e3aecf1ccd519ee750d2dea6f4b86fc66e9d8 Mon Sep 17 00:00:00 2001 From: Simon Dalvai Date: Wed, 18 Jan 2023 14:57:58 +0100 Subject: [PATCH] flightdata-skyalps: extend get fares --- .../fos/noibz/skyalps/SkyalpsApplication.java | 68 ----------- .../fos/noibz/skyalps/conf/SkyAlpsconf.java | 78 ++++++------- .../fos/noibz/skyalps/rest/AeroCRSRest.java | 107 +++++++++++++----- .../noibz/skyalps/service/AeroCRSConst.java | 25 ---- .../noibz/skyalps/service/SyncScheduler.java | 17 ++- .../src/main/resources/application.properties | 12 +- 6 files changed, 136 insertions(+), 171 deletions(-) delete mode 100644 data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/SkyalpsApplication.java diff --git a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/SkyalpsApplication.java b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/SkyalpsApplication.java deleted file mode 100644 index 5ec1b7822..000000000 --- a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/SkyalpsApplication.java +++ /dev/null @@ -1,68 +0,0 @@ -package it.fos.noibz.skyalps; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Collections; -import java.util.Date; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.context.annotation.Bean; -import org.springframework.http.client.BufferingClientHttpRequestFactory; -import org.springframework.http.client.SimpleClientHttpRequestFactory; -import org.springframework.web.client.RestTemplate; - -import it.fos.noibz.skyalps.dto.json.schedule.AeroCRSFlight; -import it.fos.noibz.skyalps.dto.json.schedule.AeroCRSGetScheduleSuccess; -import it.fos.noibz.skyalps.dto.json.schedule.AeroCRSGetScheduleSuccessResponse; -import it.fos.noibz.skyalps.rest.AeroCRSRest; - -//@SpringBootApplication -public class SkyalpsApplication { - - private static final DateFormat SDF_FORMAT = new SimpleDateFormat("yyyy/MM/dd"); - - private static final Logger LOG = LoggerFactory.getLogger(SkyalpsApplication.class); - @Autowired - private RestTemplate restTemplate; - - public static void main(String[] args) { - SpringApplication.run(SkyalpsApplication.class, args); - } - - @Bean(name = "appRestClient") - public static RestTemplate getRestClient() { - RestTemplate restClient = new RestTemplate( - new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); - // Add one interceptor like in your example, except using anonymous class. - restClient.setInterceptors(Collections.singletonList((request, body, execution) -> { - LOG.debug("Intercepting..."); - return execution.execute(request, body); - })); - return restClient; - } - - @Bean - public CommandLineRunner run(RestTemplate restTemplate) throws Exception { - Date fltsFROMperiod = SDF_FORMAT.parse("2022/05/20"); - Date fltsTOperiod = SDF_FORMAT.parse("2022/06/24"); - return args -> { - AeroCRSGetScheduleSuccessResponse result = AeroCRSRest.getSchedule(restTemplate, fltsFROMperiod, - fltsTOperiod, "IATA", "BN", false, false); - LOG.debug("Result: " + result); - LOG.debug("Result AeroCRS: " + result.getAerocrs()); - if (result.getAerocrs() instanceof AeroCRSGetScheduleSuccess) { - AeroCRSGetScheduleSuccess success = (AeroCRSGetScheduleSuccess) result.getAerocrs(); - LOG.debug("AeroCRSGetScheduleSuccess: " + success); - if (success.isSuccess()) { - for (AeroCRSFlight flight : success.getFlight()) { - LOG.debug("Flight: " + flight); - } - } - } - }; - } -} \ No newline at end of file diff --git a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/conf/SkyAlpsconf.java b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/conf/SkyAlpsconf.java index f8fe8867d..d1de7690b 100644 --- a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/conf/SkyAlpsconf.java +++ b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/conf/SkyAlpsconf.java @@ -57,46 +57,46 @@ public boolean ssim(){ return ssimEnabled; } - @SuppressWarnings("static-access") - @Bean - public CommandLineRunner runner(RestTemplate clients, boolean ssim) throws Exception { - // Parsing user string into a Date object - // Adding fixed amount of days to the Date object - // Substracting fixed amount of days to the Date object - // Retrieving flights in between - Date date = new Date(); - Calendar calFrom = Calendar.getInstance(); - calFrom.setTime(date); - calFrom.add(Calendar.DATE, -DAYS_BEFORE); - Calendar calTo = Calendar.getInstance(); - calTo.setTime(date); - calTo.add(Calendar.DATE, DAYS_AFTER); - Date fltsFROMperiod = calFrom.getTime(); - Date fltsTOperiod = calTo.getTime(); - LOG.debug("Date flight retrieval will start from {} to {}", fltsFROMperiod, fltsTOperiod); - return args -> { - AeroCRSGetScheduleSuccessResponse result = AeroCRSRest.getSchedule(clients, fltsFROMperiod, fltsTOperiod, - IATA, BN, false, ssim); - LOG.debug("The Result is " + result.getAerocrs()); - LOG.debug("The result AeroCRS is " + result.getAerocrs()); - if (result.getAerocrs() instanceof AeroCRSGetScheduleSuccess) { - AeroCRSGetScheduleSuccess success = (AeroCRSGetScheduleSuccess) result.getAerocrs(); - LOG.debug("AeroCRSGetScheduleSuccess: " + success); - if (success.isSuccess()) { - for (AeroCRSFlight flight : success.getFlight()) { - LOG.debug("Flights: " + flight); - } - LOG.debug("Result are displayed...moving on sync and push flights: "); - try { - sync.syncJobStations(); + // @SuppressWarnings("static-access") + // @Bean + // public CommandLineRunner runner(RestTemplate clients, boolean ssim) throws Exception { + // // Parsing user string into a Date object + // // Adding fixed amount of days to the Date object + // // Substracting fixed amount of days to the Date object + // // Retrieving flights in between + // Date date = new Date(); + // Calendar calFrom = Calendar.getInstance(); + // calFrom.setTime(date); + // calFrom.add(Calendar.DATE, -DAYS_BEFORE); + // Calendar calTo = Calendar.getInstance(); + // calTo.setTime(date); + // calTo.add(Calendar.DATE, DAYS_AFTER); + // Date fltsFROMperiod = calFrom.getTime(); + // Date fltsTOperiod = calTo.getTime(); + // LOG.debug("Date flight retrieval will start from {} to {}", fltsFROMperiod, fltsTOperiod); + // return args -> { + // AeroCRSGetScheduleSuccessResponse result = AeroCRSRest.getSchedule(fltsFROMperiod, fltsTOperiod, + // IATA, BN, false, ssim); + // LOG.debug("The Result is " + result.getAerocrs()); + // LOG.debug("The result AeroCRS is " + result.getAerocrs()); + // if (result.getAerocrs() instanceof AeroCRSGetScheduleSuccess) { + // AeroCRSGetScheduleSuccess success = (AeroCRSGetScheduleSuccess) result.getAerocrs(); + // LOG.debug("AeroCRSGetScheduleSuccess: " + success); + // if (success.isSuccess()) { + // for (AeroCRSFlight flight : success.getFlight()) { + // LOG.debug("Flights: " + flight); + // } + // LOG.debug("Result are displayed...moving on sync and push flights: "); + // try { + // sync.syncJobStations(); - } catch (Exception e) { - e.getCause(); - } - } - } + // } catch (Exception e) { + // e.getCause(); + // } + // } + // } - }; - } + // }; + // } } diff --git a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/rest/AeroCRSRest.java b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/rest/AeroCRSRest.java index e31a93539..540beff7f 100644 --- a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/rest/AeroCRSRest.java +++ b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/rest/AeroCRSRest.java @@ -9,6 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -16,6 +17,7 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -27,51 +29,63 @@ import it.fos.noibz.skyalps.dto.json.AeroCRSParms; import it.fos.noibz.skyalps.dto.json.AeroCRSRequest; +import it.fos.noibz.skyalps.dto.json.fares.AeroCRSFaresSuccessResponse; +import it.fos.noibz.skyalps.dto.json.fares.GetFaresRequest; import it.fos.noibz.skyalps.dto.json.schedule.AeroCRSGetScheduleSuccessResponse; import it.fos.noibz.skyalps.dto.json.schedule.GetScheduleRequest; import it.fos.noibz.skyalps.dto.string.AeroCRSGetScheduleSuccessResponseString; -import it.fos.noibz.skyalps.service.AeroCRSConst; /** * * @author Thierry BODHUIN, bodhuin@gmail.com */ +@Service public class AeroCRSRest { - private static final String URL_GET_SCHEDULE = "https://api.aerocrs.com/v5/getSchedule"; - private static final Logger LOG = LoggerFactory.getLogger(AeroCRSRest.class); - - @RequestMapping(value = "/getSchedule", method = RequestMethod.POST) - @ResponseBody - public static AeroCRSGetScheduleSuccessResponse getSchedule(RestTemplate restTemplate, - @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date fltsFROMperiod, // YYYY/MM/DD start period of the - // required data - @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date fltsTOperiod, // YYYY/MM/DD end period of the - // required data - @RequestParam String codeformat, // IATA / ICAO / INTERNAL / AIRLINE – output of codes - // IATA - the system will generate the output only for routes with IATA codes, - // and the output codes will be IATA - // ICAO - the system will generate the output only for routes with ICAO codes, - // and the output codes will be ICAO - // INTERNAL - The system will generate the output with internal assigned codes - // for non IATA destinations, note that AeroCRS internal codes can conflict with - // real IATA destinations, the rest of the destinations will be in IATA codes. - // AIRLINE - The system will output the destinations in the airline defined - // codes - @RequestParam String companycode, // Company short code (as supplied to you by the team) - Adding this - // element will narrow the search of the data for the specific airline. - @RequestParam boolean soldonline, // when set to true will show only flights which are sold online. - @RequestParam(required = false) boolean ssim - // 1 - when the response should be in an SSIM format, 0 - string SSIM format - // (for non SSIM results do not send this prameter)) { - ) throws MalformedURLException, IOException, ParseException { + private final String URL_GET_SCHEDULE = "https://api.aerocrs.com/v5/getSchedule"; + private final Logger LOG = LoggerFactory.getLogger(AeroCRSRest.class); + + @Value("${auth.fares.id}") + private String faresAuthId; + + @Value("${auth.fares.password}") + private String faresAuthPassword; + + @Value("${auth.schedule.id}") + private String scheduleAuthId; + + @Value("${auth.schedule.password}") + private String scheduleAuthPassword; + + @Value("${fares.currency}") + private String currency; + + private RestTemplate restTemplate; + + public AeroCRSRest() { + restTemplate = new RestTemplate(); + } + + // IATA / ICAO / INTERNAL / AIRLINE – output of codes + // IATA - the system will generate the output only for routes with IATA codes, + // and the output codes will be IATA + // ICAO - the system will generate the output only for routes with ICAO codes, + // and the output codes will be ICAO + // INTERNAL - The system will generate the output with internal assigned codes + // for non IATA destinations, note that AeroCRS internal codes can conflict with + // real IATA destinations, the rest of the destinations will be in IATA codes. + // AIRLINE - The system will output the destinations in the airline defined + // codes + public AeroCRSGetScheduleSuccessResponse getSchedule(Date fltsFROMperiod, Date fltsTOperiod, String codeformat, + String companycode, boolean soldonline, boolean ssim) + throws MalformedURLException, IOException, ParseException { // create an instance of RestTemplate // RestTemplate restTemplate = new RestTemplate(); // create headers HttpHeaders headers = new HttpHeaders(); // headers.setBasicAuth(AUTH_ID, AUTH_PASSWORD); - headers.set("auth_id", AeroCRSConst.getAUTHID_STATIC()); - headers.set("auth_password", AeroCRSConst.getAUTHPASSWORD_STATIC()); + headers.set("auth_id", scheduleAuthId); + headers.set("auth_password", scheduleAuthPassword); // set `content-type` header headers.setContentType(MediaType.APPLICATION_JSON); // set `accept` header @@ -112,7 +126,38 @@ public static AeroCRSGetScheduleSuccessResponse getSchedule(RestTemplate restTem LOG.debug("" + response.getStatusCode()); } } - return (null); + return null; + } + + public AeroCRSFaresSuccessResponse getFares( + Date fltsFROMperiod, Date fltsTOperiod, String from, String to) { + HttpHeaders headers = new HttpHeaders(); + headers.set("auth_id", faresAuthId); + headers.set("auth_password", faresAuthPassword); + // set `content-type` header + headers.setContentType(MediaType.APPLICATION_JSON); + // set `accept` header + headers.setAccept(Arrays.asList(MediaType.ALL) /* Collections.singletonList(MediaType.APPLICATION_JSON) */); + // build the request + GetFaresRequest faresRequest = new GetFaresRequest(fltsFROMperiod, fltsTOperiod, from, to, currency); + // build the request + HttpEntity request = new HttpEntity<>(new AeroCRSRequest(new AeroCRSParms(faresRequest)), + headers); + + restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter()); + ResponseEntity response = restTemplate + .postForEntity(URL_GET_SCHEDULE, request, AeroCRSFaresSuccessResponse.class); + // check response + if (response.getStatusCode() == HttpStatus.OK && request.getBody() != null) { + LOG.debug("Request Successful"); + LOG.debug("" + response.getBody()); + return response.getBody(); + + } else { + LOG.debug("Request Failed"); + LOG.debug("" + response.getStatusCode()); + } + return null; } public static MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() { diff --git a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/AeroCRSConst.java b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/AeroCRSConst.java index 848dd3e8e..a17d95cbe 100644 --- a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/AeroCRSConst.java +++ b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/AeroCRSConst.java @@ -120,30 +120,5 @@ public static String getFromtodestination() { public static String getEnvperiod() { return ENVPERIOD; } - - @Value("${auth.id}") - public void setAUTHIDStatic(String authid){ - AeroCRSConst.AUTHID_STATIC = authid; - } - - @Value("${auth.password}") - public void setPASSWORDIDStatic(String authpassword){ - AeroCRSConst.AUTHPASSWORD_STATIC = authpassword; - } - - public String getAuthid() { - return authid; - } - - public String getAuthpassword() { - return authpassword; - } - - public static String getAUTHID_STATIC() { - return AUTHID_STATIC; - } - public static String getAUTHPASSWORD_STATIC() { - return AUTHPASSWORD_STATIC; - } } diff --git a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/SyncScheduler.java b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/SyncScheduler.java index 4d6264f00..ec3985d6a 100644 --- a/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/SyncScheduler.java +++ b/data-collectors/flightdata-skyalps/src/main/java/it/fos/noibz/skyalps/service/SyncScheduler.java @@ -15,10 +15,10 @@ import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; import it.bz.idm.bdp.dto.StationDto; import it.bz.idm.bdp.dto.StationList; +import it.fos.noibz.skyalps.dto.json.fares.AeroCRSFaresSuccessResponse; import it.fos.noibz.skyalps.dto.json.schedule.AeroCRSFlight; import it.fos.noibz.skyalps.dto.json.schedule.AeroCRSGetScheduleSuccessResponse; import it.fos.noibz.skyalps.rest.AeroCRSRest; @@ -56,9 +56,6 @@ public class SyncScheduler { @Autowired AeroCRSConst aeroconst; - @Autowired - RestTemplate template; - @Lazy @Autowired private AeroCRSRest acrsclient; @@ -101,7 +98,9 @@ public void syncJobStations() throws IOException, ParseException { to.add(Calendar.DATE, i + 1); Date fltsTOPeriod = to.getTime(); - AeroCRSGetScheduleSuccessResponse aero = acrsclient.getSchedule(template, fltsFROMPeriod, fltsTOPeriod, + // SCHEDULES + + AeroCRSGetScheduleSuccessResponse aero = acrsclient.getSchedule(fltsFROMPeriod, fltsTOPeriod, aeroconst.getIatacode(), aeroconst.getCompanycode(), false, ssimEnabled); for (AeroCRSFlight dto : aero.getAerocrs().getFlight()) { @@ -140,6 +139,14 @@ public void syncJobStations() throws IOException, ParseException { odhStationlist.add(stationDto); } + + // FARES + + + for (StationDto dto: odhStationlist) { + AeroCRSFaresSuccessResponse fares = acrsclient.getFares(fltsFROMPeriod, fltsTOPeriod, (String)dto.getMetaData().get(aeroconst.getFromdestination()), (String) dto.getMetaData().get(aeroconst.getTodestination())); + dto.getMetaData().put("fares", fares.getFares().getFares().getAdultFareOW()); + } } LOG.info("Trying to sync the stations..."); diff --git a/data-collectors/flightdata-skyalps/src/main/resources/application.properties b/data-collectors/flightdata-skyalps/src/main/resources/application.properties index 86af1c852..6a7ea3cf4 100644 --- a/data-collectors/flightdata-skyalps/src/main/resources/application.properties +++ b/data-collectors/flightdata-skyalps/src/main/resources/application.properties @@ -7,7 +7,7 @@ root_log_level=${LOG_LEVEL:info} #Cron setup to start: # We set it every 5 minutes as per requirement -scheduler.job_stations=${SCHEDULER_JOB_STATIONS:0 */5 * * * *} +scheduler.job_stations=${SCHEDULER_JOB_STATIONS:0/10 * * * * *} sched_days_before=${DAYS_BEFORE:0} sched_days_after=${DAYS_AFTER:60} #set scheduler pool size with the dafault value of 1 @@ -20,12 +20,18 @@ odh_client.provenance.name=${ODH_CLIENT_PROVENANCE_NAME:it.fois.noibz.skyalps} odh_client.provenance.version=${ODH_CLIENT_PROVENANCE_VERSION:0.0.0-local-dev} odh_client.provenance.origin=${ODH_CLIENT_PROVENANCE_ORIGIN:SKYALPS} odh_client.Flighturl = https://api.aerocrs.com/v5/getSchedule -auth.id=${AUTH_ID:default} -auth.password=${AUTH_PASSWORD:default} odh_client.STATION_ID_PREFIX = SKYALPS odh_client.LA = 46.46248 odh_client.LON = 11.32985 +# AUTH CRS API +auth.schedule.id=${AUTH_ID:default} +auth.schedule.password=${AUTH_PASSWORD:default} +auth.fares.id=${AUTH_ID_FARES:default} +auth.fares.password=${AUTH_PASSWORD_FARES:default} + +fares.currency=${FARES_CURRENCY:Euro} + #dc-interface conf bdp_host=localhost bdp_port=8999