Skip to content

Commit

Permalink
Merge pull request #23 from sbxcloud/annotationlib
Browse files Browse the repository at this point in the history
Annotationlib
  • Loading branch information
lgguzman committed Jun 12, 2017
2 parents 363e115 + 0f39859 commit 18b863f
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 1 deletion.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Agregamos la librería como dependencia

dependencies {
//...otras dependencias de tu proyeco aquí.....
compile 'com.github.sbxcloud:androidlib:v2.2.2'
compile 'com.github.sbxcloud:androidlib:v2.3.0'
}
Esta librería se basa en annotaciones. Para crear tu propia Clase usuario puedes hacerla así:
Expand Down Expand Up @@ -388,6 +388,28 @@ Además puedes utilizar RXJava para cualquira de las anteriores fuciones
```


Ya puedes usar DataBinding y Ejecutar CLoudScript

```java


public class User extends SbxUserObservable {

....
@Bindable
public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone =
notifyPropertyChanged(BR.phone);
}
...
}

```




Expand Down
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ android {
testOptions {
unitTests.returnDefaultValues = true
}
dataBinding {
enabled = true
}

}

Expand Down
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);
}

}
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;
}
}
}
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);
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface UrlHelper {
public static final String MESSAGE_CHANNEL_MEMBER = "api/message/v1/channel/member";
public static final String MESSAGE_LIST = "api/message/v1/list";
public static final String MESSAGE_SEND = "api/message/v1/send";
public static final String CLOUDSCRIPT_RUN = "api/cloudscript/v1/run";
public static final String DOMAIN ="api/domain/v1/list/app";
// public static final String HEADER_GZIP="gzip";
public static final String HEADER_JSON="application/json";
Expand Down

0 comments on commit 18b863f

Please sign in to comment.