| 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.HttpClientErrorException; | |
| 15 | import org.springframework.web.client.HttpServerErrorException; | |
| 16 | import org.springframework.web.client.RestTemplate; | |
| 17 | ||
| 18 | /** Service object that wraps the UCSB Dining Menu API */ | |
| 19 | @Service | |
| 20 | @Slf4j | |
| 21 | public class UCSBDiningMenuService { | |
| 22 | ||
| 23 | @Value("${app.ucsb.api.consumer_key}") | |
| 24 | private String apiKey; | |
| 25 | ||
| 26 | private RestTemplate restTemplate = new RestTemplate(); | |
| 27 | ||
| 28 | public UCSBDiningMenuService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 29 | restTemplate = restTemplateBuilder.build(); | |
| 30 | } | |
| 31 | ||
| 32 | public static final String ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT = | |
| 33 | "https://api.ucsb.edu/dining/menu/v1/{date-time}/{dining-common-code}"; | |
| 34 | ||
| 35 | public String getJSON(String dateTime, String diningCommonCode) throws Exception { | |
| 36 | ||
| 37 | HttpHeaders headers = new HttpHeaders(); | |
| 38 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 39 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 40 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 41 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 42 | ||
| 43 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 44 | ||
| 45 | String url = ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT; | |
| 46 | url.replace("{date-time}", dateTime); | |
| 47 | url.replace("{dining-common-code}", diningCommonCode); | |
| 48 | ||
| 49 | log.info("url=" + url); | |
| 50 | ||
| 51 | String retVal = ""; | |
| 52 | MediaType contentType = null; | |
| 53 | HttpStatus statusCode = null; | |
| 54 | ||
| 55 | try { | |
| 56 | ResponseEntity<String> re = | |
| 57 | restTemplate.exchange( | |
| 58 | url, HttpMethod.GET, entity, String.class, dateTime, diningCommonCode); | |
| 59 | contentType = re.getHeaders().getContentType(); | |
| 60 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 61 | retVal = re.getBody(); | |
| 62 | ||
| 63 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 64 | } catch (HttpClientErrorException | HttpServerErrorException e) { | |
| 65 | log.info("Dining commons {} closed on {}: {}", diningCommonCode, dateTime, e.getMessage()); | |
| 66 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED |
return null; |
| 67 | } | |
| 68 | ||
| 69 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED |
return retVal; |
| 70 | } | |
| 71 | } | |
Mutations | ||
| 38 |
1.1 |
|
| 39 |
1.1 |
|
| 40 |
1.1 |
|
| 41 |
1.1 |
|
| 66 |
1.1 |
|
| 69 |
1.1 |