|
| 1 | +package com.sbxcloud.android.sbxcloudsdk.net.cloudscript; |
| 2 | + |
| 3 | +import com.sbxcloud.android.sbxcloudsdk.cloudscript.SbxCloudScriptHelper; |
| 4 | +import com.sbxcloud.android.sbxcloudsdk.net.ApiManager; |
| 5 | +import com.sbxcloud.android.sbxcloudsdk.net.callback.SbxSimpleResponse; |
| 6 | +import com.sbxcloud.android.sbxcloudsdk.net.model.SbxModel; |
| 7 | +import com.sbxcloud.android.sbxcloudsdk.query.SbxModelHelper; |
| 8 | +import com.sbxcloud.android.sbxcloudsdk.util.SbxDataValidator; |
| 9 | +import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer; |
| 10 | + |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONObject; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import io.reactivex.Single; |
| 20 | +import io.reactivex.SingleEmitter; |
| 21 | +import io.reactivex.SingleOnSubscribe; |
| 22 | +import okhttp3.Call; |
| 23 | +import okhttp3.Callback; |
| 24 | +import okhttp3.Request; |
| 25 | +import okhttp3.Response; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by lgguzman on 12/06/17. |
| 29 | + */ |
| 30 | + |
| 31 | +public class SbxCloudScript { |
| 32 | + |
| 33 | + private String key; |
| 34 | + private JSONObject params; |
| 35 | + private int cloudScriptId; |
| 36 | + private int runId; |
| 37 | + private double duration; |
| 38 | + private JSONObject bodyResult; |
| 39 | + private List<Log> outLog=new ArrayList<>(); |
| 40 | + private List<Log> errorLog=new ArrayList<>(); |
| 41 | + |
| 42 | + public double getDuration() { |
| 43 | + return duration; |
| 44 | + } |
| 45 | + |
| 46 | + public int getCloudScriptId() { |
| 47 | + return cloudScriptId; |
| 48 | + } |
| 49 | + |
| 50 | + public JSONObject getBodyResult() { |
| 51 | + return bodyResult; |
| 52 | + } |
| 53 | + |
| 54 | + public List<Log> getOutLog() { |
| 55 | + return outLog; |
| 56 | + } |
| 57 | + |
| 58 | + public List<Log> getErrorLog() { |
| 59 | + return errorLog; |
| 60 | + } |
| 61 | + |
| 62 | + public String getKey() { |
| 63 | + return key; |
| 64 | + } |
| 65 | + |
| 66 | + public void setKey(String key) { |
| 67 | + this.key = key; |
| 68 | + } |
| 69 | + |
| 70 | + public JSONObject getParams() { |
| 71 | + return params; |
| 72 | + } |
| 73 | + |
| 74 | + public void setParams(JSONObject params) { |
| 75 | + this.params = params; |
| 76 | + } |
| 77 | + |
| 78 | + public SbxCloudScript(String key, JSONObject params) { |
| 79 | + this.key = key; |
| 80 | + this.params = params; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + public void runInBackground(final SbxSimpleResponse<SbxCloudScript> simpleResponse)throws Exception{ |
| 86 | + SbxUrlComposer sbxUrlComposer = SbxCloudScriptHelper.getUrlRunCloudScript(getKey(),getParams()); |
| 87 | + Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer); |
| 88 | + ApiManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() { |
| 89 | + @Override |
| 90 | + public void onFailure(Call call, IOException e) { |
| 91 | + simpleResponse.onError(e); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onResponse(Call call, Response response) throws IOException { |
| 96 | + try { |
| 97 | + JSONObject jsonObject = new JSONObject(response.body().string()); |
| 98 | + if(jsonObject.getBoolean("success")) { |
| 99 | + update(jsonObject); |
| 100 | + simpleResponse.onSuccess(SbxCloudScript.this); |
| 101 | + }else{ |
| 102 | + simpleResponse.onError(new Exception(jsonObject.getString("error"))); |
| 103 | + } |
| 104 | + }catch (Exception e ){ |
| 105 | + simpleResponse.onError(e); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + public Single<SbxCloudScript> run()throws Exception{ |
| 112 | + SbxUrlComposer sbxUrlComposer = SbxCloudScriptHelper.getUrlRunCloudScript(getKey(),getParams()); |
| 113 | + final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer); |
| 114 | + return Single.create(new SingleOnSubscribe<SbxCloudScript>() { |
| 115 | + @Override |
| 116 | + public void subscribe(final SingleEmitter<SbxCloudScript> e) throws Exception { |
| 117 | + new Thread(new Runnable() { |
| 118 | + @Override |
| 119 | + public void run() { |
| 120 | + try { |
| 121 | + Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute(); |
| 122 | + JSONObject jsonObject = new JSONObject(response.body().string()); |
| 123 | + if (jsonObject.getBoolean("success")) { |
| 124 | + update(jsonObject); |
| 125 | + e.onSuccess(SbxCloudScript.this); |
| 126 | + //sucess |
| 127 | + } else { |
| 128 | + //error |
| 129 | + e.onError(new Exception(jsonObject.getString("error"))); |
| 130 | + } |
| 131 | + }catch (Exception ex){ |
| 132 | + e.onError(ex); |
| 133 | + } |
| 134 | + } |
| 135 | + }).start(); |
| 136 | + |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + private void update(JSONObject jsonObject) throws Exception{ |
| 143 | + JSONObject temp = jsonObject.getJSONObject("cloud_script_run"); |
| 144 | + cloudScriptId = temp.getInt("cloud_script_id"); |
| 145 | + runId = temp.getInt("id"); |
| 146 | + duration = temp.getDouble("duration"); |
| 147 | + JSONArray tempoutLog=temp.getJSONArray("out_log"); |
| 148 | + JSONArray tempErrorLog=temp.getJSONArray("error_log"); |
| 149 | + for (int i=0;i<tempoutLog.length();i++){ |
| 150 | + outLog.add(new Log(tempoutLog.getJSONObject(i).optString("date"), |
| 151 | + tempoutLog.getJSONObject(i).optString("body"))); |
| 152 | + } |
| 153 | + for (int i=0;i<tempErrorLog.length();i++){ |
| 154 | + errorLog.add(new Log(tempErrorLog.getJSONObject(i).optString("date"), |
| 155 | + tempErrorLog.getJSONObject(i).optString("body"))); |
| 156 | + } |
| 157 | + bodyResult= jsonObject.getJSONObject("response").getJSONObject("body"); |
| 158 | + } |
| 159 | + |
| 160 | + class Log{ |
| 161 | + private Date date; |
| 162 | + private String body; |
| 163 | + |
| 164 | + public Log(String date, String body) { |
| 165 | + try { |
| 166 | + this.date = SbxDataValidator.getDate(date); |
| 167 | + }catch (Exception ex){} |
| 168 | + this.body = body; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments