| 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.time.Duration; | |
| 8 | import java.util.List; | |
| 9 | import lombok.extern.slf4j.Slf4j; | |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | import org.springframework.beans.factory.annotation.Value; | |
| 12 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 13 | import org.springframework.cache.annotation.Cacheable; | |
| 14 | import org.springframework.http.HttpEntity; | |
| 15 | import org.springframework.http.HttpHeaders; | |
| 16 | import org.springframework.http.HttpMethod; | |
| 17 | import org.springframework.http.MediaType; | |
| 18 | import org.springframework.http.ResponseEntity; | |
| 19 | import org.springframework.stereotype.Service; | |
| 20 | import org.springframework.web.client.RestTemplate; | |
| 21 | ||
| 22 | // Reworked from proj-course's UCSBSubjectsService.java : | |
| 23 | // https://github.com/ucsb-cs156/proj-courses/blob/main/src/main/java/edu/ucsb/cs156/courses/services/UCSBSubjectsService.java | |
| 24 | ||
| 25 | // Uses UCSB's developer api for dining commons. | |
| 26 | // https://developer.ucsb.edu/apis/dining/dining-commons | |
| 27 | ||
| 28 | // Service for the dining commons page. | |
| 29 | // This service particularly utilizes an endpoint for getting all dining commons from the above api. | |
| 30 | ||
| 31 | @Slf4j | |
| 32 | @Service("DiningCommons") | |
| 33 | public class DiningCommonsService { | |
| 34 | ||
| 35 | @Autowired private ObjectMapper mapper; | |
| 36 | ||
| 37 | @Value("${app.ucsb.api.consumer_key}") | |
| 38 | private String apiKey; | |
| 39 | ||
| 40 | @Value("${app.ucsb.api.host}") | |
| 41 | private String apiHost; | |
| 42 | ||
| 43 | @Value("${app.name}") | |
| 44 | private String appName; | |
| 45 | ||
| 46 | @Value("${app.hostname}") | |
| 47 | private String appHostname; | |
| 48 | ||
| 49 | public static final String ENDPOINT = "{apiHost}/dining/commons/v1/"; | |
| 50 | ||
| 51 | /* | |
| 52 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 53 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 54 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 55 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 56 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 57 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 58 | * can't hang forever. | |
| 59 | */ | |
| 60 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 61 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 62 | ||
| 63 | private final RestTemplate restTemplate; | |
| 64 | ||
| 65 | public DiningCommonsService(RestTemplateBuilder restTemplateBuilder) { | |
| 66 | restTemplate = | |
| 67 | restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 68 | } | |
| 69 | ||
| 70 | @Cacheable("diningCommons") | |
| 71 | public List<DiningCommons> get() throws JsonProcessingException { | |
| 72 | ||
| 73 | HttpHeaders headers = new HttpHeaders(); | |
| 74 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 75 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 76 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 77 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("X-Requesting-App", this.appName + "." + this.appHostname); |
| 78 | ||
| 79 | log.info("Fetching dining commons from UCSB API"); | |
| 80 | ||
| 81 | String url = ENDPOINT.replace("{apiHost}", apiHost); | |
| 82 | ||
| 83 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 84 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 85 | ||
| 86 | String retBody = re.getBody(); | |
| 87 | List<DiningCommons> commons = | |
| 88 | mapper.readValue(retBody, new TypeReference<List<DiningCommons>>() {}); | |
| 89 | ||
| 90 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/DiningCommonsService::get → KILLED |
return commons; |
| 91 | } | |
| 92 | } | |
Mutations | ||
| 74 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 90 |
1.1 |