|
| 1 | +package com.skyflow.vault; |
| 2 | + |
| 3 | +import com.skyflow.common.utils.Helpers; |
| 4 | +import com.skyflow.common.utils.HttpUtility; |
| 5 | +import com.skyflow.common.utils.LogUtil; |
| 6 | +import com.skyflow.common.utils.Validators; |
| 7 | +import com.skyflow.entities.DeleteOptions; |
| 8 | +import com.skyflow.entities.DeleteRecordInput; |
| 9 | +import com.skyflow.errors.ErrorCode; |
| 10 | +import com.skyflow.errors.SkyflowException; |
| 11 | +import com.skyflow.logs.ErrorLogs; |
| 12 | +import org.json.simple.JSONArray; |
| 13 | +import org.json.simple.JSONObject; |
| 14 | +import org.json.simple.parser.JSONParser; |
| 15 | +import org.json.simple.parser.ParseException; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.Callable; |
| 21 | + |
| 22 | +public class DeleteBySkyflowId implements Callable<String> { |
| 23 | + private final DeleteRecordInput recordInput; |
| 24 | + private final String vaultID; |
| 25 | + private final String vaultURL; |
| 26 | + private final Map<String, String> headers; |
| 27 | + private final DeleteOptions deleteOptions; |
| 28 | + |
| 29 | + public DeleteBySkyflowId(DeleteRecordInput recordInput, String vaultID, String vaultURL, Map<String, String> headers, DeleteOptions deleteOptions) { |
| 30 | + this.recordInput = recordInput; |
| 31 | + this.vaultID = vaultID; |
| 32 | + this.vaultURL = vaultURL; |
| 33 | + this.headers = headers; |
| 34 | + this.deleteOptions = deleteOptions; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + @Override |
| 39 | + public String call() throws SkyflowException { |
| 40 | + String response = null; |
| 41 | + try { |
| 42 | + Validators.validateDeleteBySkyflowId(recordInput); |
| 43 | + String url = vaultURL+ "/v1/vaults/"+ vaultID + "/" + recordInput.getTable() + "/" + recordInput.getId(); |
| 44 | + response = HttpUtility.sendRequest("DELETE", new URL(url), null,headers); |
| 45 | + JSONObject formattedResponse = new JSONObject(); |
| 46 | + JSONArray formattedRecords = new JSONArray(); |
| 47 | + |
| 48 | + JSONObject responseRecords = ((JSONObject) (new JSONParser().parse(response))); |
| 49 | + if (responseRecords != null && responseRecords.size() > 0) { |
| 50 | + String id = (String) responseRecords.get("skyflow_id"); |
| 51 | + JSONObject formattedRecord = new JSONObject(); |
| 52 | + formattedRecord.put("skyflow_id", responseRecords.get("skyflow_id")); |
| 53 | + formattedRecord.put("deleted", responseRecords.get("deleted")); |
| 54 | + formattedRecords.add(formattedRecord); |
| 55 | + } |
| 56 | + formattedResponse.put("records", formattedRecords); |
| 57 | + response = formattedResponse.toJSONString(); |
| 58 | + |
| 59 | + } catch (IOException e) { |
| 60 | + LogUtil.printErrorLog(ErrorLogs.Server.getLog()); |
| 61 | + throw new SkyflowException(ErrorCode.Server, e); |
| 62 | + } catch (ParseException e) { |
| 63 | + LogUtil.printErrorLog(ErrorLogs.ResponseParsingError.getLog()); |
| 64 | + throw new SkyflowException(ErrorCode.ResponseParsingError, e); |
| 65 | + } catch (SkyflowException e) { |
| 66 | + response = constructDeleteByIdErrorObject(e, recordInput.getId()); |
| 67 | + } |
| 68 | + return response; |
| 69 | + } |
| 70 | + private String constructDeleteByIdErrorObject(SkyflowException skyflowException, String id){ |
| 71 | + String deleteByIdResponse = null; |
| 72 | + JSONObject finalResponseError = new JSONObject(); |
| 73 | + finalResponseError.put("id", id); |
| 74 | + try{ |
| 75 | + JSONObject errorObject = (JSONObject) ((JSONObject) new JSONParser().parse(skyflowException.getMessage())).get("error"); |
| 76 | + if (errorObject != null) { |
| 77 | + JSONObject responseError = new JSONObject(); |
| 78 | + responseError.put("code", errorObject.get("http_code")); |
| 79 | + responseError.put("description", errorObject.get("message")); |
| 80 | + finalResponseError.put("error", responseError); |
| 81 | + |
| 82 | + deleteByIdResponse = finalResponseError.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + } catch (ParseException e){ |
| 86 | + JSONObject responseError = new JSONObject(); |
| 87 | + responseError.put("code", skyflowException.getCode()); |
| 88 | + responseError.put("description", skyflowException.getMessage()); |
| 89 | + finalResponseError.put("error", responseError); |
| 90 | + deleteByIdResponse = finalResponseError.toString(); |
| 91 | + } |
| 92 | + return deleteByIdResponse; |
| 93 | + } |
| 94 | +} |
0 commit comments