|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package unicon.matthews.dataloader; |
| 5 | + |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 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.web.client.RestClientException; |
| 20 | +import org.springframework.web.client.RestTemplate; |
| 21 | + |
| 22 | +import unicon.matthews.caliper.Envelope; |
| 23 | +import unicon.matthews.caliper.Event; |
| 24 | +import unicon.matthews.entity.ClassMapping; |
| 25 | +import unicon.matthews.entity.DataSync; |
| 26 | +import unicon.matthews.entity.UserMapping; |
| 27 | +import unicon.matthews.oneroster.Enrollment; |
| 28 | +import unicon.matthews.oneroster.LineItem; |
| 29 | +import unicon.matthews.oneroster.User; |
| 30 | + |
| 31 | +import com.google.gson.JsonObject; |
| 32 | +import com.google.gson.JsonPrimitive; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author ggilbert |
| 36 | + * |
| 37 | + */ |
| 38 | +public class MatthewsClient { |
| 39 | + |
| 40 | + private RestTemplate restTemplate; |
| 41 | + private HttpHeaders httpHeaders; |
| 42 | + |
| 43 | + private String key; |
| 44 | + private String secret; |
| 45 | + private String baseUrl; |
| 46 | + |
| 47 | + private static final String LOGIN_URL = "/api/auth/login"; |
| 48 | + |
| 49 | + private MatthewsClient() {} |
| 50 | + |
| 51 | + public static class Builder { |
| 52 | + MatthewsClient _matthewsClient = new MatthewsClient(); |
| 53 | + |
| 54 | + public Builder withRestTemplate(RestTemplate restTemplate) { |
| 55 | + _matthewsClient.restTemplate = restTemplate; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public Builder withHttpHeaders(HttpHeaders httpHeaders) { |
| 60 | + _matthewsClient.httpHeaders = httpHeaders; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + public Builder withBaseUrl(String baseUrl) { |
| 65 | + if (StringUtils.endsWith(baseUrl, "/")) { |
| 66 | + baseUrl = StringUtils.substringBeforeLast(baseUrl, "/"); |
| 67 | + } |
| 68 | + |
| 69 | + _matthewsClient.baseUrl = baseUrl; |
| 70 | + |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public Builder withKey(String key) { |
| 75 | + _matthewsClient.key = key; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public Builder withSecret(String secret) { |
| 80 | + _matthewsClient.secret = secret; |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + public MatthewsClient build() { |
| 85 | + if (StringUtils.isBlank(_matthewsClient.key) || |
| 86 | + StringUtils.isBlank(_matthewsClient.secret) || |
| 87 | + StringUtils.isBlank(_matthewsClient.baseUrl) || |
| 88 | + _matthewsClient.restTemplate == null || |
| 89 | + _matthewsClient.httpHeaders == null) { |
| 90 | + throw new IllegalStateException(); |
| 91 | + } |
| 92 | + |
| 93 | + _matthewsClient.httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 94 | + _matthewsClient.httpHeaders.add("X-Requested-With", "XMLHttpRequest"); |
| 95 | + |
| 96 | + _matthewsClient.httpHeaders.add("Authorization", "Bearer "+_matthewsClient.getToken()); |
| 97 | + |
| 98 | + return _matthewsClient; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @SuppressWarnings("rawtypes") |
| 103 | + public String getToken() { |
| 104 | + |
| 105 | + JsonObject request = new JsonObject(); |
| 106 | + request.add("username", new JsonPrimitive(this.key)); |
| 107 | + request.add("password", new JsonPrimitive(this.secret)); |
| 108 | + |
| 109 | + HttpEntity<String> entity = new HttpEntity<String>(request.toString(), httpHeaders); |
| 110 | + |
| 111 | + ResponseEntity<Map> loginResponse |
| 112 | + = restTemplate |
| 113 | + .exchange(this.baseUrl + LOGIN_URL, |
| 114 | + HttpMethod.POST, |
| 115 | + entity, |
| 116 | + Map.class); |
| 117 | + |
| 118 | + return (String)loginResponse.getBody().get("token"); |
| 119 | + } |
| 120 | + |
| 121 | + public void postEnrollment(Enrollment enrollment) { |
| 122 | + HttpEntity<Enrollment> he = new HttpEntity<Enrollment>(enrollment, this.httpHeaders); |
| 123 | + |
| 124 | + String path = "/api/classes/{classSourcedId}/enrollments"; |
| 125 | + String url = this.baseUrl + StringUtils.replace(path, "{classSourcedId}", enrollment.getKlass().getSourcedId()); |
| 126 | + |
| 127 | + restTemplate |
| 128 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 129 | + } |
| 130 | + |
| 131 | + public void postUser(User user) { |
| 132 | + HttpEntity<User> he = new HttpEntity<User>(user, this.httpHeaders); |
| 133 | + |
| 134 | + String path = "/api/users"; |
| 135 | + String url = this.baseUrl + path; |
| 136 | + |
| 137 | + restTemplate |
| 138 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 139 | + } |
| 140 | + |
| 141 | + public void postUserMapping(UserMapping userMapping) { |
| 142 | + HttpEntity<UserMapping> he = new HttpEntity<UserMapping>(userMapping, this.httpHeaders); |
| 143 | + |
| 144 | + String path = "/api/users/mapping"; |
| 145 | + String url = this.baseUrl + path; |
| 146 | + |
| 147 | + restTemplate |
| 148 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + public void postLineItem(LineItem lineItem) { |
| 153 | + HttpEntity<LineItem> he = new HttpEntity<LineItem>(lineItem, this.httpHeaders); |
| 154 | + |
| 155 | + String path = "/api/classes/{classSourcedId}/lineitems"; |
| 156 | + String url = this.baseUrl + StringUtils.replace(path, "{classSourcedId}", lineItem.getKlass().getSourcedId()); |
| 157 | + |
| 158 | + restTemplate |
| 159 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + public void postClass(unicon.matthews.oneroster.Class klass) { |
| 164 | + HttpEntity<unicon.matthews.oneroster.Class> he = new HttpEntity<unicon.matthews.oneroster.Class>(klass, this.httpHeaders); |
| 165 | + |
| 166 | + String path = "/api/classes"; |
| 167 | + String url = this.baseUrl + path; |
| 168 | + |
| 169 | + restTemplate |
| 170 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 171 | + } |
| 172 | + |
| 173 | + public void postClassMapping(ClassMapping classMapping) { |
| 174 | + HttpEntity<ClassMapping> he = new HttpEntity<ClassMapping>(classMapping, this.httpHeaders); |
| 175 | + |
| 176 | + String path = "/api/classes/mapping"; |
| 177 | + String url = this.baseUrl + path; |
| 178 | + |
| 179 | + restTemplate |
| 180 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + public void postEvent(Event event, String sensorName) { |
| 185 | + Envelope envelope |
| 186 | + = new Envelope.Builder() |
| 187 | + .withData(Collections.singletonList(event)) |
| 188 | + .withSendTime(LocalDateTime.now()) |
| 189 | + .withSensor(sensorName) |
| 190 | + .build(); |
| 191 | + |
| 192 | + HttpEntity<Envelope> h = new HttpEntity<Envelope>(envelope, this.httpHeaders); |
| 193 | + |
| 194 | + String path = "/api/caliper"; |
| 195 | + String url = this.baseUrl + path; |
| 196 | + |
| 197 | + restTemplate |
| 198 | + .exchange(url, HttpMethod.POST, h, String.class); |
| 199 | + } |
| 200 | + |
| 201 | + public void postEvents(Collection<Event> events, String sensorName) { |
| 202 | + Envelope envelope |
| 203 | + = new Envelope.Builder() |
| 204 | + .withData(new ArrayList<>(events)) |
| 205 | + .withSendTime(LocalDateTime.now()) |
| 206 | + .withSensor(sensorName) |
| 207 | + .build(); |
| 208 | + |
| 209 | + HttpEntity<Envelope> h = new HttpEntity<Envelope>(envelope, this.httpHeaders); |
| 210 | + |
| 211 | + String path = "/api/caliper"; |
| 212 | + String url = this.baseUrl + path; |
| 213 | + |
| 214 | + restTemplate |
| 215 | + .exchange(url, HttpMethod.POST, h, String.class); |
| 216 | + } |
| 217 | + |
| 218 | + public void postDataSync(DataSync dataSync) { |
| 219 | + HttpEntity<DataSync> he = new HttpEntity<DataSync>(dataSync, this.httpHeaders); |
| 220 | + |
| 221 | + String path = "/api/sync"; |
| 222 | + String url = this.baseUrl + path; |
| 223 | + |
| 224 | + restTemplate |
| 225 | + .exchange(url, HttpMethod.POST, he, JsonObject.class); |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + public DataSync getLatestDataSyncForType(unicon.matthews.entity.DataSync.DataSyncType dataSyncType) { |
| 230 | + DataSync dataSync = null; |
| 231 | + |
| 232 | + String path = "/api/sync/".concat(dataSyncType.toString()).concat("/latest"); |
| 233 | + String url = this.baseUrl + path; |
| 234 | + |
| 235 | + ResponseEntity<DataSync> response = null; |
| 236 | + try { |
| 237 | + response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(this.httpHeaders), DataSync.class); |
| 238 | + } |
| 239 | + catch (RestClientException e) { |
| 240 | + // TODO Auto-generated catch block |
| 241 | + e.printStackTrace(); |
| 242 | + } |
| 243 | + |
| 244 | + if (response != null && response.getStatusCode() == HttpStatus.OK) { |
| 245 | + dataSync = response.getBody(); |
| 246 | + } |
| 247 | + |
| 248 | + return dataSync; |
| 249 | + } |
| 250 | +} |
0 commit comments