Skip to content

Instantly share code, notes, and snippets.

@MelulekiDube
Created April 9, 2020 16:51
Show Gist options
  • Save MelulekiDube/c991bfd37ae7e4e3efec3784ac769fbe to your computer and use it in GitHub Desktop.
Save MelulekiDube/c991bfd37ae7e4e3efec3784ac769fbe to your computer and use it in GitHub Desktop.
package com.meluleki.corona.CoronaVirusApplicaion;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
public class CoronaController {
private RestTemplate rest;
private HttpHeaders headers;
private HttpStatus status;
CoronaController() {
this.rest = new RestTemplate();
this.headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Accept", "*/*");
}
@GetMapping("/fetchCountry")
public ResponseEntity<Country> fetchCountry(@RequestParam(value = "Country", defaultValue = "China") String name) {
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
Country[] countries = rest.getForObject("https://api.covid19api.com/countries", Country[].class);
if (countries != null) {
for (Country country : countries) {
if (name.equals(country.getCountry())) {
return new ResponseEntity<>(country, HttpStatus.OK);
}
}
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
// HttpClient client = ;
// HttpRequest request = HttpRequest.newBuilder()
// .uri(URI.create("https://api.covid19api.com/countries"))
// .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
// .build();
// String test;
// HttpHeaders headers = new HttpHeaders();
// try {
// test = client
// .send(request, HttpResponse.BodyHandlers.ofString()).body();
// return test;
// }catch (InterruptedException | IOException e) {
// e.printStackTrace();
// return null;
// }
// }
//private Country parseToCountry(String jsonBody)
//{
// }
package com.meluleki.corona.CoronaVirusApplicaion;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@JsonDeserialize()
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {
private String Country;
private String Slug;
private String ISO2;
@JsonCreator
public Country(@JsonProperty("Country") String country,
@JsonProperty("Slug") String slug,
@JsonProperty("ISO2") String iso2
) {
Country = country;
Slug = slug;
ISO2 = iso2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment