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