| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.dining.models.DiningCommon; | |
| 6 | import java.util.ArrayList; | |
| 7 | import java.util.List; | |
| 8 | import lombok.extern.slf4j.Slf4j; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.beans.factory.annotation.Value; | |
| 11 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 12 | import org.springframework.http.HttpEntity; | |
| 13 | import org.springframework.http.HttpHeaders; | |
| 14 | import org.springframework.http.HttpMethod; | |
| 15 | import org.springframework.http.HttpStatusCode; | |
| 16 | import org.springframework.http.MediaType; | |
| 17 | import org.springframework.http.ResponseEntity; | |
| 18 | import org.springframework.stereotype.Service; | |
| 19 | import org.springframework.web.client.RestTemplate; | |
| 20 | ||
| 21 | /** Service object that wraps the UCSB Dining Commons API */ | |
| 22 | @Service | |
| 23 | @Slf4j | |
| 24 | public class DiningCommonsService { | |
| 25 | ||
| 26 | @Value("${app.ucsb.api.consumer_key}") | |
| 27 | private String apiKey; | |
| 28 | ||
| 29 | private final RestTemplate restTemplate; | |
| 30 | private final ObjectMapper objectMapper; | |
| 31 | ||
| 32 | public static final String DINING_COMMONS_ENDPOINT = "https://api.ucsb.edu/dining/commons/v1/"; | |
| 33 | ||
| 34 | public DiningCommonsService(RestTemplateBuilder restTemplateBuilder, ObjectMapper objectMapper) { | |
| 35 | this.restTemplate = restTemplateBuilder.build(); | |
| 36 | this.objectMapper = objectMapper; | |
| 37 | } | |
| 38 | ||
| 39 | public List<DiningCommon> getAllDiningCommons() throws Exception { | |
| 40 | try { | |
| 41 | HttpHeaders headers = new HttpHeaders(); | |
| 42 |
1
1. getAllDiningCommons : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 43 |
1
1. getAllDiningCommons : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 44 |
1
1. getAllDiningCommons : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", apiKey); |
| 45 | ||
| 46 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 47 | ||
| 48 | log.info("Fetching data from URL: {}", DINING_COMMONS_ENDPOINT); | |
| 49 | ||
| 50 | ResponseEntity<String> response = restTemplate.exchange(DINING_COMMONS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 51 | ||
| 52 | | |
| 53 | ||
| 54 |
1
1. getAllDiningCommons : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/DiningCommonsService::getAllDiningCommons → KILLED |
return objectMapper.readValue(response.getBody(), new TypeReference<List<DiningCommon>>() {}); |
| 55 | } catch (Exception e) { | |
| 56 | log.error("Error occurred while fetching dining commons", e); | |
| 57 | throw new RuntimeException("Error fetching dining commons", e); | |
| 58 | } | |
| 59 | } | |
| 60 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 43 |
1.1 |
|
| 44 |
1.1 |
|
| 54 |
1.1 |