-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from sbxcloud/annotationlib
Annotationlib
- Loading branch information
Showing
7 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ android { | |
testOptions { | ||
unitTests.returnDefaultValues = true | ||
} | ||
dataBinding { | ||
enabled = true | ||
} | ||
|
||
} | ||
|
||
|
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/sbxcloud/android/sbxcloudsdk/cloudscript/SbxCloudScriptHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.sbxcloud.android.sbxcloudsdk.cloudscript; | ||
|
||
import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth; | ||
import com.sbxcloud.android.sbxcloudsdk.query.SbxQueryBuilder; | ||
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer; | ||
import com.sbxcloud.android.sbxcloudsdk.util.UrlHelper; | ||
|
||
import org.json.JSONObject; | ||
|
||
/** | ||
* Created by lgguzman on 12/06/17. | ||
*/ | ||
|
||
public class SbxCloudScriptHelper { | ||
|
||
/** | ||
* | ||
* @param key llave del cloudScript | ||
* @param params parametros del CloudScript | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public static SbxUrlComposer getUrlRunCloudScript(String key, JSONObject params)throws Exception { | ||
|
||
// int domain = SbxAuth.getDefaultSbxAuth().getDomain(); | ||
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey(); | ||
String token = SbxAuth.getDefaultSbxAuth().getToken(); | ||
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer( UrlHelper.CLOUDSCRIPT_RUN, UrlHelper.POST); | ||
JSONObject jsonObject = new JSONObject(); | ||
try{ | ||
jsonObject.put("key",key); | ||
jsonObject.put("params",params); | ||
}catch (Exception ex){} | ||
return sbxUrlComposer | ||
.addHeader(UrlHelper.HEADER_KEY_APP_KEY, appKey) | ||
// .addHeader(UrlHelper.HEADER_KEY_ENCODING, UrlHelper.HEADER_GZIP) | ||
.addHeader(UrlHelper.HEADER_KEY_CONTENT_TYPE, UrlHelper.HEADER_JSON) | ||
.addHeader(UrlHelper.HEADER_KEY_AUTORIZATION, UrlHelper.HEADER_BEARER+token) | ||
.addBody(jsonObject); | ||
} | ||
|
||
} |
171 changes: 171 additions & 0 deletions
171
app/src/main/java/com/sbxcloud/android/sbxcloudsdk/net/cloudscript/SbxCloudScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package com.sbxcloud.android.sbxcloudsdk.net.cloudscript; | ||
|
||
import com.sbxcloud.android.sbxcloudsdk.cloudscript.SbxCloudScriptHelper; | ||
import com.sbxcloud.android.sbxcloudsdk.net.ApiManager; | ||
import com.sbxcloud.android.sbxcloudsdk.net.callback.SbxSimpleResponse; | ||
import com.sbxcloud.android.sbxcloudsdk.net.model.SbxModel; | ||
import com.sbxcloud.android.sbxcloudsdk.query.SbxModelHelper; | ||
import com.sbxcloud.android.sbxcloudsdk.util.SbxDataValidator; | ||
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import io.reactivex.Single; | ||
import io.reactivex.SingleEmitter; | ||
import io.reactivex.SingleOnSubscribe; | ||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* Created by lgguzman on 12/06/17. | ||
*/ | ||
|
||
public class SbxCloudScript { | ||
|
||
private String key; | ||
private JSONObject params; | ||
private int cloudScriptId; | ||
private int runId; | ||
private double duration; | ||
private JSONObject bodyResult; | ||
private List<Log> outLog=new ArrayList<>(); | ||
private List<Log> errorLog=new ArrayList<>(); | ||
|
||
public double getDuration() { | ||
return duration; | ||
} | ||
|
||
public int getCloudScriptId() { | ||
return cloudScriptId; | ||
} | ||
|
||
public JSONObject getBodyResult() { | ||
return bodyResult; | ||
} | ||
|
||
public List<Log> getOutLog() { | ||
return outLog; | ||
} | ||
|
||
public List<Log> getErrorLog() { | ||
return errorLog; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public JSONObject getParams() { | ||
return params; | ||
} | ||
|
||
public void setParams(JSONObject params) { | ||
this.params = params; | ||
} | ||
|
||
public SbxCloudScript(String key, JSONObject params) { | ||
this.key = key; | ||
this.params = params; | ||
} | ||
|
||
|
||
|
||
public void runInBackground(final SbxSimpleResponse<SbxCloudScript> simpleResponse)throws Exception{ | ||
SbxUrlComposer sbxUrlComposer = SbxCloudScriptHelper.getUrlRunCloudScript(getKey(),getParams()); | ||
Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer); | ||
ApiManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() { | ||
@Override | ||
public void onFailure(Call call, IOException e) { | ||
simpleResponse.onError(e); | ||
} | ||
|
||
@Override | ||
public void onResponse(Call call, Response response) throws IOException { | ||
try { | ||
JSONObject jsonObject = new JSONObject(response.body().string()); | ||
if(jsonObject.getBoolean("success")) { | ||
update(jsonObject); | ||
simpleResponse.onSuccess(SbxCloudScript.this); | ||
}else{ | ||
simpleResponse.onError(new Exception(jsonObject.getString("error"))); | ||
} | ||
}catch (Exception e ){ | ||
simpleResponse.onError(e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public Single<SbxCloudScript> run()throws Exception{ | ||
SbxUrlComposer sbxUrlComposer = SbxCloudScriptHelper.getUrlRunCloudScript(getKey(),getParams()); | ||
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer); | ||
return Single.create(new SingleOnSubscribe<SbxCloudScript>() { | ||
@Override | ||
public void subscribe(final SingleEmitter<SbxCloudScript> e) throws Exception { | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute(); | ||
JSONObject jsonObject = new JSONObject(response.body().string()); | ||
if (jsonObject.getBoolean("success")) { | ||
update(jsonObject); | ||
e.onSuccess(SbxCloudScript.this); | ||
//sucess | ||
} else { | ||
//error | ||
e.onError(new Exception(jsonObject.getString("error"))); | ||
} | ||
}catch (Exception ex){ | ||
e.onError(ex); | ||
} | ||
} | ||
}).start(); | ||
|
||
} | ||
}); | ||
|
||
} | ||
|
||
private void update(JSONObject jsonObject) throws Exception{ | ||
JSONObject temp = jsonObject.getJSONObject("cloud_script_run"); | ||
cloudScriptId = temp.getInt("cloud_script_id"); | ||
runId = temp.getInt("id"); | ||
duration = temp.getDouble("duration"); | ||
JSONArray tempoutLog=temp.getJSONArray("out_log"); | ||
JSONArray tempErrorLog=temp.getJSONArray("error_log"); | ||
for (int i=0;i<tempoutLog.length();i++){ | ||
outLog.add(new Log(tempoutLog.getJSONObject(i).optString("date"), | ||
tempoutLog.getJSONObject(i).optString("body"))); | ||
} | ||
for (int i=0;i<tempErrorLog.length();i++){ | ||
errorLog.add(new Log(tempErrorLog.getJSONObject(i).optString("date"), | ||
tempErrorLog.getJSONObject(i).optString("body"))); | ||
} | ||
bodyResult= jsonObject.getJSONObject("response").getJSONObject("body"); | ||
} | ||
|
||
class Log{ | ||
private Date date; | ||
private String body; | ||
|
||
public Log(String date, String body) { | ||
try { | ||
this.date = SbxDataValidator.getDate(date); | ||
}catch (Exception ex){} | ||
this.body = body; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/sbxcloud/android/sbxcloudsdk/net/observable/SbxModelObservable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.sbxcloud.android.sbxcloudsdk.net.observable; | ||
|
||
|
||
import android.databinding.Bindable; | ||
import android.databinding.Observable; | ||
import android.databinding.PropertyChangeRegistry; | ||
|
||
import com.sbxcloud.android.sbxcloudsdk.net.model.SbxModel; | ||
|
||
/** | ||
* Created by lgguzman on 12/06/17. | ||
*/ | ||
|
||
public class SbxModelObservable extends SbxModel implements Observable { | ||
|
||
private transient PropertyChangeRegistry mCallbacks; | ||
|
||
/** | ||
* Notifies listeners that a specific property has changed. The getter for the property | ||
* that changes should be marked with {@link Bindable} to generate a field in | ||
* <code>BR</code> to be used as <code>fieldId</code>. | ||
* | ||
* @param fieldId The generated BR id for the Bindable field. | ||
*/ | ||
public void notifyPropertyChanged(int fieldId) { | ||
if (mCallbacks != null) { | ||
mCallbacks.notifyCallbacks(this, fieldId, null); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) { | ||
if (mCallbacks == null) { | ||
mCallbacks = new PropertyChangeRegistry(); | ||
} | ||
mCallbacks.add(callback); | ||
} | ||
|
||
@Override | ||
public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) { | ||
if (mCallbacks != null) { | ||
mCallbacks.remove(callback); | ||
} | ||
} | ||
|
||
/** | ||
* Notifies listeners that all properties of this instance have changed. | ||
*/ | ||
public synchronized void notifyChange() { | ||
if (mCallbacks != null) { | ||
mCallbacks.notifyCallbacks(this, 0, null); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/sbxcloud/android/sbxcloudsdk/net/observable/SbxUserObservable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.sbxcloud.android.sbxcloudsdk.net.observable; | ||
|
||
import com.sbxcloud.android.sbxcloudsdk.net.auth.SbxUser; | ||
|
||
import android.databinding.Bindable; | ||
import android.databinding.Observable; | ||
import android.databinding.PropertyChangeRegistry; | ||
|
||
/** | ||
* Created by lgguzman on 29/05/17. | ||
*/ | ||
|
||
public class SbxUserObservable extends SbxUser implements Observable { | ||
|
||
private transient PropertyChangeRegistry mCallbacks; | ||
|
||
/** | ||
* Notifies listeners that a specific property has changed. The getter for the property | ||
* that changes should be marked with {@link Bindable} to generate a field in | ||
* <code>BR</code> to be used as <code>fieldId</code>. | ||
* | ||
* @param fieldId The generated BR id for the Bindable field. | ||
*/ | ||
public void notifyPropertyChanged(int fieldId) { | ||
if (mCallbacks != null) { | ||
mCallbacks.notifyCallbacks(this, fieldId, null); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) { | ||
if (mCallbacks == null) { | ||
mCallbacks = new PropertyChangeRegistry(); | ||
} | ||
mCallbacks.add(callback); | ||
} | ||
|
||
@Override | ||
public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) { | ||
if (mCallbacks != null) { | ||
mCallbacks.remove(callback); | ||
} | ||
} | ||
|
||
/** | ||
* Notifies listeners that all properties of this instance have changed. | ||
*/ | ||
public synchronized void notifyChange() { | ||
if (mCallbacks != null) { | ||
mCallbacks.notifyCallbacks(this, 0, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters