| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.dining.models.DiningCommons; | |
| 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.cache.annotation.Cacheable; | |
| 13 | import org.springframework.http.HttpEntity; | |
| 14 | import org.springframework.http.HttpHeaders; | |
| 15 | import org.springframework.http.HttpMethod; | |
| 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 | // Reworked from proj-course's UCSBSubjectsService.java : | |
| 22 | // https://github.com/ucsb-cs156/proj-courses/blob/main/src/main/java/edu/ucsb/cs156/courses/services/UCSBSubjectsService.java | |
| 23 | ||
| 24 | // Uses UCSB's developer api for dining commons. | |
| 25 | // https://developer.ucsb.edu/apis/dining/dining-commons | |
| 26 | ||
| 27 | // Service for the dining commons page. | |
| 28 | // This service particularly utilizes an endpoint for getting all dining commons from the above api. | |
| 29 | ||
| 30 | @Slf4j | |
| 31 | @Service("DiningCommons") | |
| 32 | public class DiningCommonsService { | |
| 33 | ||
| 34 | @Autowired private ObjectMapper mapper; | |
| 35 | ||
| 36 | @Value("${app.ucsb.api.consumer_key}") | |
| 37 | private String apiKey; | |
| 38 | ||
| 39 | public static final String ENDPOINT = "https://api.ucsb.edu/dining/commons/v1/"; | |
| 40 | ||
| 41 | private final RestTemplate restTemplate; | |
| 42 | ||
| 43 | public DiningCommonsService(RestTemplateBuilder restTemplateBuilder) { | |
| 44 | restTemplate = restTemplateBuilder.build(); | |
| 45 | } | |
| 46 | ||
| 47 | @Cacheable("diningCommons") | |
| 48 | public List<DiningCommons> get() throws JsonProcessingException { | |
| 49 | ||
| 50 | HttpHeaders headers = new HttpHeaders(); | |
| 51 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 52 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 53 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 54 | ||
| 55 | log.info("Fetching dining commons from UCSB API"); | |
| 56 | ||
| 57 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 58 | ResponseEntity<String> re = | |
| 59 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 60 | ||
| 61 | String retBody = re.getBody(); | |
| 62 | List<DiningCommons> commons = | |
| 63 | mapper.readValue(retBody, new TypeReference<List<DiningCommons>>() {}); | |
| 64 | ||
| 65 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/DiningCommonsService::get → KILLED |
return commons; |
| 66 | } | |
| 67 | } | |
Mutations | ||
| 51 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 65 |
1.1 |