Skip to content

Commit 7ae3147

Browse files
SK-2147: resolve comments and add tests
1 parent 62733bb commit 7ae3147

File tree

9 files changed

+315
-100
lines changed

9 files changed

+315
-100
lines changed

pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,6 @@
287287
<autoReleaseAfterClose>true</autoReleaseAfterClose>
288288
</configuration>
289289
</plugin>
290-
<plugin>
291-
<groupId>org.apache.maven.plugins</groupId>
292-
<artifactId>maven-javadoc-plugin</artifactId>
293-
<version>3.2.0</version>
294-
<configuration>
295-
<additionalJOption>-Xdoclint:none</additionalJOption>
296-
<failOnError>false</failOnError>
297-
</configuration>
298-
</plugin>
299290
</plugins>
300291
</build>
301292
</profile>

src/main/java/com/skyflow/VaultClient.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,9 @@ protected V1TokenizePayload getTokenizePayload(TokenizeRequest request) {
194194
for (ColumnValue columnValue : request.getColumnValues()) {
195195
V1TokenizeRecordRequest.Builder recordBuilder = V1TokenizeRecordRequest.builder();
196196
String value = columnValue.getValue();
197-
if (value != null) {
198-
recordBuilder.value(value);
199-
}
197+
recordBuilder.value(value);
200198
String columnGroup = columnValue.getColumnGroup();
201-
if (columnGroup != null) {
202-
recordBuilder.columnGroup(columnGroup);
203-
}
204-
199+
recordBuilder.columnGroup(columnGroup);
205200
tokenizationParameters.add(recordBuilder.build());
206201
}
207202

@@ -509,7 +504,6 @@ protected DeidentifyAudioRequest getDeidentifyAudioRequest(DeidentifyFileRequest
509504
.build();
510505
}
511506

512-
// Add to VaultClient.java class
513507
protected DeidentifyPdfRequest getDeidentifyPdfRequest(DeidentifyFileRequest request, String vaultId, String base64Content) {
514508
List<EntityType> mappedEntityTypes = getEntityTypes(request.getEntities());
515509

src/main/java/com/skyflow/errors/SkyflowException.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ private String parseJsonStringWithErrorObject(String responseBody) {
5656

5757
// If already valid JSON, parsing it directly
5858
try {
59-
JsonObject obj = JsonParser.parseString(responseBody).getAsJsonObject();
59+
JsonObject responseBodyObject = JsonParser.parseString(responseBody).getAsJsonObject();
6060
// If it's valid JSON and has error object, return as is
61-
if (obj.has("error")) {
61+
if (responseBodyObject.has("error")) {
6262
return responseBody;
6363
}
6464
return "";
@@ -138,59 +138,22 @@ private void setRequestId(Map<String, List<String>> responseHeaders) {
138138
}
139139
}
140140

141-
// For legacy error structure
142-
private void setMessage() {
143-
JsonElement messageElement = ((JsonObject) responseBody.get("error")).get("message");
144-
this.message = messageElement == null ? null : messageElement.getAsString();
145-
}
146141

147-
// For new error structure
148142
private void setMessage(JsonObject errorObj) {
149143
JsonElement messageElement = errorObj.get("message");
150144
this.message = messageElement == null ? null : messageElement.getAsString();
151145
}
152146

153-
// For legacy error structure
154-
private void setGrpcCode() {
155-
JsonElement grpcElement = ((JsonObject) responseBody.get("error")).get("grpc_code");
156-
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
157-
}
158-
159-
// For new error structure
160147
private void setGrpcCode(JsonObject errorObj) {
161148
JsonElement grpcElement = errorObj.get("grpc_code");
162149
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
163150
}
164151

165-
// For legacy error structure
166-
private void setHttpStatus() {
167-
JsonElement statusElement = ((JsonObject) responseBody.get("error")).get("http_status");
168-
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
169-
}
170-
171-
// For new error structure
172152
private void setHttpStatus(JsonObject errorObj) {
173153
JsonElement statusElement = errorObj.get("http_status");
174154
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
175155
}
176156

177-
// For legacy error structure
178-
private void setDetails(Map<String, List<String>> responseHeaders) {
179-
JsonElement detailsElement = ((JsonObject) responseBody.get("error")).get("details");
180-
List<String> errorFromClientHeader = responseHeaders.get("error-from-client");
181-
if (detailsElement != null) {
182-
this.details = detailsElement.getAsJsonArray();
183-
}
184-
if (errorFromClientHeader != null) {
185-
this.details = this.details == null ? new JsonArray() : this.details;
186-
String errorFromClient = errorFromClientHeader.get(0);
187-
JsonObject detailObject = new JsonObject();
188-
detailObject.addProperty("errorFromClient", errorFromClient);
189-
this.details.add(detailObject);
190-
}
191-
}
192-
193-
// For new error structure
194157
private void setDetails(JsonObject errorObj, Map<String, List<String>> responseHeaders) {
195158
JsonElement detailsElement = errorObj.get("details");
196159
List<String> errorFromClientHeader = responseHeaders.get("error-from-client");

src/main/java/com/skyflow/serviceaccount/util/BearerToken.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.skyflow.serviceaccount.util;
22

3-
import com.google.gson.JsonElement;
4-
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
6-
import com.google.gson.JsonSyntaxException;
3+
import com.google.gson.*;
74
import com.skyflow.errors.ErrorCode;
85
import com.skyflow.errors.ErrorMessage;
96
import com.skyflow.errors.SkyflowException;
@@ -31,6 +28,7 @@
3128
import java.util.Objects;
3229

3330
public class BearerToken {
31+
private static final Gson gson = new GsonBuilder().serializeNulls().create();
3432
private static final ApiClientBuilder apiClientBuilder = new ApiClientBuilder();
3533
private final File credentialsFile;
3634
private final String credentialsString;
@@ -140,9 +138,9 @@ private static V1GetAuthTokenResponse getBearerTokenFromCredentials(
140138
LogUtil.printErrorLog(ErrorLogs.INVALID_TOKEN_URI.getLog());
141139
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.InvalidTokenUri.getMessage());
142140
} catch (ApiClientApiException e) {
141+
String bodyString = gson.toJson(e.body());
143142
LogUtil.printErrorLog(ErrorLogs.BEARER_TOKEN_REJECTED.getLog());
144-
// throw new SkyflowException(e.getCode(), e, e.getResponseHeaders(), e.getResponseBody());
145-
throw e;
143+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
146144
}
147145
}
148146

src/main/java/com/skyflow/utils/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ public final class Constants {
2525
public static final String SDK_AUTH_HEADER_KEY = "x-skyflow-authorization";
2626
public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
2727
public static final String REQUEST_ID_HEADER_KEY = "x-request-id";
28+
public static final String DEIDENTIFY_FILE_IN_PROGRESS_STATUS = "IN_PROGRESS";
29+
public static final String DEIDENTIFY_FILE_SUCCESS_STATUS = "SUCCESS";
30+
public static final String PROCESSED_FILE_NAME_PREFIX = "processed-";
2831
}

src/main/java/com/skyflow/vault/controller/DetectController.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.skyflow.vault.controller;
22

3+
import com.google.gson.*;
34
import com.skyflow.VaultClient;
45
import com.skyflow.config.Credentials;
56
import com.skyflow.config.VaultConfig;
@@ -13,6 +14,7 @@
1314
import com.skyflow.generated.rest.types.*;
1415
import com.skyflow.logs.ErrorLogs;
1516
import com.skyflow.logs.InfoLogs;
17+
import com.skyflow.utils.Constants;
1618
import com.skyflow.utils.logger.LogUtil;
1719
import com.skyflow.utils.validations.Validations;
1820
import com.skyflow.vault.detect.*;
@@ -26,6 +28,7 @@
2628
import java.util.*;
2729

2830
public final class DetectController extends VaultClient {
31+
private static final Gson gson = new GsonBuilder().serializeNulls().create();
2932

3033
public DetectController(VaultConfig vaultConfig, Credentials credentials) {
3134
super(vaultConfig, credentials);
@@ -52,8 +55,9 @@ public DeidentifyTextResponse deidentifyText(DeidentifyTextRequest deidentifyTex
5255
deidentifyTextResponse = getDeIdentifyTextResponse(deidentifyStringResponse);
5356
LogUtil.printInfoLog(InfoLogs.DEIDENTIFY_TEXT_REQUEST_RESOLVED.getLog());
5457
} catch (ApiClientApiException ex) {
58+
String bodyString = gson.toJson(ex.body());
5559
LogUtil.printErrorLog(ErrorLogs.DEIDENTIFY_TEXT_REQUEST_REJECTED.getLog());
56-
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), ex.body().toString());
60+
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), bodyString);
5761
}
5862
LogUtil.printInfoLog(InfoLogs.DEIDENTIFY_TEXT_SUCCESS.getLog());
5963
return deidentifyTextResponse;
@@ -78,8 +82,9 @@ public ReidentifyTextResponse reidentifyText(ReidentifyTextRequest reidentifyTex
7882
reidentifyTextResponse = new ReidentifyTextResponse(reidentifyStringResponse.getText().orElse(null));
7983
LogUtil.printInfoLog(InfoLogs.REIDENTIFY_TEXT_REQUEST_RESOLVED.getLog());
8084
} catch (ApiClientApiException ex) {
85+
String bodyString = gson.toJson(ex.body());
8186
LogUtil.printErrorLog(ErrorLogs.REIDENTIFY_TEXT_REQUEST_REJECTED.getLog());
82-
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), ex.body().toString());
87+
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), bodyString);
8388
}
8489
LogUtil.printInfoLog(InfoLogs.REIDENTIFY_TEXT_SUCCESS.getLog());
8590
return reidentifyTextResponse;
@@ -119,12 +124,12 @@ public DeidentifyFileResponse deidentifyFile(DeidentifyFileRequest request) thro
119124
throw new SkyflowException(ErrorCode.SERVER_ERROR.getCode(), ErrorMessage.PollingForResultsFailed.getMessage());
120125
}
121126

122-
if ("SUCCESS".equalsIgnoreCase(response.getStatus())) {
127+
if (Constants.DEIDENTIFY_FILE_SUCCESS_STATUS.equalsIgnoreCase(response.getStatus())) {
123128
String base64File = response.getFileBase64();
124129
if (base64File != null) {
125130
byte[] decodedBytes = Base64.getDecoder().decode(base64File);
126131
String outputDir = request.getOutputDirectory();
127-
String outputFileName = "processed-" + fileName;
132+
String outputFileName = Constants.PROCESSED_FILE_NAME_PREFIX + fileName;
128133
File outputFile;
129134
if (outputDir != null && !outputDir.isEmpty()) {
130135
outputFile = new File(outputDir, outputFileName);
@@ -140,8 +145,9 @@ public DeidentifyFileResponse deidentifyFile(DeidentifyFileRequest request) thro
140145
}
141146
}
142147
} catch (ApiClientApiException e) {
148+
String bodyString = gson.toJson(e.body());
143149
LogUtil.printErrorLog(ErrorLogs.DEIDENTIFY_FILE_REQUEST_REJECTED.getLog());
144-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
150+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
145151
}
146152
return response;
147153
}
@@ -171,24 +177,9 @@ private DeidentifyFileResponse pollForResults(String runId, Integer maxWaitTime)
171177

172178
DeidentifyStatusResponseStatus status = response.getStatus();
173179

174-
if (Objects.equals(status.toString(), "IN_PROGRESS")) {
180+
if (Constants.DEIDENTIFY_FILE_IN_PROGRESS_STATUS.equalsIgnoreCase(String.valueOf(status))) {
175181
if (currentWaitTime >= maxWaitTime) {
176-
return new DeidentifyFileResponse(
177-
null, // file
178-
null, // file base64
179-
null, // type
180-
null, // extension
181-
null, // wordCount
182-
null, // charCount
183-
null, // sizeInKb
184-
null, // durationInSeconds
185-
null, // pageCount
186-
null, // slideCount
187-
null, // entities
188-
runId, // runId
189-
"IN_PROGRESS", // status
190-
null // errors
191-
);
182+
return new DeidentifyFileResponse(runId, Constants.DEIDENTIFY_FILE_IN_PROGRESS_STATUS);
192183
}
193184

194185
int nextWaitTime = currentWaitTime * 2;
@@ -209,8 +200,9 @@ private DeidentifyFileResponse pollForResults(String runId, Integer maxWaitTime)
209200
return parseDeidentifyFileResponse(response, runId, status.toString());
210201
}
211202
} catch (ApiClientApiException e) {
203+
String bodyString = gson.toJson(e.body());
212204
LogUtil.printErrorLog(ErrorLogs.GET_DETECT_RUN_REQUEST_REJECTED.getLog());
213-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
205+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
214206
}
215207
}
216208

@@ -376,8 +368,9 @@ public DeidentifyFileResponse getDetectRun(GetDetectRunRequest request) throws S
376368

377369
return parseDeidentifyFileResponse(apiResponse, runId, apiResponse.getStatus().toString());
378370
} catch (ApiClientApiException e) {
371+
String bodyString = gson.toJson(e.body());
379372
LogUtil.printErrorLog(ErrorLogs.GET_DETECT_RUN_REQUEST_REJECTED.getLog());
380-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
373+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
381374
}
382375
}
383376
}

src/main/java/com/skyflow/vault/controller/VaultController.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.skyflow.utils.validations.Validations;
2323
import com.skyflow.vault.data.*;
2424
import com.skyflow.vault.tokens.*;
25-
2625
import java.util.*;
2726

2827
public final class VaultController extends VaultClient {
@@ -70,7 +69,6 @@ private static synchronized HashMap<String, Object> getFormattedBulkInsertRecord
7069
return insertRecord;
7170
}
7271

73-
7472
private static synchronized HashMap<String, Object> getFormattedGetRecord(V1FieldRecords record) {
7573
HashMap<String, Object> getRecord = new HashMap<>();
7674

@@ -82,11 +80,9 @@ private static synchronized HashMap<String, Object> getFormattedGetRecord(V1Fiel
8280
} else if (tokensOpt.isPresent()) {
8381
getRecord.putAll(tokensOpt.get());
8482
}
85-
8683
return getRecord;
8784
}
8885

89-
9086
private static synchronized HashMap<String, Object> getFormattedUpdateRecord(V1UpdateRecordResponse record) {
9187
HashMap<String, Object> updateTokens = new HashMap<>();
9288

@@ -97,7 +93,6 @@ private static synchronized HashMap<String, Object> getFormattedUpdateRecord(V1U
9793
return updateTokens;
9894
}
9995

100-
10196
private static synchronized HashMap<String, Object> getFormattedQueryRecord(V1FieldRecords record) {
10297
HashMap<String, Object> queryRecord = new HashMap<>();
10398
Object fields = record.getFields();
@@ -155,8 +150,9 @@ public InsertResponse insert(InsertRequest insertRequest) throws SkyflowExceptio
155150
}
156151
}
157152
} catch (ApiClientApiException e) {
153+
String bodyString = gson.toJson(e.body());
158154
LogUtil.printErrorLog(ErrorLogs.INSERT_RECORDS_REJECTED.getLog());
159-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
155+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
160156
}
161157
LogUtil.printInfoLog(InfoLogs.INSERT_SUCCESS.getLog());
162158
if (insertedFields.isEmpty()) {
@@ -198,8 +194,9 @@ public DetokenizeResponse detokenize(DetokenizeRequest detokenizeRequest) throws
198194
}
199195
}
200196
} catch (ApiClientApiException e) {
197+
String bodyString = gson.toJson(e.body());
201198
LogUtil.printErrorLog(ErrorLogs.DETOKENIZE_REQUEST_REJECTED.getLog());
202-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
199+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
203200
}
204201

205202
if (!errorRecords.isEmpty()) {
@@ -252,8 +249,9 @@ public GetResponse get(GetRequest getRequest) throws SkyflowException {
252249
}
253250
}
254251
} catch (ApiClientApiException e) {
252+
String bodyString = gson.toJson(e.body());
255253
LogUtil.printErrorLog(ErrorLogs.GET_REQUEST_REJECTED.getLog());
256-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
254+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
257255
}
258256
LogUtil.printInfoLog(InfoLogs.GET_SUCCESS.getLog());
259257
return new GetResponse(data, null);
@@ -279,8 +277,9 @@ public UpdateResponse update(UpdateRequest updateRequest) throws SkyflowExceptio
279277
skyflowId = String.valueOf(result.getSkyflowId());
280278
tokensMap = getFormattedUpdateRecord(result);
281279
} catch (ApiClientApiException e) {
280+
String bodyString = gson.toJson(e.body());
282281
LogUtil.printErrorLog(ErrorLogs.UPDATE_REQUEST_REJECTED.getLog());
283-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
282+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
284283
}
285284
LogUtil.printInfoLog(InfoLogs.UPDATE_SUCCESS.getLog());
286285
return new UpdateResponse(skyflowId, tokensMap);
@@ -294,14 +293,15 @@ public DeleteResponse delete(DeleteRequest deleteRequest) throws SkyflowExceptio
294293
Validations.validateDeleteRequest(deleteRequest);
295294
setBearerToken();
296295
RecordServiceBulkDeleteRecordBody deleteBody = RecordServiceBulkDeleteRecordBody.builder().skyflowIds(deleteRequest.getIds())
297-
.build();
296+
.build();
298297

299298
result = super.getRecordsApi().recordServiceBulkDeleteRecord(
300299
super.getVaultConfig().getVaultId(), deleteRequest.getTable(), deleteBody);
301300
LogUtil.printInfoLog(InfoLogs.DELETE_REQUEST_RESOLVED.getLog());
302301
} catch (ApiClientApiException e) {
302+
String bodyString = gson.toJson(e.body());
303303
LogUtil.printErrorLog(ErrorLogs.DELETE_REQUEST_REJECTED.getLog());
304-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
304+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
305305
}
306306
LogUtil.printInfoLog(InfoLogs.DELETE_SUCCESS.getLog());
307307
return new DeleteResponse(result.getRecordIdResponse().get());
@@ -325,8 +325,9 @@ public QueryResponse query(QueryRequest queryRequest) throws SkyflowException {
325325
}
326326
}
327327
} catch (ApiClientApiException e) {
328+
String bodyString = gson.toJson(e.body());
328329
LogUtil.printErrorLog(ErrorLogs.QUERY_REQUEST_REJECTED.getLog());
329-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString());
330+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
330331
}
331332
LogUtil.printInfoLog(InfoLogs.QUERY_SUCCESS.getLog());
332333
return new QueryResponse(fields);
@@ -351,8 +352,10 @@ public TokenizeResponse tokenize(TokenizeRequest tokenizeRequest) throws Skyflow
351352
}
352353
}
353354
} catch (ApiClientApiException e) {
355+
String bodyString = gson.toJson(e.body());
354356
LogUtil.printErrorLog(ErrorLogs.TOKENIZE_REQUEST_REJECTED.getLog());
355-
throw new SkyflowException(e.statusCode(), e, e.headers(), e.body().toString()); }
357+
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
358+
}
356359
LogUtil.printInfoLog(InfoLogs.TOKENIZE_SUCCESS.getLog());
357360
return new TokenizeResponse(list);
358361
}

0 commit comments

Comments
 (0)