|
| 1 | +package com.yetanalytics.xapi.client; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Base64; |
| 7 | +import java.util.List; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +import org.apache.http.Header; |
| 11 | +import org.apache.http.HttpHeaders; |
| 12 | +import org.apache.http.ParseException; |
| 13 | +import org.apache.http.client.ClientProtocolException; |
| 14 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 15 | +import org.apache.http.client.methods.HttpGet; |
| 16 | +import org.apache.http.client.methods.HttpPost; |
| 17 | +import org.apache.http.entity.StringEntity; |
| 18 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 19 | +import org.apache.http.impl.client.HttpClients; |
| 20 | +import org.apache.http.message.BasicHeader; |
| 21 | +import org.apache.http.util.EntityUtils; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 24 | +import com.google.common.collect.Lists; |
| 25 | +import com.yetanalytics.xapi.client.filters.StatementFilters; |
| 26 | +import com.yetanalytics.xapi.exception.StatementClientException; |
| 27 | +import com.yetanalytics.xapi.model.Statement; |
| 28 | +import com.yetanalytics.xapi.model.StatementResult; |
| 29 | +import com.yetanalytics.xapi.util.Mapper; |
| 30 | + |
| 31 | +/** |
| 32 | + * Minimal xAPI Client featuring GET and POST Operations for LRS interop. |
| 33 | + */ |
| 34 | +public class StatementClient { |
| 35 | + |
| 36 | + private static final String STATEMENT_ENDPOINT = "statements"; |
| 37 | + |
| 38 | + private LRS lrs; |
| 39 | + private CloseableHttpClient client; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructor to create an xAPI Client |
| 43 | + * |
| 44 | + * @param lrs The Learning Record store to connect to |
| 45 | + */ |
| 46 | + public StatementClient (LRS lrs) { |
| 47 | + this.lrs = lrs; |
| 48 | + |
| 49 | + String encodedCreds = Base64.getEncoder().encodeToString( |
| 50 | + String.format("%s:%s", lrs.getKey(), lrs.getSecret()).getBytes()); |
| 51 | + |
| 52 | + //TODO: Version headers |
| 53 | + List<Header> headers = new ArrayList<Header>(); |
| 54 | + headers.add(new BasicHeader("X-Experience-API-Version","1.0.3")); |
| 55 | + headers.add(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json")); |
| 56 | + headers.add(new BasicHeader(HttpHeaders.ACCEPT, "application/json")); |
| 57 | + headers.add(new BasicHeader("Authorization", |
| 58 | + String.format("Basic %s", encodedCreds))); |
| 59 | + |
| 60 | + this.client = HttpClients.custom() |
| 61 | + .setDefaultHeaders(headers) |
| 62 | + .build(); |
| 63 | + } |
| 64 | + |
| 65 | + private List<UUID> doPost(List<Statement> statements, URI endpoint) |
| 66 | + throws ParseException, IOException { |
| 67 | + HttpPost post = new HttpPost(endpoint); |
| 68 | + post.setEntity(new StringEntity( |
| 69 | + Mapper.getMapper().writeValueAsString(statements))); |
| 70 | + |
| 71 | + CloseableHttpResponse response = client.execute(post); |
| 72 | + |
| 73 | + if (response.getStatusLine().getStatusCode() == 200) { |
| 74 | + String responseBody = EntityUtils.toString(response.getEntity()); |
| 75 | + return Mapper.getMapper().readValue( |
| 76 | + responseBody, |
| 77 | + new TypeReference<List<UUID>>(){}); |
| 78 | + } else { |
| 79 | + throw new StatementClientException(String.format( |
| 80 | + "Error, Non-200 Status. Received: %s", |
| 81 | + response.getStatusLine().getStatusCode())); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Method to post a single xAPI Statement to an LRS. |
| 87 | + * |
| 88 | + * @param stmt Statement to post to LRS |
| 89 | + * @return List of IDs for created statement(s) from LRS |
| 90 | + */ |
| 91 | + public List<UUID> postStatement(Statement stmt) { |
| 92 | + return postStatements(new ArrayList<>(List.of(stmt))); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Method to post a List of xAPI Statements to an LRS. |
| 97 | + * |
| 98 | + * @param stmts Statements to post to LRS |
| 99 | + * @return List of IDs for created statement(s) from LRS |
| 100 | + */ |
| 101 | + public List<UUID> postStatements(List<Statement> stmts) { |
| 102 | + try { |
| 103 | + List<UUID> result = new ArrayList<UUID>(); |
| 104 | + for (List<Statement> p : Lists.partition(stmts, lrs.getBatchSize())) { |
| 105 | + result.addAll(doPost(p, lrs.getHost().resolve(STATEMENT_ENDPOINT))); |
| 106 | + } |
| 107 | + return result; |
| 108 | + } catch (ParseException | IOException e) { |
| 109 | + throw new StatementClientException("Error posting Statements", e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private StatementResult doGetStatementResult(URI endpoint) |
| 114 | + throws ClientProtocolException, IOException { |
| 115 | + HttpGet get = new HttpGet(endpoint); |
| 116 | + CloseableHttpResponse response = client.execute(get); |
| 117 | + |
| 118 | + if (response.getStatusLine().getStatusCode() == 200) { |
| 119 | + String responseBody = EntityUtils.toString(response.getEntity()); |
| 120 | + return Mapper.getMapper().readValue(responseBody, StatementResult.class); |
| 121 | + } else { |
| 122 | + throw new StatementClientException(String.format( |
| 123 | + "Error, Non-200 Status. Received: %s", |
| 124 | + response.getStatusLine().getStatusCode())); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private Statement doGetStatement(URI endpoint) |
| 129 | + throws ClientProtocolException, IOException { |
| 130 | + HttpGet get = new HttpGet(endpoint); |
| 131 | + CloseableHttpResponse response = client.execute(get); |
| 132 | + |
| 133 | + if (response.getStatusLine().getStatusCode() == 200) { |
| 134 | + String responseBody = EntityUtils.toString(response.getEntity()); |
| 135 | + return Mapper.getMapper().readValue(responseBody, Statement.class); |
| 136 | + } else { |
| 137 | + throw new StatementClientException(String.format( |
| 138 | + "Error, Non-200 Status. Received: %s", |
| 139 | + response.getStatusLine().getStatusCode())); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private URI resolveMore(URI moreLink) { |
| 144 | + if (moreLink == null || moreLink.toString().equals("")) |
| 145 | + return null; |
| 146 | + URI uri = lrs.getHost().resolve(STATEMENT_ENDPOINT); |
| 147 | + return uri.resolve(moreLink.toString()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Method to get Statements from LRS |
| 152 | + * |
| 153 | + * @param filters StatementFilters object to filter the query. |
| 154 | + * @return All statements that match filter |
| 155 | + */ |
| 156 | + public List<Statement> getStatements(StatementFilters filters) { |
| 157 | + List<Statement> statements = new ArrayList<Statement>(); |
| 158 | + |
| 159 | + URI target = lrs.getHost().resolve(STATEMENT_ENDPOINT); |
| 160 | + if (filters != null) { |
| 161 | + target = filters.addQueryToUri(target); |
| 162 | + } |
| 163 | + |
| 164 | + try { |
| 165 | + while(target != null) { |
| 166 | + if (filters != null && |
| 167 | + (filters.getStatementId() != null |
| 168 | + || filters.getVoidedStatementId() != null)) { |
| 169 | + statements.add(doGetStatement(target)); |
| 170 | + target = null; |
| 171 | + } else { |
| 172 | + StatementResult result = doGetStatementResult(target); |
| 173 | + statements.addAll(result.getStatements()); |
| 174 | + target = resolveMore(result.getMore()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + } catch (IOException e) { |
| 179 | + throw new StatementClientException("Error getting Statements", e); |
| 180 | + } |
| 181 | + return statements; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Method to get Statements from LRS with no filters |
| 186 | + * |
| 187 | + * @return All statements |
| 188 | + */ |
| 189 | + public List<Statement> getStatements() { |
| 190 | + return getStatements(null); |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments