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.ArrayList; | |
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.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 = | |
40 | "https://api.ucsb.edu/dining/commons/v1/"; | |
41 | ||
42 | private final RestTemplate restTemplate; | |
43 | ||
44 | public DiningCommonsService(RestTemplateBuilder restTemplateBuilder) { | |
45 | restTemplate = restTemplateBuilder.build(); | |
46 | } | |
47 | ||
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 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
56 | ResponseEntity<String> re = | |
57 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
58 | ||
59 | String retBody = re.getBody(); | |
60 | List<DiningCommons> commons = | |
61 | mapper.readValue(retBody, new TypeReference<List<DiningCommons>>() {}); | |
62 | ||
63 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/DiningCommonsService::get → KILLED |
return commons; |
64 | } | |
65 | } | |
Mutations | ||
51 |
1.1 |
|
52 |
1.1 |
|
53 |
1.1 |
|
63 |
1.1 |