Skip to content

Instantly share code, notes, and snippets.

@Raouf25
Last active August 10, 2020 00:26
Show Gist options
  • Save Raouf25/5f421f945812863c2832f137ef72e734 to your computer and use it in GitHub Desktop.
Save Raouf25/5f421f945812863c2832f137ef72e734 to your computer and use it in GitHub Desktop.
package com.rest.api.covid19;
import com.rest.api.covid19.domain.Country;
import com.rest.api.covid19.domain.Covid19Statistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
@Slf4j
@SpringBootApplication
public class Covid19apiApplication implements ApplicationRunner {
static final String COVID19_SUMMARY_URL = "https://api.covid19api.com/summary";
public static void main(String[] args) {
SpringApplication.run(Covid19apiApplication.class, args);
}
@Override
public void run(ApplicationArguments args) {
RestTemplate restTemplate = new RestTemplate();
final Covid19Statistics body = restTemplate.getForEntity(COVID19_SUMMARY_URL, Covid19Statistics.class)
.getBody();
assert body != null;
final List<Country> top5 = body.getCountries().stream()
.sorted(comparing(Country::getTotalDeaths, comparing(Math::abs)).reversed())
.limit(5)
.collect(Collectors.toList());
top5.forEach(country -> log.info(country.getCountryName() + " " + country.getTotalDeaths()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment