Skip to content

Commit

Permalink
Merge pull request #93 from skyflowapi/release/23.8.2
Browse files Browse the repository at this point in the history
SK-993/Release/23.8.2
  • Loading branch information
yaswanth-pula-skyflow authored Sep 1, 2023
2 parents 3a04710 + c0dee29 commit f52266e
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.skyflow</groupId>
<artifactId>skyflow-java</artifactId>
<version>1.10.0</version>
<version>1.11.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/common/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class Constants {

public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
public static final String SDK_VERSION = "1.10.0";
public static final String SDK_VERSION = "1.11.0";

}
32 changes: 32 additions & 0 deletions src/main/java/com/skyflow/common/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,38 @@ public static JSONObject constructUpdateRequest(UpdateRecordInput record, Update
return postRequestInput;

}
public static JSONObject constructQueryRequest(QueryRecordInput queryInput, QueryOptions options)
throws SkyflowException {
if (queryInput == null) {
throw new SkyflowException(ErrorCode.EmptyRecords);
}
JSONObject postRequestInput = new JSONObject();
if (queryInput.getQuery() == null||queryInput.getQuery().trim().isEmpty()) {
throw new SkyflowException(ErrorCode.InvalidQuery);
}
postRequestInput.put("query", queryInput.getQuery());
return postRequestInput;
}

public static JSONObject constructQueryErrorObject(SkyflowException skyflowException) {
JSONObject finalResponseError = new JSONObject();
try {
JSONObject errorObject = (JSONObject) ((JSONObject) new JSONParser().parse(skyflowException.getMessage())).get("error");
if (errorObject != null) {
JSONObject responseError = new JSONObject();
responseError.put("code", errorObject.get("http_code"));
responseError.put("description", errorObject.get("message"));
finalResponseError.put("error", responseError);
}
} catch (ParseException e) {
JSONObject responseError = new JSONObject();
responseError.put("code", skyflowException.getCode());
responseError.put("description", skyflowException.getMessage());
finalResponseError.put("error", responseError);

}
return finalResponseError;
}

public static StringBuilder constructGetByIdRequestURLParams(GetByIdRecordInput record) {
StringBuilder paramsList = new StringBuilder();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/skyflow/entities/QueryInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.skyflow.entities;

public class QueryInput {
private QueryRecordInput queryInput;

public QueryRecordInput getQueryInput() {
return queryInput;
}

public void setQueryInput(QueryRecordInput queryInput) {
this.queryInput = queryInput;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/skyflow/entities/QueryOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Copyright (c) 2022 Skyflow, Inc.
*/
package com.skyflow.entities;

public class QueryOptions {

}
11 changes: 11 additions & 0 deletions src/main/java/com/skyflow/entities/QueryRecordInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.skyflow.entities;

public class QueryRecordInput {
private String query;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/skyflow/errors/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum ErrorCode {
InvalidTable(400, "Table name is missing"),
InvalidId(400, "Skyflow id is missing"),
InvalidFields(400, "Fields are missing"),
InvalidQueryInput(400, "Invalid query input"),
InvalidQuery(400, "Query is missing"),
InvalidSkyflowId(400, "Skyflow id are missing"),
InvalidToken(400, "Token is empty"),
InvalidDetokenizeInput(400, "Invalid Detokenize Input"),
Expand Down Expand Up @@ -66,7 +68,7 @@ public int getCode() {
return code;
}

public String getDescription() {
public String getDescription() {
return description;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/skyflow/logs/ErrorLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum ErrorLogs {
InvalidUpdateInput("invalid update input"),
InvalidDeleteInput("invalid delete input"),
InvalidDetokenizeInput("invalid detokenize input"),
InvalidQueryInput("Invalid query input"),
ResponseParsingError("Unable to parse response in %s1 method"),
ThreadInterruptedException("Thread was interrupted in %s1 method"),
ThreadExecutionException("ThreadExecution exception in %s1 method"),
Expand Down Expand Up @@ -40,6 +41,7 @@ public enum ErrorLogs {
InvalidBearerToken("Invalid Bearer token"),
InvalidTable("Table name is missing"),
InvalidId("Skyflow id is missing"),
InvalidQuery("Query is missing"),

Server("Internal server error"),
ServerReturnedErrors("Server returned errors, check SkyflowException.getData() for more"),
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/skyflow/logs/InfoLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum InfoLogs {
GetMethodCalled("get method has triggered"),
InvokeConnectionCalled("invokeConnection method has triggered"),
GenerateBearerTokenCalled("generateBearerToken method has triggered"),
QuerySupportCalled("query method has triggered"),
GenerateBearerTokenFromCredsCalled("generateBearerTokenFromCreds method has triggered"),
ValidatingUpsertOptions("validating upsert options.");
private final String log;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/skyflow/vault/Skyflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public JSONObject insert(JSONObject records) throws SkyflowException {
return insert(records, new InsertOptions(true));
}

public JSONObject query(JSONObject queryObject) throws SkyflowException {
return query(queryObject, new QueryOptions());
}

public JSONObject update(JSONObject records) throws SkyflowException {
return update(records, new UpdateOptions(true));
}
Expand Down Expand Up @@ -446,4 +450,36 @@ public JSONObject invokeConnection(JSONObject connectionConfig) throws SkyflowEx
}
return connectionResponse;
}

public JSONObject query(JSONObject queryObject,QueryOptions queryOptions) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.QuerySupportCalled.getLog());
Validators.validateConfiguration(configuration);
LogUtil.printInfoLog(Helpers.parameterizedString(InfoLogs.ValidatedSkyflowConfiguration.getLog(), "query"));
JSONObject queryResponse = null;
try {
JSONObject queryJsonbject = (JSONObject) queryObject;

QueryRecordInput queryInput = new ObjectMapper().readValue(queryJsonbject.toString(), QueryRecordInput.class);

JSONObject requestBody = Helpers.constructQueryRequest(queryInput, queryOptions);

String url = configuration.getVaultURL() + "/v1/vaults/" + configuration.getVaultID() + "/query";
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + TokenUtils.getBearerToken(configuration.getTokenProvider()));
headers.put(Constants.SDK_METRICS_HEADER_KEY, Helpers.getMetrics().toJSONString());
String response = HttpUtility.sendRequest("POST", new URL(url), requestBody, headers);
queryResponse = (JSONObject) new JSONParser().parse(response);
} catch (IOException e) {
LogUtil.printErrorLog(ErrorLogs.InvalidQueryInput.getLog());
throw new SkyflowException(ErrorCode.InvalidQueryInput,e);
} catch (ParseException e) {
LogUtil.printErrorLog(Helpers.parameterizedString(ErrorLogs.ResponseParsingError.getLog(), "query"));
throw new SkyflowException(ErrorCode.ResponseParsingError, e);
}
catch (SkyflowException e) {
JSONObject queryErrorResponse = Helpers.constructQueryErrorObject(e);
throw new SkyflowException(400, "Query is missing", queryErrorResponse);
}
return queryResponse;
}
}
Loading

0 comments on commit f52266e

Please sign in to comment.