-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVaultController.java
More file actions
489 lines (451 loc) · 25.7 KB
/
VaultController.java
File metadata and controls
489 lines (451 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package com.skyflow.vault.controller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.skyflow.VaultClient;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.RedactionType;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;
import com.skyflow.generated.rest.core.ApiClientApiException;
import com.skyflow.generated.rest.core.ApiClientException;
import com.skyflow.generated.rest.core.ApiClientHttpResponse;
import com.skyflow.generated.rest.core.RequestOptions;
import com.skyflow.generated.rest.resources.query.requests.QueryServiceExecuteQueryBody;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceBatchOperationBody;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceBulkDeleteRecordBody;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceBulkGetRecordRequest;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceInsertRecordBody;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceUpdateRecordBody;
import com.skyflow.generated.rest.resources.records.requests.UploadFileV2Request;
import com.skyflow.generated.rest.resources.records.types.RecordServiceBulkGetRecordRequestOrderBy;
import com.skyflow.generated.rest.resources.records.types.RecordServiceBulkGetRecordRequestRedaction;
import com.skyflow.generated.rest.resources.tokens.requests.V1DetokenizePayload;
import com.skyflow.generated.rest.resources.tokens.requests.V1TokenizePayload;
import com.skyflow.generated.rest.types.UploadFileV2Response;
import com.skyflow.generated.rest.types.V1BatchOperationResponse;
import com.skyflow.generated.rest.types.V1BulkDeleteRecordResponse;
import com.skyflow.generated.rest.types.V1BulkGetRecordResponse;
import com.skyflow.generated.rest.types.V1DetokenizeRecordResponse;
import com.skyflow.generated.rest.types.V1DetokenizeResponse;
import com.skyflow.generated.rest.types.V1FieldRecords;
import com.skyflow.generated.rest.types.V1GetQueryResponse;
import com.skyflow.generated.rest.types.V1InsertRecordResponse;
import com.skyflow.generated.rest.types.V1RecordMetaProperties;
import com.skyflow.generated.rest.types.V1TokenizeRecordResponse;
import com.skyflow.generated.rest.types.V1TokenizeResponse;
import com.skyflow.generated.rest.types.V1UpdateRecordResponse;
import com.skyflow.logs.ErrorLogs;
import com.skyflow.logs.InfoLogs;
import com.skyflow.utils.Constants;
import com.skyflow.utils.Utils;
import com.skyflow.utils.logger.LogUtil;
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.data.DeleteRequest;
import com.skyflow.vault.data.DeleteResponse;
import com.skyflow.vault.data.FileUploadRequest;
import com.skyflow.vault.data.FileUploadResponse;
import com.skyflow.vault.data.GetRequest;
import com.skyflow.vault.data.GetResponse;
import com.skyflow.vault.data.InsertRequest;
import com.skyflow.vault.data.InsertResponse;
import com.skyflow.vault.data.QueryRequest;
import com.skyflow.vault.data.QueryResponse;
import com.skyflow.vault.data.UpdateRequest;
import com.skyflow.vault.data.UpdateResponse;
import com.skyflow.vault.tokens.DetokenizeRecordResponse;
import com.skyflow.vault.tokens.DetokenizeRequest;
import com.skyflow.vault.tokens.DetokenizeResponse;
import com.skyflow.vault.tokens.TokenizeRequest;
import com.skyflow.vault.tokens.TokenizeResponse;
public final class VaultController extends VaultClient {
private static final Gson GSON = new GsonBuilder().serializeNulls().create();
private static final JsonObject SKY_METADATA = Utils.getMetrics();
public VaultController(VaultConfig vaultConfig, Credentials credentials) {
super(vaultConfig, credentials);
}
private static synchronized HashMap<String, Object> getFormattedBatchInsertRecord(Object record, Integer requestIndex) {
HashMap<String, Object> insertRecord = new HashMap<>();
String jsonString = GSON.toJson(record);
JsonObject bodyObject = JsonParser.parseString(jsonString).getAsJsonObject().get("Body").getAsJsonObject();
JsonArray records = bodyObject.getAsJsonArray("records");
JsonPrimitive error = bodyObject.getAsJsonPrimitive("error");
if (records != null) {
for (JsonElement recordElement : records) {
JsonObject recordObject = recordElement.getAsJsonObject();
insertRecord.put("skyflowId", recordObject.get("skyflow_id").getAsString());
JsonElement tokensElement = recordObject.get("tokens");
if (tokensElement != null) {
insertRecord.putAll(tokensElement.getAsJsonObject().asMap());
}
}
}
if (error != null) {
insertRecord.put("error", error.getAsString());
}
insertRecord.put("requestIndex", requestIndex);
return insertRecord;
}
private static synchronized HashMap<String, Object> getFormattedBulkInsertRecord(V1RecordMetaProperties record) {
HashMap<String, Object> insertRecord = new HashMap<>();
if (record.getSkyflowId().isPresent()) {
insertRecord.put("skyflowId", record.getSkyflowId().get());
}
if (record.getTokens().isPresent()) {
Map<String, Object> tokensMap = record.getTokens().get();
insertRecord.putAll(tokensMap);
}
return insertRecord;
}
private static synchronized HashMap<String, Object> getFormattedGetRecord(V1FieldRecords record) {
HashMap<String, Object> getRecord = new HashMap<>();
Optional<Map<String, Object>> fieldsOpt = record.getFields();
Optional<Map<String, Object>> tokensOpt = record.getTokens();
if (fieldsOpt.isPresent()) {
getRecord.putAll(fieldsOpt.get());
} else if (tokensOpt.isPresent()) {
getRecord.putAll(tokensOpt.get());
}
return getRecord;
}
private static synchronized HashMap<String, Object> getFormattedUpdateRecord(V1UpdateRecordResponse record) {
HashMap<String, Object> updateTokens = new HashMap<>();
record.getSkyflowId().ifPresent(skyflowId -> updateTokens.put("skyflowId", skyflowId));
record.getTokens().ifPresent(tokensMap -> updateTokens.putAll(tokensMap));
return updateTokens;
}
private static synchronized HashMap<String, Object> getFormattedQueryRecord(V1FieldRecords record) {
HashMap<String, Object> queryRecord = new HashMap<>();
Optional<Map<String, Object>> fieldsOpt = record.getFields();
if (fieldsOpt.isPresent()) {
queryRecord.putAll(fieldsOpt.get());
}
return queryRecord;
}
public InsertResponse insert(InsertRequest insertRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.INSERT_TRIGGERED.getLog());
V1InsertRecordResponse bulkInsertResult = null;
ApiClientHttpResponse<V1BatchOperationResponse> batchInsertResult = null;
ArrayList<HashMap<String, Object>> insertedFields = new ArrayList<>();
ArrayList<HashMap<String, Object>> errorFields = new ArrayList<>();
Boolean continueOnError = insertRequest.getContinueOnError();
try {
LogUtil.printInfoLog(InfoLogs.VALIDATE_INSERT_REQUEST.getLog());
Validations.validateInsertRequest(insertRequest);
setBearerToken();
if (continueOnError) {
RecordServiceBatchOperationBody insertBody = super.getBatchInsertRequestBody(insertRequest);
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
batchInsertResult = super.getRecordsApi().withRawResponse().recordServiceBatchOperation(super.getVaultConfig().getVaultId(), insertBody, requestOptions);
LogUtil.printInfoLog(InfoLogs.INSERT_REQUEST_RESOLVED.getLog());
Optional<List<Map<String, Object>>> records = batchInsertResult.body().getResponses();
if (records.isPresent()) {
List<Map<String, Object>> recordList = records.get();
for (Integer index = (Integer) 0; index < recordList.size(); index++) {
Map<String, Object> record = recordList.get(index);
HashMap<String, Object> insertRecord = getFormattedBatchInsertRecord(record, index);
if (insertRecord.containsKey("skyflowId")) {
insertedFields.add(insertRecord);
} else {
insertRecord.put("requestId", batchInsertResult.headers().get(Constants.REQUEST_ID_HEADER_KEY).get(0));
insertRecord.put("httpCode", ErrorCode.INVALID_INPUT.getCode());
errorFields.add(insertRecord);
}
}
}
} else {
RecordServiceInsertRecordBody insertBody = super.getBulkInsertRequestBody(insertRequest);
bulkInsertResult = super.getRecordsApi().recordServiceInsertRecord(
super.getVaultConfig().getVaultId(), insertRequest.getTable(), insertBody);
LogUtil.printInfoLog(InfoLogs.INSERT_REQUEST_RESOLVED.getLog());
Optional<List<V1RecordMetaProperties>> records = bulkInsertResult.getRecords();
if (records.isPresent()) {
for (V1RecordMetaProperties record : records.get()) {
HashMap<String, Object> insertRecord = getFormattedBulkInsertRecord(record);
insertedFields.add(insertRecord);
}
}
}
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.INSERT_RECORDS_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.INSERT_RECORDS_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
LogUtil.printInfoLog(InfoLogs.INSERT_SUCCESS.getLog());
if (insertedFields.isEmpty()) {
return new InsertResponse(null, errorFields.isEmpty() ? null : errorFields);
}
if (errorFields.isEmpty()) {
return new InsertResponse(insertedFields.isEmpty() ? null : insertedFields, null);
}
return new InsertResponse(insertedFields, errorFields);
}
public DetokenizeResponse detokenize(DetokenizeRequest detokenizeRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.DETOKENIZE_TRIGGERED.getLog());
ApiClientHttpResponse<V1DetokenizeResponse> result = null;
ArrayList<DetokenizeRecordResponse> detokenizedFields = new ArrayList<>();
ArrayList<DetokenizeRecordResponse> errorRecords = new ArrayList<>();
try {
LogUtil.printInfoLog(InfoLogs.VALIDATE_DETOKENIZE_REQUEST.getLog());
Validations.validateDetokenizeRequest(detokenizeRequest);
setBearerToken();
V1DetokenizePayload payload = super.getDetokenizePayload(detokenizeRequest);
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
result = super.getTokensApi().withRawResponse().recordServiceDetokenize(super.getVaultConfig().getVaultId(), payload, requestOptions);
LogUtil.printInfoLog(InfoLogs.DETOKENIZE_REQUEST_RESOLVED.getLog());
Map<String, List<String>> responseHeaders = result.headers();
String requestId = responseHeaders.get(Constants.REQUEST_ID_HEADER_KEY).get(0);
Optional<List<V1DetokenizeRecordResponse>> records = result.body().getRecords();
if (records.isPresent()) {
List<V1DetokenizeRecordResponse> recordList = records.get();
for (V1DetokenizeRecordResponse record : recordList) {
if (record.getError().isPresent()) {
DetokenizeRecordResponse recordResponse = new DetokenizeRecordResponse(record, requestId);
errorRecords.add(recordResponse);
} else {
DetokenizeRecordResponse recordResponse = new DetokenizeRecordResponse(record);
detokenizedFields.add(recordResponse);
}
}
}
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.DETOKENIZE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.DETOKENIZE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
if (!errorRecords.isEmpty()) {
LogUtil.printInfoLog(InfoLogs.DETOKENIZE_PARTIAL_SUCCESS.getLog());
} else {
LogUtil.printInfoLog(InfoLogs.DETOKENIZE_SUCCESS.getLog());
}
if (detokenizedFields.isEmpty()) {
return new DetokenizeResponse(null, errorRecords.isEmpty() ? null : errorRecords);
}
if (errorRecords.isEmpty()) {
return new DetokenizeResponse(detokenizedFields, null);
}
return new DetokenizeResponse(detokenizedFields, errorRecords);
}
public GetResponse get(GetRequest getRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.GET_TRIGGERED.getLog());
V1BulkGetRecordResponse result = null;
ArrayList<HashMap<String, Object>> data = new ArrayList<>();
ArrayList<HashMap<String, Object>> errors = new ArrayList<>();
try {
LogUtil.printInfoLog(InfoLogs.VALIDATE_GET_REQUEST.getLog());
Validations.validateGetRequest(getRequest);
setBearerToken();
RedactionType redactionType = getRequest.getRedactionType();
RecordServiceBulkGetRecordRequest recordServiceBulkGetRecordRequest = RecordServiceBulkGetRecordRequest.builder()
.skyflowIds(getRequest.getIds())
.redaction(redactionType != null ? RecordServiceBulkGetRecordRequestRedaction.valueOf(redactionType.toString()) : null)
.tokenization(getRequest.getReturnTokens())
.offset(getRequest.getOffset())
.limit(getRequest.getLimit())
.downloadUrl(getRequest.getDownloadURL())
.columnName(getRequest.getColumnName())
.columnValues(getRequest.getColumnValues())
.fields(getRequest.getFields())
.orderBy(RecordServiceBulkGetRecordRequestOrderBy.valueOf(getRequest.getOrderBy()))
.build();
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
result = super.getRecordsApi().recordServiceBulkGetRecord(
super.getVaultConfig().getVaultId(),
getRequest.getTable(),
recordServiceBulkGetRecordRequest,
requestOptions
);
LogUtil.printInfoLog(InfoLogs.GET_REQUEST_RESOLVED.getLog());
List<V1FieldRecords> records = result.getRecords().get();
if (records != null) {
for (V1FieldRecords record : records) {
data.add(getFormattedGetRecord(record));
}
}
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.GET_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.GET_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
LogUtil.printInfoLog(InfoLogs.GET_SUCCESS.getLog());
return new GetResponse(data, null);
}
public UpdateResponse update(UpdateRequest updateRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.UPDATE_TRIGGERED.getLog());
V1UpdateRecordResponse result;
String skyflowId;
HashMap<String, Object> tokensMap;
try {
LogUtil.printInfoLog(InfoLogs.VALIDATE_UPDATE_REQUEST.getLog());
Validations.validateUpdateRequest(updateRequest);
setBearerToken();
RecordServiceUpdateRecordBody updateBody = super.getUpdateRequestBody(updateRequest);
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
result = super.getRecordsApi().recordServiceUpdateRecord(
super.getVaultConfig().getVaultId(),
updateRequest.getTable(),
updateRequest.getData().remove("skyflow_id").toString(),
updateBody,
requestOptions
);
LogUtil.printInfoLog(InfoLogs.UPDATE_REQUEST_RESOLVED.getLog());
skyflowId = String.valueOf(result.getSkyflowId());
tokensMap = getFormattedUpdateRecord(result);
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.UPDATE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.UPDATE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
LogUtil.printInfoLog(InfoLogs.UPDATE_SUCCESS.getLog());
return new UpdateResponse(skyflowId, tokensMap);
}
public DeleteResponse delete(DeleteRequest deleteRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.DELETE_TRIGGERED.getLog());
V1BulkDeleteRecordResponse result;
try {
LogUtil.printInfoLog(InfoLogs.VALIDATING_DELETE_REQUEST.getLog());
Validations.validateDeleteRequest(deleteRequest);
setBearerToken();
RecordServiceBulkDeleteRecordBody deleteBody = RecordServiceBulkDeleteRecordBody.builder().skyflowIds(deleteRequest.getIds())
.build();
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
result = super.getRecordsApi().recordServiceBulkDeleteRecord(
super.getVaultConfig().getVaultId(), deleteRequest.getTable(), deleteBody, requestOptions);
LogUtil.printInfoLog(InfoLogs.DELETE_REQUEST_RESOLVED.getLog());
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.DELETE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.DELETE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
LogUtil.printInfoLog(InfoLogs.DELETE_SUCCESS.getLog());
return new DeleteResponse(result.getRecordIdResponse().get());
}
public QueryResponse query(QueryRequest queryRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.QUERY_TRIGGERED.getLog());
V1GetQueryResponse result;
ArrayList<HashMap<String, Object>> fields = new ArrayList<>();
try {
LogUtil.printInfoLog(InfoLogs.VALIDATING_QUERY_REQUEST.getLog());
Validations.validateQueryRequest(queryRequest);
setBearerToken();
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
result = super.getQueryApi().queryServiceExecuteQuery(
super.getVaultConfig().getVaultId(),
QueryServiceExecuteQueryBody.builder().query(queryRequest.getQuery()).build(),
requestOptions
);
LogUtil.printInfoLog(InfoLogs.QUERY_REQUEST_RESOLVED.getLog());
if (result.getRecords().isPresent()) {
List<V1FieldRecords> records = result.getRecords().get(); // Extract the List from Optional
for (V1FieldRecords record : records) {
fields.add(getFormattedQueryRecord(record));
}
}
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.QUERY_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.QUERY_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
LogUtil.printInfoLog(InfoLogs.QUERY_SUCCESS.getLog());
return new QueryResponse(fields);
}
public TokenizeResponse tokenize(TokenizeRequest tokenizeRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.TOKENIZE_TRIGGERED.getLog());
V1TokenizeResponse result = null;
List<String> list = new ArrayList<>();
try {
LogUtil.printInfoLog(InfoLogs.VALIDATING_TOKENIZE_REQUEST.getLog());
Validations.validateTokenizeRequest(tokenizeRequest);
setBearerToken();
V1TokenizePayload payload = super.getTokenizePayload(tokenizeRequest);
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
result = super.getTokensApi().recordServiceTokenize(super.getVaultConfig().getVaultId(), payload, requestOptions);
LogUtil.printInfoLog(InfoLogs.TOKENIZE_REQUEST_RESOLVED.getLog());
if (result != null && result.getRecords().isPresent() && !result.getRecords().get().isEmpty()) {
for (V1TokenizeRecordResponse response : result.getRecords().get()) {
if (response.getToken().isPresent()) {
list.add(response.getToken().get());
}
}
}
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.TOKENIZE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.TOKENIZE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
}
LogUtil.printInfoLog(InfoLogs.TOKENIZE_SUCCESS.getLog());
return new TokenizeResponse(list);
}
public FileUploadResponse uploadFile(FileUploadRequest fileUploadRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.FILE_UPLOAD_TRIGGERED.getLog());
FileUploadResponse fileUploadResponse = null;
try {
LogUtil.printInfoLog(InfoLogs.VALIDATING_FILE_UPLOAD_REQUEST.getLog());
Validations.validateFileUploadRequest(fileUploadRequest);
setBearerToken();
File file = super.getFileForFileUpload(fileUploadRequest);
UploadFileV2Request uploadFileV2Request = UploadFileV2Request.builder()
.tableName(fileUploadRequest.getTable())
.columnName(fileUploadRequest.getColumnName())
.skyflowId(fileUploadRequest.getSkyflowId())
.returnFileMetadata(false)
.build();
RequestOptions requestOptions = RequestOptions.builder().addHeader(Constants.SDK_METRICS_HEADER_KEY, SKY_METADATA.toString()).build();
UploadFileV2Response uploadFileV2Response = super.getRecordsApi().uploadFileV2(
super.getVaultConfig().getVaultId(),
file,
uploadFileV2Request,
requestOptions
);
fileUploadResponse = new FileUploadResponse(
uploadFileV2Response.getSkyflowId().orElse(null),
null
);
} catch (ApiClientApiException e) {
String bodyString = GSON.toJson(e.body());
LogUtil.printErrorLog(ErrorLogs.UPLOAD_FILE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
} catch (ApiClientException e) {
LogUtil.printErrorLog(ErrorLogs.UPLOAD_FILE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e.getCause(), ErrorCode.INVALID_INPUT.getCode());
} catch (IOException e) {
LogUtil.printErrorLog(ErrorLogs.UPLOAD_FILE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.getMessage(), e);
}
LogUtil.printInfoLog(InfoLogs.FILE_UPLOAD_SUCCESS.getLog());
return fileUploadResponse;
}
}