Skip to content

Commit 9f8f30c

Browse files
committed
Updated return format
1 parent 86dfc66 commit 9f8f30c

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.appsfly.core;
22

33
import io.appsfly.crypto.CtyptoUtil;
4+
import io.appsfly.util.json.JSONArray;
45
import io.appsfly.util.json.JSONObject;
6+
import io.appsfly.util.json.JSONTokener;
57
import okhttp3.*;
68

79
import java.io.IOException;
@@ -26,24 +28,26 @@ public AFConfig(String repoUrl, String secretKey, String appKey) {
2628
private String microModuleId;
2729
public static final MediaType JSON
2830
= MediaType.parse("application/json; charset=utf-8");
31+
2932
public AppInstance(AFConfig config, String microModuleId) {
3033
this.config = config;
3134
this.microModuleId = microModuleId;
3235
}
3336

34-
public void exec(final String intent, final JSONObject intentData, final Callback callback){
37+
public void exec(final String intent, final JSONObject intentData, final Callback callback) {
3538
exec(intent, intentData, "generic", callback);
3639
}
37-
public void exec(final String intent, final JSONObject intentData, final String userID, final Callback callback){
40+
41+
public void exec(final String intent, final JSONObject intentData, final String userID, final Callback callback) {
3842
final JSONObject body = new JSONObject() {{
3943
put("intent", intent);
4044
put("data", intentData);
4145
}};
42-
String payload = body + "|" + microModuleId + "|" + config.appKey + "|" + userID;
46+
String payload = body + "|" + microModuleId + "|" + config.appKey + "|" + userID;
4347
String checksum = CtyptoUtil.getInstance().getChecksum(payload.getBytes(), config.secretKey);
4448
OkHttpClient httpClient = new OkHttpClient();
4549
Request request = new Request.Builder()
46-
.url(this.config.repoUrl+"/executor/exec")
50+
.url(this.config.repoUrl + "/executor/exec")
4751
.addHeader("X-Module-Handle", microModuleId)
4852
.addHeader("X-App-Key", config.appKey)
4953
.addHeader("X-Checksum", checksum)
@@ -59,22 +63,20 @@ public void onFailure(Call call, IOException e) {
5963
@Override
6064
public void onResponse(Call call, final okhttp3.Response response) throws IOException {
6165
String checksum = response.headers().get("X-Checksum");
62-
if(checksum!=null){
66+
if (checksum != null) {
6367
byte[] bytes = response.body().bytes();
6468
boolean verified = CtyptoUtil.getInstance().verifychecksum(bytes, checksum, config.secretKey);
65-
if (verified){
69+
if (verified) {
6670
callback.onResponse(new JSONObject(bytes));
67-
}
68-
else{
69-
callback.onError(new JSONObject(){{
71+
} else {
72+
callback.onError(new JSONObject() {{
7073
put("message", "Checksum Validation Failed");
7174
}});
7275
}
73-
}
74-
else{
76+
} else {
7577
final JSONObject responseBody = new JSONObject(response.body().string());
76-
if(responseBody.has("error")){
77-
callback.onError(new JSONObject(){{
78+
if (responseBody.has("error")) {
79+
callback.onError(new JSONObject() {{
7880
put("message", responseBody.getJSONObject("error").get("message"));
7981
put("status", response.code());
8082
}});
@@ -84,20 +86,20 @@ public void onResponse(Call call, final okhttp3.Response response) throws IOExce
8486
});
8587
}
8688

87-
public JSONObject execSync(final String intent, final JSONObject intentData) throws AppsflyException{
89+
public Object execSync(final String intent, final JSONObject intentData) throws AppsflyException {
8890
return execSync(intent, intentData, "generic");
8991
}
9092

91-
public JSONObject execSync(final String intent, final JSONObject intentData, final String userID) throws AppsflyException{
93+
public Object execSync(final String intent, final JSONObject intentData, final String userID) throws AppsflyException {
9294
final JSONObject body = new JSONObject() {{
9395
put("intent", intent);
9496
put("data", intentData);
9597
}};
96-
String payload = body + "|" + microModuleId + "|" + config.appKey + "|" + userID;
98+
String payload = body + "|" + microModuleId + "|" + config.appKey + "|" + userID;
9799
String checksum = CtyptoUtil.getInstance().getChecksum(payload.getBytes(), config.secretKey);
98100
OkHttpClient httpClient = new OkHttpClient();
99101
Request request = new Request.Builder()
100-
.url(this.config.repoUrl+"/executor/exec")
102+
.url(this.config.repoUrl + "/executor/exec")
101103
.addHeader("X-Module-Handle", microModuleId)
102104
.addHeader("X-App-Key", config.appKey)
103105
.addHeader("X-Checksum", checksum)
@@ -107,19 +109,17 @@ public JSONObject execSync(final String intent, final JSONObject intentData, fin
107109
try {
108110
final Response response = httpClient.newCall(request).execute();
109111
String responseChecksum = response.headers().get("X-Checksum");
110-
if(responseChecksum!=null){
112+
if (responseChecksum != null) {
111113
byte[] bytes = response.body().bytes();
112114
boolean verified = CtyptoUtil.getInstance().verifychecksum(bytes, responseChecksum, config.secretKey);
113-
if (verified){
114-
return new JSONObject(new String(bytes));
115-
}
116-
else{
115+
if (verified) {
116+
return new JSONTokener(new String(bytes)).nextValue();
117+
} else {
117118
throw new AppsflyException("Checksum Validation Failed");
118119
}
119-
}
120-
else{
120+
} else {
121121
final JSONObject responseBody = new JSONObject(response.body().string());
122-
if(responseBody.has("error")){
122+
if (responseBody.has("error")) {
123123
throw new AppsflyException(responseBody.getJSONObject("error").getString("message"));
124124
} else {
125125
return responseBody;

0 commit comments

Comments
 (0)