Skip to content

Commit b9f9de2

Browse files
SK-2181: Fix inconsistencies (#187)
* SK-2181: fix inconsistencies (#186)
1 parent 02e622d commit b9f9de2

File tree

15 files changed

+231
-39
lines changed

15 files changed

+231
-39
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.skyflow</groupId>
88
<artifactId>skyflow-java</artifactId>
9-
<version>2.0.0-beta.2</version>
9+
<version>2.0.0-beta.2-dev.cc7223a</version>
1010
<packaging>jar</packaging>
1111

1212
<name>${project.groupId}:${project.artifactId}</name>

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,20 @@ private void setRequestId(Map<String, List<String>> responseHeaders) {
7575
}
7676

7777
private void setMessage() {
78-
JsonElement messageElement = ((JsonObject) responseBody.get("error")).get("message");
78+
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
79+
JsonElement messageElement = errorObj.has("message") ? errorObj.get("message") : null;
7980
this.message = messageElement == null ? null : messageElement.getAsString();
8081
}
8182

8283
private void setGrpcCode() {
83-
JsonElement grpcElement = ((JsonObject) responseBody.get("error")).get("grpc_code");
84+
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
85+
JsonElement grpcElement = errorObj.has("grpc_code") ? errorObj.get("grpc_code") : errorObj.has("grpcCode") ? errorObj.get("grpcCode") : null;
8486
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
8587
}
8688

8789
private void setHttpStatus() {
88-
JsonElement statusElement = ((JsonObject) responseBody.get("error")).get("http_status");
90+
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
91+
JsonElement statusElement = errorObj.has("http_status") ? errorObj.get("http_status") : errorObj.has("httpStatus") ? errorObj.get("httpStatus") : null;
8992
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
9093
}
9194

@@ -98,11 +101,38 @@ public JsonArray getDetails() {
98101
}
99102

100103
private void setDetails(Map<String, List<String>> responseHeaders) {
101-
JsonElement detailsElement = ((JsonObject) responseBody.get("error")).get("details");
104+
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
105+
JsonElement detailsElement = errorObj.has("details") ? errorObj.get("details") : null;
102106
List<String> errorFromClientHeader = responseHeaders.get(Constants.ERROR_FROM_CLIENT_HEADER_KEY);
107+
103108
if (detailsElement != null) {
104-
this.details = detailsElement.getAsJsonArray();
109+
if (detailsElement.isJsonArray()) {
110+
this.details = detailsElement.getAsJsonArray();
111+
if (this.details.isEmpty()) this.details = new JsonArray();
112+
} else if (detailsElement.isJsonObject()) {
113+
JsonObject obj = detailsElement.getAsJsonObject();
114+
boolean allEmpty = true;
115+
for (String key : obj.keySet()) {
116+
if (obj.get(key).isJsonArray() && !obj.get(key).getAsJsonArray().isEmpty()) {
117+
allEmpty = false;
118+
break;
119+
} else if (!obj.get(key).isJsonArray() && !obj.get(key).isJsonNull()) {
120+
allEmpty = false;
121+
break;
122+
}
123+
}
124+
if (obj.isEmpty() || allEmpty) {
125+
this.details = new JsonArray();
126+
} else {
127+
JsonArray arr = new JsonArray();
128+
arr.add(obj);
129+
this.details = arr;
130+
}
131+
}
132+
} else {
133+
this.details = new JsonArray();
105134
}
135+
106136
if (errorFromClientHeader != null) {
107137
this.details = this.details == null ? new JsonArray() : this.details;
108138
String errorFromClient = errorFromClientHeader.get(0);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public String getSignedToken() {
2323

2424
@Override
2525
public String toString() {
26-
Gson gson = new Gson();
26+
Gson gson = new Gson().newBuilder().serializeNulls().create();
2727
return gson.toJson(this);
2828
}
2929
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public final class Constants {
2626
public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
2727
public static final String REQUEST_ID_HEADER_KEY = "x-request-id";
2828
public static final String PROCESSED_FILE_NAME_PREFIX = "processed-";
29-
public static final String ERROR_FROM_CLIENT_HEADER_KEY = "eror-from-client";
30-
public static final String DEIDENTIFIED_FILE_PREFIX = "deidentified";;
29+
public static final String ERROR_FROM_CLIENT_HEADER_KEY = "error-from-client";
30+
public static final String DEIDENTIFIED_FILE_PREFIX = "deidentified";
3131
}

src/main/java/com/skyflow/vault/connection/InvokeConnectionResponse.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
55

6+
import java.util.ArrayList;
67
import java.util.HashMap;
78

89
public class InvokeConnectionResponse {
910
private final Object data;
1011
private final HashMap<String, String> metadata;
12+
private final ArrayList<HashMap<String, Object>> errors;
1113

12-
public InvokeConnectionResponse(Object data, HashMap<String, String> metadata) {
14+
public InvokeConnectionResponse(Object data, HashMap<String, String> metadata, ArrayList<HashMap<String, Object>> errors) {
1315
this.data = data;
1416
this.metadata = metadata;
17+
this.errors = errors;
1518
}
1619

1720
public Object getData() {
@@ -22,9 +25,13 @@ public HashMap<String, String> getMetadata() {
2225
return metadata;
2326
}
2427

28+
public ArrayList<HashMap<String, Object>> getErrors() {
29+
return errors;
30+
}
31+
2532
@Override
2633
public String toString() {
27-
Gson gson = new GsonBuilder().serializeNulls().create();
34+
Gson gson = new Gson().newBuilder().serializeNulls().create();
2835
return gson.toJson(this);
2936
}
3037
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public InvokeConnectionResponse invoke(InvokeConnectionRequest invokeConnectionR
6767
JsonObject data = JsonParser.parseString(response).getAsJsonObject();
6868
HashMap<String, String> metadata = new HashMap<>();
6969
metadata.put("requestId", HttpUtility.getRequestID());
70-
connectionResponse = new InvokeConnectionResponse(data, metadata);
70+
connectionResponse = new InvokeConnectionResponse(data, metadata, null);
7171
LogUtil.printInfoLog(InfoLogs.INVOKE_CONNECTION_REQUEST_RESOLVED.getLog());
7272
} catch (IOException e) {
7373
LogUtil.printErrorLog(ErrorLogs.INVOKE_CONNECTION_REQUEST_REJECTED.getLog());

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
import java.util.*;
2929

3030
public final class DetectController extends VaultClient {
31-
private static final Gson gson = new GsonBuilder().serializeNulls().create();
31+
Gson gson = new GsonBuilder()
32+
.registerTypeAdapter(Optional.class, (JsonSerializer<Optional<?>>) (src, typeOfSrc, context) ->
33+
src.map(context::serialize).orElse(null))
34+
.serializeNulls()
35+
.create();
3236

3337
public DetectController(VaultConfig vaultConfig, Credentials credentials) {
3438
super(vaultConfig, credentials);
@@ -55,7 +59,7 @@ public DeidentifyTextResponse deidentifyText(DeidentifyTextRequest deidentifyTex
5559
deidentifyTextResponse = getDeIdentifyTextResponse(deidentifyStringResponse);
5660
LogUtil.printInfoLog(InfoLogs.DEIDENTIFY_TEXT_REQUEST_RESOLVED.getLog());
5761
} catch (ApiClientApiException ex) {
58-
String bodyString = gson.toJson(ex.body());
62+
String bodyString = extractBodyAsString(ex.body());
5963
LogUtil.printErrorLog(ErrorLogs.DEIDENTIFY_TEXT_REQUEST_REJECTED.getLog());
6064
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), bodyString);
6165
}
@@ -82,7 +86,7 @@ public ReidentifyTextResponse reidentifyText(ReidentifyTextRequest reidentifyTex
8286
reidentifyTextResponse = new ReidentifyTextResponse(reidentifyStringResponse.getText().orElse(null));
8387
LogUtil.printInfoLog(InfoLogs.REIDENTIFY_TEXT_REQUEST_RESOLVED.getLog());
8488
} catch (ApiClientApiException ex) {
85-
String bodyString = gson.toJson(ex.body());
89+
String bodyString = extractBodyAsString(ex.body());
8690
LogUtil.printErrorLog(ErrorLogs.REIDENTIFY_TEXT_REQUEST_REJECTED.getLog());
8791
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), bodyString);
8892
}
@@ -145,7 +149,7 @@ public DeidentifyFileResponse deidentifyFile(DeidentifyFileRequest request) thro
145149
}
146150
}
147151
} catch (ApiClientApiException e) {
148-
String bodyString = gson.toJson(e.body());
152+
String bodyString = extractBodyAsString(e.body());
149153
LogUtil.printErrorLog(ErrorLogs.DEIDENTIFY_FILE_REQUEST_REJECTED.getLog());
150154
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
151155
}
@@ -287,6 +291,14 @@ private static synchronized List<FileEntityInfo> getEntities(DeidentifyStatusRes
287291
return entities;
288292
}
289293

294+
private String extractBodyAsString(Object body) {
295+
if (body instanceof String) {
296+
return (String) body;
297+
} else {
298+
return gson.toJson(body);
299+
}
300+
}
301+
290302
private com.skyflow.generated.rest.types.DeidentifyFileResponse processFileByType(String fileExtension, String base64Content, DeidentifyFileRequest request, String vaultId) throws SkyflowException {
291303
switch (fileExtension.toLowerCase()) {
292304
case "txt":
@@ -367,7 +379,7 @@ public DeidentifyFileResponse getDetectRun(GetDetectRunRequest request) throws S
367379

368380
return parseDeidentifyFileResponse(apiResponse, runId, apiResponse.getStatus().toString());
369381
} catch (ApiClientApiException e) {
370-
String bodyString = gson.toJson(e.body());
382+
String bodyString = extractBodyAsString(e.body());
371383
LogUtil.printErrorLog(ErrorLogs.GET_DETECT_RUN_REQUEST_REJECTED.getLog());
372384
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
373385
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public VaultController(VaultConfig vaultConfig, Credentials credentials) {
3131
super(vaultConfig, credentials);
3232
}
3333

34-
private static synchronized HashMap<String, Object> getFormattedBatchInsertRecord(Object record, int requestIndex) {
34+
private static synchronized HashMap<String, Object> getFormattedBatchInsertRecord(Object record, Integer requestIndex) {
3535
HashMap<String, Object> insertRecord = new HashMap<>();
3636
String jsonString = gson.toJson(record);
3737
JsonObject bodyObject = JsonParser.parseString(jsonString).getAsJsonObject().get("Body").getAsJsonObject();
@@ -95,11 +95,9 @@ private static synchronized HashMap<String, Object> getFormattedUpdateRecord(V1U
9595

9696
private static synchronized HashMap<String, Object> getFormattedQueryRecord(V1FieldRecords record) {
9797
HashMap<String, Object> queryRecord = new HashMap<>();
98-
Object fields = record.getFields();
99-
if (fields != null) {
100-
String fieldsString = gson.toJson(fields);
101-
JsonObject fieldsObject = JsonParser.parseString(fieldsString).getAsJsonObject();
102-
queryRecord.putAll(fieldsObject.asMap());
98+
Optional<Map<String, Object>> fieldsOpt = record.getFields();
99+
if (fieldsOpt.isPresent()) {
100+
queryRecord.putAll(fieldsOpt.get());
103101
}
104102
return queryRecord;
105103
}
@@ -124,14 +122,15 @@ public InsertResponse insert(InsertRequest insertRequest) throws SkyflowExceptio
124122
if (records.isPresent()) {
125123
List<Map<String, Object>> recordList = records.get();
126124

127-
for (int index = 0; index < recordList.size(); index++) {
125+
for (Integer index = (Integer) 0; index < recordList.size(); index++) {
128126
Map<String, Object> record = recordList.get(index);
129127
HashMap<String, Object> insertRecord = getFormattedBatchInsertRecord(record, index);
130128

131129
if (insertRecord.containsKey("skyflowId")) {
132130
insertedFields.add(insertRecord);
133131
} else {
134132
insertRecord.put("requestId", batchInsertResult.headers().get("x-request-id").get(0));
133+
insertRecord.put("httpCode", 400);
135134
errorFields.add(insertRecord);
136135
}
137136
}
@@ -232,6 +231,7 @@ public GetResponse get(GetRequest getRequest) throws SkyflowException {
232231
.downloadUrl(getRequest.getDownloadURL())
233232
.columnName(getRequest.getColumnName())
234233
.columnValues(getRequest.getColumnValues())
234+
.fields(getRequest.getFields())
235235
.orderBy(RecordServiceBulkGetRecordRequestOrderBy.valueOf(getRequest.getOrderBy()))
236236
.build();
237237

src/main/java/com/skyflow/vault/data/GetRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public String getOrderBy() {
6363
public static final class GetRequestBuilder {
6464
private String table;
6565
private ArrayList<String> ids;
66-
private RedactionType redactionType;
66+
private RedactionType redactionType = RedactionType.PLAIN_TEXT;
6767
private Boolean returnTokens;
6868
private ArrayList<String> fields;
6969
private String offset;

src/main/java/com/skyflow/vault/data/GetResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ArrayList<HashMap<String, Object>> getErrors() {
2424

2525
@Override
2626
public String toString() {
27-
Gson gson = new Gson();
27+
Gson gson = new Gson().newBuilder().serializeNulls().create();
2828
return gson.toJson(this);
2929
}
3030
}

0 commit comments

Comments
 (0)