Skip to content

Commit

Permalink
Added validation for the payload json
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Bassini committed Jan 27, 2023
1 parent 1f21be5 commit 099fb64
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions java/deepgram_audio_summary/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public RuntimeResponse main(RuntimeRequest req,RuntimeResponse res)throws Except

// Validate the requested payload (URL)
String payloadString=req.getPayload().toString();
Map<String, Object> payload=gson.fromJson(payloadString,Map.class);

errorResponse=validatePayload(payload,res);

errorResponse=validatePayload(payloadString,res);
if(errorResponse!=null){
return errorResponse;
}

Map<String, Object> payload = gson.fromJson(payloadString,Map.class);

// Get file url from payload
String fileurl=payload.get("fileUrl").toString();

Expand Down Expand Up @@ -125,8 +126,16 @@ private RuntimeResponse checkEmptyAPIKey(RuntimeRequest req,RuntimeResponse res,
* @param payload is the object that contains the URL
* @return null if payload is valid, otherwise an error response
*/
private RuntimeResponse validatePayload(Map<String, Object> payload,RuntimeResponse res){
private RuntimeResponse validatePayload(String payloadString,RuntimeResponse res){
Map<String, Object> responseData=new HashMap<>();
Map<String, Object> payload=new HashMap<>();;
try{
payload=gson.fromJson(payloadString,Map.class);
} catch (Exception e) {
responseData.put("success",false);
responseData.put("message","The payload is invalid. Example of valid payload:{\"fileUrl\": \"...\"}");
return res.json(responseData);
}

// Validate that payload has fileUrl
if(!payload.containsKey("fileUrl")){
Expand Down Expand Up @@ -154,18 +163,20 @@ private RuntimeResponse validatePayload(Map<String, Object> payload,RuntimeRespo
* @return null is nothing is empty, otherwise an error response
*/
private RuntimeResponse checkEmptyPayloadAndVariables(RuntimeRequest req,RuntimeResponse res){
Map<String, Object> responseData=new HashMap<>();

Map<String, Object> responseData=new HashMap<>();

if(req.getPayload()==null||req.getPayload().trim().isEmpty()||req.getPayload().trim().equals("{}")){
if(req.getPayload()==null||req.getPayload().trim().isEmpty()||req.getPayload().trim().equals("{}")){
responseData.put("success",false);
responseData.put("message","Payload is empty, expected a payload with provider and URL");
return res.json(responseData);
}

if(req.getVariables()==null){
return res.json(responseData);

}
if(req.getVariables()==null){
responseData.put("success",false);
responseData.put("message","Empty function variables found. You need to pass an API key for the provider");
return res.json(responseData);
}
return null;
return res.json(responseData);
}

return null;
}

0 comments on commit 099fb64

Please sign in to comment.