1 | package edu.ucsb.cs156.dining.services; | |
2 | ||
3 | import edu.ucsb.cs156.dining.models.Entree; | |
4 | ||
5 | import java.util.Arrays; | |
6 | import java.util.List; | |
7 | import com.fasterxml.jackson.databind.ObjectMapper; | |
8 | ||
9 | import com.fasterxml.jackson.core.JsonProcessingException; | |
10 | import com.fasterxml.jackson.core.type.TypeReference; | |
11 | import lombok.extern.slf4j.Slf4j; | |
12 | import org.springframework.beans.factory.annotation.Autowired; | |
13 | import org.springframework.beans.factory.annotation.Value; | |
14 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
15 | import org.springframework.http.HttpEntity; | |
16 | import org.springframework.http.HttpHeaders; | |
17 | import org.springframework.http.HttpMethod; | |
18 | import org.springframework.http.HttpStatus; | |
19 | import org.springframework.http.MediaType; | |
20 | import org.springframework.http.ResponseEntity; | |
21 | import org.springframework.stereotype.Service; | |
22 | import org.springframework.web.client.RestTemplate; | |
23 | ||
24 | @Service | |
25 | @Slf4j | |
26 | public class UCSBDiningMenuItemsService { | |
27 | ||
28 | @Autowired private ObjectMapper objectMapper; | |
29 | ||
30 | @Value("${app.ucsb.api.consumer_key}") | |
31 | private String apiKey; | |
32 | ||
33 | private RestTemplate restTemplate = new RestTemplate(); | |
34 | ||
35 | public UCSBDiningMenuItemsService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
36 | restTemplate = restTemplateBuilder.build(); | |
37 | } | |
38 | ||
39 | public static final String ALL_MEAL_ITEMS_AT_A_DINING_COMMON_ENDPOINT = | |
40 | "https://api.ucsb.edu/dining/menu/v1/{date-time}/{dining-common-code}/{meal-code}"; | |
41 | ||
42 | /** | |
43 | * Create a List of Entree from json representation | |
44 | * @param dateTime String of date in iso format | |
45 | * @param diningCommonCode String of dining common | |
46 | * @param mealCode String of meal code | |
47 | * @return a list of menu items | |
48 | */ | |
49 | ||
50 | public List<Entree> get(String dateTime, String diningCommonCode, String mealCode) throws JsonProcessingException { | |
51 | | |
52 | HttpHeaders headers = new HttpHeaders(); | |
53 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
54 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
55 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
56 | ||
57 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
58 | String url = ALL_MEAL_ITEMS_AT_A_DINING_COMMON_ENDPOINT; | |
59 | url = url.replace("{date-time}", dateTime); | |
60 | url = url.replace("{dining-common-code}", diningCommonCode); | |
61 | url = url.replace("{meal-code}", mealCode); | |
62 | ||
63 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
64 | String retBody = re.getBody(); | |
65 | ||
66 | List<Entree> menuItems = objectMapper.readValue(retBody, new TypeReference<List<Entree>>() {}); | |
67 | ||
68 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::get → KILLED |
return menuItems; |
69 | } | |
70 | ||
71 | } | |
Mutations | ||
53 |
1.1 |
|
54 |
1.1 |
|
55 |
1.1 |
|
68 |
1.1 |