| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | import java.time.LocalDateTime; | |
| 5 | import java.util.Arrays; | |
| 6 | import java.util.HashMap; | |
| 7 | import java.util.List; | |
| 8 | import java.util.Map; | |
| 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.HttpStatus; | |
| 17 | import org.springframework.http.MediaType; | |
| 18 | import org.springframework.http.ResponseEntity; | |
| 19 | import org.springframework.stereotype.Service; | |
| 20 | import org.springframework.web.client.RestTemplate; | |
| 21 | ||
| 22 | /** Service object that wraps the UCSB Dining Commons API */ | |
| 23 | @Service | |
| 24 | @Slf4j | |
| 25 | public class DiningCommonsService { | |
| 26 | ||
| 27 | @Autowired private ObjectMapper objectMapper; | |
| 28 | ||
| 29 | @Value("${app.ucsb.api.consumer_key}") | |
| 30 | private String apiKey; | |
| 31 | ||
| 32 | private RestTemplate restTemplate = new RestTemplate(); | |
| 33 | ||
| 34 | public DiningCommonsService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 35 | restTemplate = restTemplateBuilder.build(); | |
| 36 | } | |
| 37 | ||
| 38 | public static final String NAMES_ENDPOINT = | |
| 39 | "https://api.ucsb.edu/dining/commons/v1/names"; | |
| 40 | ||
| 41 | public static final String MEALS_BY_DATE_ENDPOINT = "https://api.ucsb.edu/dining/menu/v1/"; | |
| 42 | ||
| 43 | public String getDiningCommonsJSON() throws Exception { | |
| 44 | ||
| 45 | HttpHeaders headers = new HttpHeaders(); | |
| 46 |
1
1. getDiningCommonsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 47 |
1
1. getDiningCommonsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 48 |
1
1. getDiningCommonsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 49 |
1
1. getDiningCommonsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 50 | ||
| 51 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 52 | ||
| 53 | String url = NAMES_ENDPOINT; | |
| 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); | |
| 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. getDiningCommonsJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/DiningCommonsService::getDiningCommonsJSON → KILLED |
return retVal; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Get the meals for a specific dining commons by date | |
| 72 | * @param date | |
| 73 | * @param diningCommonsCode | |
| 74 | * @return meals by date by dining common | |
| 75 | * @throws Exception | |
| 76 | */ | |
| 77 | public String getMealsByDateJSON(LocalDateTime date, String diningCommonsCode) throws Exception { | |
| 78 | ||
| 79 | // set headers for api requests | |
| 80 | HttpHeaders headers = new HttpHeaders(); | |
| 81 |
1
1. getMealsByDateJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 82 |
1
1. getMealsByDateJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 83 |
1
1. getMealsByDateJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 84 |
1
1. getMealsByDateJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 85 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 86 | ||
| 87 | // set the date in the format that the api expects | |
| 88 | String formattedDate = date.toString(); | |
| 89 | log.info("formattedDate: {}", formattedDate); | |
| 90 | ||
| 91 | // set the url for the api request (https://api.ucsb.edu/dining/menu/v1/2024-10-21T12%3A00%3A00/carrillo) | |
| 92 | String url = MEALS_BY_DATE_ENDPOINT + date.toString() + "/" + diningCommonsCode; | |
| 93 | ||
| 94 | ||
| 95 | // get the information from request | |
| 96 | String retVal = ""; | |
| 97 | MediaType contentType = null; | |
| 98 | HttpStatus statusCode = null; | |
| 99 | ||
| 100 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 101 | contentType = re.getHeaders().getContentType(); | |
| 102 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 103 | retVal = re.getBody(); | |
| 104 | ||
| 105 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 106 |
1
1. getMealsByDateJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/DiningCommonsService::getMealsByDateJSON → KILLED |
return retVal; |
| 107 | } | |
| 108 | ||
| 109 | ||
| 110 | /** | |
| 111 | * Get the meals for a specific dining commons by date | |
| 112 | * @param meal | |
| 113 | * @param date | |
| 114 | * @param diningCommonsCode | |
| 115 | * @return meals by date by dining common | |
| 116 | * @throws Exception | |
| 117 | */ | |
| 118 | public String getMenuItemsByMealAndDateJSON(LocalDateTime date, String diningCommonsCode, String meal) throws Exception { | |
| 119 | ||
| 120 | // set headers for api requests | |
| 121 | HttpHeaders headers = new HttpHeaders(); | |
| 122 |
1
1. getMenuItemsByMealAndDateJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 123 |
1
1. getMenuItemsByMealAndDateJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 124 |
1
1. getMenuItemsByMealAndDateJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 125 |
1
1. getMenuItemsByMealAndDateJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 126 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 127 | ||
| 128 | // set the date in the format that the api expects | |
| 129 | String formattedDate = date.toString(); | |
| 130 | log.info("formattedDate: {}", formattedDate); | |
| 131 | ||
| 132 | // set the url for the api request e.g. (https://api.ucsb.edu/dining/menu/v1/2021-02-10/ortega/lunch) | |
| 133 | String url = MEALS_BY_DATE_ENDPOINT + date.toString() + "/" + diningCommonsCode + "/" + meal; | |
| 134 | ||
| 135 | ||
| 136 | // get the information from request | |
| 137 | String retVal = ""; | |
| 138 | MediaType contentType = null; | |
| 139 | HttpStatus statusCode = null; | |
| 140 | ||
| 141 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 142 | contentType = re.getHeaders().getContentType(); | |
| 143 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 144 | retVal = re.getBody(); | |
| 145 | ||
| 146 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 147 |
1
1. getMenuItemsByMealAndDateJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/DiningCommonsService::getMenuItemsByMealAndDateJSON → KILLED |
return retVal; |
| 148 | } | |
| 149 | } | |
Mutations | ||
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 49 |
1.1 |
|
| 67 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 84 |
1.1 |
|
| 106 |
1.1 |
|
| 122 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 125 |
1.1 |
|
| 147 |
1.1 |