Hi I'm new to reactive programming. I want to get a response from external API which has holiday calendar and count working days between given two days. Here is what I implement up to now. But I have no idea how to get holidays from it.
WebClientConfiguration.java
@Configuration
public class WebClientConfiguration {
@Bean
public WebClient webclient() {
final int size = 16 * 1024 * 1024;
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();
WebClient webClient = WebClient
.builder()
.baseUrl("http://222.165.138.144:5015/api/worklog/leaves")
.defaultCookie("cookieKey", "cookieValue")
.exchangeStrategies(strategies)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
return webClient;
}
}
@Service
public class CompanyCalandarServiceImpl implements CompanyCalendarService {
private static final Logger LOGGER = LoggerFactory.getLogger(CompanyCalandarServiceImpl.class);
@Autowired
WebClient webClient;
String Calandar = "http://222.165.138.144:5015/api/worklog/leaves";
@Override
public Flux<CompanyHolidayCalendar> getCompanyHolidays() {
return webClient.get()
.uri(Calandar)
.retrieve()
.bodyToFlux(CompanyHolidayCalendar.class)
.doOnError(throwable -> LOGGER.error("Failed for some reason", throwable));
}
}
companyHolidayCalanderDTO.java
@Data
public class CompanyHolidayCalendar {
private String date ;
private String summery;
private String description;
private String type;
}
controller
@GetMapping("/days-count")
public Mono<ResponseEntity<LmsApiResponse>> getDaysCount(@RequestParam("start_date") String startDate,
@RequestParam("end_date") String endDate) {
long startTime = System.currentTimeMillis();
LOGGER.info("getDaysCountRequest : StartDate={}|EndDate={}", startDate, endDate);
return leaveRequestService.getDaysCount(startDate, endDate).map(
response -> {
LOGGER.info("getDaysCountResponse : timeTaken={}|response={}", CommonUtil.getTimeTaken(startTime),
CommonUtil.convertToString(response));
return new ResponseEntity<>(
new LmsApiResponse(200, "Done!", response), HttpStatus.OK);
});
}
*Here I calculate working days without Saturday , Sunday and Holidays(coming from External API)
@Override
public Mono<Integer> getDaysCount(String startDate, String endDate) {
LOGGER.info("getDaysCount");
Flux<CompanyHolidayCalendar> holidays = companyCalendarService.getCompanyHolidays();
/*This return FluxPeek. How to get date from this and calculate working days?*/
System.out.println(holidays);
int workingDays = 0;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Calendar start = Calendar.getInstance();
start.setTime(sdf.parse(startDate));
Calendar end = Calendar.getInstance();
end.setTime(sdf.parse(endDate));
while (!start.after(end)) {
int day = start.get(Calendar.DAY_OF_WEEK);
day = day + 3;
if (day > 7) {
day = day - 7;
}
if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
workingDays++;
start.add(Calendar.DATE, 1);
}
System.out.println(workingDays);
} catch (Exception e) {
e.printStackTrace();
}
return Mono.just(workingDays);
}
P.S : I'm using Java 18 to develop this. And I get Start date and End Date from FrontEnd as String (dd/mm/yyyy) format and this holiday API response coming as a String (2022-12-15).
Well, I am just guessing here and trying to give you some directions without a sample project and I have not run the code, but I would try to do something like this in the
getDaysCount
method:in other words, I would try to do something like this where I get the
companyHolidays
from your service and then convert them into a list. Then you can do the rest of the calculations as you already do.Also, I would suggest you to think about exception handling. Generally, it is not a good idea to catch an exception and print the stack. You could return a Mono.error instead: