1 | package edu.ucsb.cs156.courses.services; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
6 | import edu.ucsb.cs156.courses.documents.CoursePage; | |
7 | import java.util.Arrays; | |
8 | import java.util.HashMap; | |
9 | import java.util.List; | |
10 | import java.util.Map; | |
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.HttpClientErrorException; | |
23 | import org.springframework.web.client.RestTemplate; | |
24 | import org.springframework.web.util.UriComponentsBuilder; | |
25 | ||
26 | /** Service object that wraps the UCSB Academic Curriculum API */ | |
27 | @Service | |
28 | @Slf4j | |
29 | public class UCSBCurriculumService { | |
30 | ||
31 | @Autowired private ObjectMapper objectMapper; | |
32 | ||
33 | @Value("${app.ucsb.api.consumer_key}") | |
34 | private String apiKey; | |
35 | ||
36 | private RestTemplate restTemplate = new RestTemplate(); | |
37 | ||
38 | public UCSBCurriculumService(RestTemplateBuilder restTemplateBuilder) { | |
39 | restTemplate = restTemplateBuilder.build(); | |
40 | } | |
41 | ||
42 | public static final String CURRICULUM_ENDPOINT = | |
43 | "https://api.ucsb.edu/academics/curriculums/v1/classes/search"; | |
44 | ||
45 | public static final String SUBJECTS_ENDPOINT = | |
46 | "https://api.ucsb.edu/students/lookups/v1/subjects"; | |
47 | ||
48 | public static final String SECTION_ENDPOINT = | |
49 | "https://api.ucsb.edu/academics/curriculums/v1/classsection/{quarter}/{enrollcode}"; | |
50 | ||
51 | public static final String ALL_SECTIONS_ENDPOINT = | |
52 | "https://api.ucsb.edu/academics/curriculums/v3/classes/{quarter}/{enrollcode}"; | |
53 | ||
54 | public static final String FINALS_ENDPOINT = | |
55 | "https://api.ucsb.edu/academics/curriculums/v3/finals"; | |
56 | ||
57 | public String getJSON(String subjectArea, String quarter, String courseLevel) { | |
58 | ||
59 | HttpHeaders headers = new HttpHeaders(); | |
60 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
61 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
62 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
63 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
64 | ||
65 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
66 | ||
67 | String params = | |
68 | String.format( | |
69 | "?quarter=%s&subjectCode=%s&objLevelCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
70 | quarter, subjectArea, courseLevel, 1, 100, "true"); | |
71 | String url = CURRICULUM_ENDPOINT + params; | |
72 | ||
73 |
1
1. getJSON : negated conditional → KILLED |
if (courseLevel.equals("A")) { |
74 | params = | |
75 | String.format( | |
76 | "?quarter=%s&subjectCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
77 | quarter, subjectArea, 1, 100, "true"); | |
78 | url = CURRICULUM_ENDPOINT + params; | |
79 | } | |
80 | ||
81 | log.info("url=" + url); | |
82 | ||
83 | String retVal = ""; | |
84 | MediaType contentType = null; | |
85 | HttpStatus statusCode = null; | |
86 | try { | |
87 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
88 | contentType = re.getHeaders().getContentType(); | |
89 | statusCode = re.getStatusCode(); | |
90 | retVal = re.getBody(); | |
91 | } catch (HttpClientErrorException e) { | |
92 | retVal = "{\"error\": \"401: Unauthorized\"}"; | |
93 | } | |
94 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
95 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSON → KILLED |
return retVal; |
96 | } | |
97 | ||
98 | public List<ConvertedSection> getConvertedSections( | |
99 | String subjectArea, String quarter, String courseLevel) throws JsonProcessingException { | |
100 | String json = getJSON(subjectArea, quarter, courseLevel); | |
101 | CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
102 | List<ConvertedSection> result = coursePage.convertedSections(); | |
103 |
1
1. getConvertedSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getConvertedSections → KILLED |
return result; |
104 | } | |
105 | ||
106 | public String getSectionJSON(String subjectArea, String quarter, String courseLevel) | |
107 | throws JsonProcessingException { | |
108 | List<ConvertedSection> l = getConvertedSections(subjectArea, quarter, courseLevel); | |
109 | ||
110 | String arrayToJson = objectMapper.writeValueAsString(l); | |
111 | ||
112 |
1
1. getSectionJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSectionJSON → KILLED |
return arrayToJson; |
113 | } | |
114 | ||
115 | public String getSubjectsJSON() { | |
116 | ||
117 | HttpHeaders headers = new HttpHeaders(); | |
118 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
119 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
120 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
121 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
122 | ||
123 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
124 | ||
125 | log.info("url=" + SUBJECTS_ENDPOINT); | |
126 | ||
127 | String retVal = ""; | |
128 | MediaType contentType = null; | |
129 | HttpStatus statusCode = null; | |
130 | try { | |
131 | ResponseEntity<String> re = | |
132 | restTemplate.exchange(SUBJECTS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
133 | contentType = re.getHeaders().getContentType(); | |
134 | statusCode = re.getStatusCode(); | |
135 | retVal = re.getBody(); | |
136 | } catch (HttpClientErrorException e) { | |
137 | retVal = "{\"error\": \"401: Unauthorized\"}"; | |
138 | } | |
139 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
140 |
1
1. getSubjectsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSubjectsJSON → KILLED |
return retVal; |
141 | } | |
142 | ||
143 | /** | |
144 | * This method retrieves exactly one section matching the enrollCode and quarter arguments, if | |
145 | * such a section exists. | |
146 | */ | |
147 | public String getSection(String enrollCode, String quarter) { | |
148 | ||
149 | HttpHeaders headers = new HttpHeaders(); | |
150 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
151 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
152 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
153 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
154 | ||
155 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
156 | ||
157 | String url = SECTION_ENDPOINT; | |
158 | ||
159 | log.info("url=" + url); | |
160 | ||
161 | String urlTemplate = | |
162 | UriComponentsBuilder.fromHttpUrl(url) | |
163 | .queryParam("quarter", "{quarter}") | |
164 | .queryParam("enrollcode", "{enrollcode}") | |
165 | .encode() | |
166 | .toUriString(); | |
167 | ||
168 | Map<String, String> params = new HashMap<>(); | |
169 | params.put("quarter", quarter); | |
170 | params.put("enrollcode", enrollCode); | |
171 | ||
172 | String retVal = ""; | |
173 | MediaType contentType = null; | |
174 | HttpStatus statusCode = null; | |
175 | try { | |
176 | ResponseEntity<String> re = | |
177 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
178 | contentType = re.getHeaders().getContentType(); | |
179 | statusCode = re.getStatusCode(); | |
180 | retVal = re.getBody(); | |
181 | } catch (HttpClientErrorException e) { | |
182 | retVal = "{\"error\": \"401: Unauthorized\"}"; | |
183 | } | |
184 | ||
185 |
1
1. getSection : negated conditional → KILLED |
if (retVal.equals("null")) { |
186 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
187 | } | |
188 | ||
189 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
190 |
1
1. getSection : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSection → KILLED |
return retVal; |
191 | } | |
192 | ||
193 | /** | |
194 | * This method retrieves all of the sections related to a certain enroll code. For example, if the | |
195 | * enrollCode is for a discussion section, the lecture section and all related discussion sections | |
196 | * will also be returned. | |
197 | */ | |
198 | public String getAllSections(String enrollCode, String quarter) { | |
199 | ||
200 | HttpHeaders headers = new HttpHeaders(); | |
201 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
202 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
203 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
204 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
205 | ||
206 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
207 | ||
208 | String url = ALL_SECTIONS_ENDPOINT; | |
209 | ||
210 | log.info("url=" + url); | |
211 | ||
212 | String urlTemplate = | |
213 | UriComponentsBuilder.fromHttpUrl(url) | |
214 | .queryParam("quarter", "{quarter}") | |
215 | .queryParam("enrollcode", "{enrollcode}") | |
216 | .encode() | |
217 | .toUriString(); | |
218 | ||
219 | Map<String, String> params = new HashMap<>(); | |
220 | params.put("quarter", quarter); | |
221 | params.put("enrollcode", enrollCode); | |
222 | ||
223 | String retVal = ""; | |
224 | MediaType contentType = null; | |
225 | HttpStatus statusCode = null; | |
226 | try { | |
227 | ResponseEntity<String> re = | |
228 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
229 | contentType = re.getHeaders().getContentType(); | |
230 | statusCode = re.getStatusCode(); | |
231 | retVal = re.getBody(); | |
232 | } catch (HttpClientErrorException e) { | |
233 | retVal = "{\"error\": \"401: Unauthorized\"}"; | |
234 | } | |
235 | ||
236 |
1
1. getAllSections : negated conditional → KILLED |
if (retVal.equals("null")) { |
237 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
238 | } | |
239 | ||
240 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
241 |
1
1. getAllSections : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getAllSections → KILLED |
return retVal; |
242 | } | |
243 | ||
244 | public String getJSONbyQtrEnrollCd(String quarter, String enrollCd) { | |
245 | ||
246 | HttpHeaders headers = new HttpHeaders(); | |
247 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
248 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
249 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
250 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
251 | ||
252 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
253 | ||
254 | String url = | |
255 | "https://api.ucsb.edu/academics/curriculums/v3/classsection/" + quarter + "/" + enrollCd; | |
256 | ||
257 | log.info("url=" + url); | |
258 | ||
259 | String retVal = ""; | |
260 | MediaType contentType = null; | |
261 | HttpStatus statusCode = null; | |
262 | try { | |
263 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
264 | contentType = re.getHeaders().getContentType(); | |
265 | statusCode = re.getStatusCode(); | |
266 | retVal = re.getBody(); | |
267 | } catch (HttpClientErrorException e) { | |
268 | retVal = "{\"error\": \"401: Unauthorized\"}"; | |
269 | } | |
270 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
271 |
1
1. getJSONbyQtrEnrollCd : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSONbyQtrEnrollCd → KILLED |
return retVal; |
272 | } | |
273 | ||
274 | public String getFinalsJSON(String quarter, String enrollCode) { | |
275 | ||
276 | HttpHeaders headers = new HttpHeaders(); | |
277 |
1
1. getFinalsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
278 |
1
1. getFinalsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
279 |
1
1. getFinalsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
280 |
1
1. getFinalsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
281 | ||
282 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
283 | ||
284 | String params = String.format("?quarter=%s&enrollCode=%s", quarter, enrollCode); | |
285 | ||
286 | String url = FINALS_ENDPOINT + params; | |
287 | ||
288 | log.info("url=" + url); | |
289 | ||
290 | String retVal = ""; | |
291 | MediaType contentType = null; | |
292 | HttpStatus statusCode = null; | |
293 | try { | |
294 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
295 | contentType = re.getHeaders().getContentType(); | |
296 | statusCode = re.getStatusCode(); | |
297 | retVal = re.getBody(); | |
298 | } catch (HttpClientErrorException e) { | |
299 | retVal = "{\"error\": \"400: Bad Request\"}"; | |
300 | } | |
301 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
302 |
1
1. getFinalsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getFinalsJSON → KILLED |
return retVal; |
303 | } | |
304 | } | |
Mutations | ||
60 |
1.1 |
|
61 |
1.1 |
|
62 |
1.1 |
|
63 |
1.1 |
|
73 |
1.1 |
|
95 |
1.1 |
|
103 |
1.1 |
|
112 |
1.1 |
|
118 |
1.1 |
|
119 |
1.1 |
|
120 |
1.1 |
|
121 |
1.1 |
|
140 |
1.1 |
|
150 |
1.1 |
|
151 |
1.1 |
|
152 |
1.1 |
|
153 |
1.1 |
|
185 |
1.1 |
|
190 |
1.1 |
|
201 |
1.1 |
|
202 |
1.1 |
|
203 |
1.1 |
|
204 |
1.1 |
|
236 |
1.1 |
|
241 |
1.1 |
|
247 |
1.1 |
|
248 |
1.1 |
|
249 |
1.1 |
|
250 |
1.1 |
|
271 |
1.1 |
|
277 |
1.1 |
|
278 |
1.1 |
|
279 |
1.1 |
|
280 |
1.1 |
|
302 |
1.1 |