Skip to content

Commit

Permalink
flightdata-skyalps: extend get fares
Browse files Browse the repository at this point in the history
  • Loading branch information
dulvui committed Jan 18, 2023
1 parent 09ff76e commit b30e3ae
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 171 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
// }
// }
// }

};
}
// };
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

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;
import org.springframework.http.HttpStatus;
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;
Expand All @@ -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, [email protected]
*/
@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
Expand Down Expand Up @@ -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<AeroCRSRequest> request = new HttpEntity<>(new AeroCRSRequest(new AeroCRSParms(faresRequest)),
headers);

restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter());
ResponseEntity<AeroCRSFaresSuccessResponse> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,9 +56,6 @@ public class SyncScheduler {
@Autowired
AeroCRSConst aeroconst;

@Autowired
RestTemplate template;

@Lazy
@Autowired
private AeroCRSRest acrsclient;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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...");
Expand Down
Loading

0 comments on commit b30e3ae

Please sign in to comment.