| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 4 | ||
| 5 | import java.time.LocalDateTime; | |
| 6 | import java.time.format.DateTimeFormatter; | |
| 7 | ||
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.Arrays; | |
| 10 | import lombok.extern.slf4j.Slf4j; | |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.beans.factory.annotation.Value; | |
| 13 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 14 | import org.springframework.http.HttpEntity; | |
| 15 | import org.springframework.http.HttpHeaders; | |
| 16 | import org.springframework.http.HttpMethod; | |
| 17 | import org.springframework.http.HttpStatus; | |
| 18 | import org.springframework.http.MediaType; | |
| 19 | import org.springframework.http.ResponseEntity; | |
| 20 | import org.springframework.stereotype.Service; | |
| 21 | import org.springframework.web.client.RestTemplate; | |
| 22 | ||
| 23 | /** Service object that wraps the UCSB Dining Menu API */ | |
| 24 | @Service | |
| 25 | @Slf4j | |
| 26 | public class UCSBDiningMenuService { | |
| 27 | ||
| 28 | @Value("${app.ucsb.api.consumer_key}") | |
| 29 | private String apiKey; | |
| 30 | ||
| 31 | private RestTemplate restTemplate = new RestTemplate(); | |
| 32 | ||
| 33 | public UCSBDiningMenuService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 34 | restTemplate = restTemplateBuilder.build(); | |
| 35 | } | |
| 36 | ||
| 37 | public static final String ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT = | |
| 38 | "https://api.ucsb.edu/dining/menu/v1/{date-time}/{dining-common-code}"; | |
| 39 | ||
| 40 | public String getJSON(String dateTime, String diningCommonCode) throws Exception { | |
| 41 | ||
| 42 | HttpHeaders headers = new HttpHeaders(); | |
| 43 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 44 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 45 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 46 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 47 | ||
| 48 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 49 | ||
| 50 | String url = ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT; | |
| 51 | url.replace("{date-time}", dateTime); | |
| 52 | url.replace("{dining-common-code}", diningCommonCode); | |
| 53 | | |
| 54 | ||
| 55 | log.info("url=" + url); | |
| 56 | ||
| 57 | String retVal = ""; | |
| 58 | MediaType contentType = null; | |
| 59 | HttpStatus statusCode = null; | |
| 60 | ||
| 61 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, dateTime, diningCommonCode); | |
| 62 | contentType = re.getHeaders().getContentType(); | |
| 63 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 64 | retVal = re.getBody(); | |
| 65 | ||
| 66 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 67 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED |
return retVal; |
| 68 | } | |
| 69 | } | |
| 70 | ||
Mutations | ||
| 43 |
1.1 |
|
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 67 |
1.1 |