Skip to content

Commit ea6e225

Browse files
committed
Added execSync method
1 parent 9e29f0e commit ea6e225

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/io/appsfly/core/AppInstance.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,46 @@ public void onResponse(Call call, final okhttp3.Response response) throws IOExce
8282
}
8383
});
8484
}
85+
86+
public JSONObject execSync(final String intent, final JSONObject intentData, final String userID) throws AppsflyException{
87+
final JSONObject body = new JSONObject() {{
88+
put("intent", intent);
89+
put("data", intentData);
90+
}};
91+
String payload = body + "|" + microModuleId + "|" + config.appKey + "|" + userID;
92+
String checksum = CtyptoUtil.getInstance().getChecksum(payload.getBytes(), config.secretKey);
93+
OkHttpClient httpClient = new OkHttpClient();
94+
Request request = new Request.Builder()
95+
.url(this.config.repoUrl+"/executor/exec")
96+
.addHeader("X-Module-Handle", microModuleId)
97+
.addHeader("X-App-Key", config.appKey)
98+
.addHeader("X-Checksum", checksum)
99+
.addHeader("X-UUID", userID)
100+
.post(RequestBody.create(JSON, body.toString()))
101+
.build();
102+
try {
103+
final Response response = httpClient.newCall(request).execute();
104+
String responseChecksum = response.headers().get("X-Checksum");
105+
if(responseChecksum!=null){
106+
boolean verified = CtyptoUtil.getInstance().verifychecksum(response.body().bytes(), responseChecksum, config.secretKey);
107+
if (verified){
108+
return new JSONObject(response.body().bytes());
109+
}
110+
else{
111+
throw new AppsflyException("Checksum Validation Failed");
112+
}
113+
}
114+
else{
115+
final JSONObject responseBody = new JSONObject(response.body().string());
116+
if(responseBody.has("error")){
117+
throw new AppsflyException(responseBody.getJSONObject("error").getString("message"));
118+
} else {
119+
return responseBody;
120+
}
121+
}
122+
} catch (IOException e) {
123+
e.printStackTrace();
124+
return null;
125+
}
126+
}
85127
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.appsfly.core;
2+
3+
public class AppsflyException extends Exception {
4+
private final String message;
5+
AppsflyException(String message) {
6+
this.message = message;
7+
}
8+
}

0 commit comments

Comments
 (0)