| 1 | package edu.ucsb.cs156.spring.backenddemo.services; | |
| 2 | ||
| 3 | import java.io.StringReader; | |
| 4 | import java.util.List; | |
| 5 | ||
| 6 | ||
| 7 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 8 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 9 | import com.opencsv.bean.CsvToBeanBuilder; | |
| 10 | ||
| 11 | import org.springframework.web.client.RestTemplate; | |
| 12 | ||
| 13 | import edu.ucsb.cs156.spring.backenddemo.beans.CollegeSubreddit; | |
| 14 | import lombok.extern.slf4j.Slf4j; | |
| 15 | ||
| 16 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 17 | import org.springframework.http.HttpEntity; | |
| 18 | import org.springframework.http.HttpHeaders; | |
| 19 | import org.springframework.http.HttpMethod; | |
| 20 | import org.springframework.http.MediaType; | |
| 21 | import org.springframework.http.ResponseEntity; | |
| 22 | import org.springframework.stereotype.Service; | |
| 23 | import org.springframework.web.client.HttpClientErrorException; | |
| 24 | ||
| 25 | @Service | |
| 26 | public class CollegeSubredditQueryService { | |
| 27 | ||
| 28 | private ObjectMapper mapper = new ObjectMapper(); | |
| 29 | ||
| 30 | private final RestTemplate restTemplate; | |
| 31 | ||
| 32 | public CollegeSubredditQueryService(RestTemplateBuilder restTemplateBuilder) { | |
| 33 | restTemplate = restTemplateBuilder.build(); | |
| 34 | } | |
| 35 | ||
| 36 | public static final String ENDPOINT = "https://raw.githubusercontent.com/karlding/college-subreddits/master/data/colleges.csv"; | |
| 37 | ||
| 38 | public String getJSON() throws HttpClientErrorException, JsonProcessingException { | |
| 39 | | |
| 40 | HttpHeaders headers = new HttpHeaders(); | |
| 41 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.TEXT_PLAIN)); |
| 42 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 43 | ||
| 44 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 45 | ||
| 46 | ResponseEntity<String> re = restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 47 | String csvData = re.getBody(); | |
| 48 | List<CollegeSubreddit> subreddits = new CsvToBeanBuilder<CollegeSubreddit>(new StringReader(csvData)).withType(CollegeSubreddit.class).build().parse(); | |
| 49 | String jsonData = mapper.writeValueAsString(subreddits); | |
| 50 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/spring/backenddemo/services/CollegeSubredditQueryService::getJSON → KILLED |
return jsonData; |
| 51 | } | |
| 52 | ||
| 53 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 42 |
1.1 |
|
| 50 |
1.1 |