Skip to content

Commit 18b863f

Browse files
authored
Merge pull request #23 from sbxcloud/annotationlib
Annotationlib
2 parents 363e115 + 0f39859 commit 18b863f

7 files changed

Lines changed: 347 additions & 1 deletion

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Agregamos la librería como dependencia
2424

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

390390

391+
Ya puedes usar DataBinding y Ejecutar CLoudScript
392+
393+
```java
394+
395+
396+
public class User extends SbxUserObservable {
397+
398+
....
399+
@Bindable
400+
public String getPhone() {
401+
return phone;
402+
}
403+
404+
public void setPhone(String phone) {
405+
this.phone =
406+
notifyPropertyChanged(BR.phone);
407+
}
408+
...
409+
}
410+
411+
```
412+
391413

392414

393415

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ android {
2424
testOptions {
2525
unitTests.returnDefaultValues = true
2626
}
27+
dataBinding {
28+
enabled = true
29+
}
2730

2831
}
2932

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.sbxcloud.android.sbxcloudsdk.cloudscript;
2+
3+
import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth;
4+
import com.sbxcloud.android.sbxcloudsdk.query.SbxQueryBuilder;
5+
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer;
6+
import com.sbxcloud.android.sbxcloudsdk.util.UrlHelper;
7+
8+
import org.json.JSONObject;
9+
10+
/**
11+
* Created by lgguzman on 12/06/17.
12+
*/
13+
14+
public class SbxCloudScriptHelper {
15+
16+
/**
17+
*
18+
* @param key llave del cloudScript
19+
* @param params parametros del CloudScript
20+
* @return
21+
* @throws Exception
22+
*/
23+
public static SbxUrlComposer getUrlRunCloudScript(String key, JSONObject params)throws Exception {
24+
25+
// int domain = SbxAuth.getDefaultSbxAuth().getDomain();
26+
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
27+
String token = SbxAuth.getDefaultSbxAuth().getToken();
28+
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer( UrlHelper.CLOUDSCRIPT_RUN, UrlHelper.POST);
29+
JSONObject jsonObject = new JSONObject();
30+
try{
31+
jsonObject.put("key",key);
32+
jsonObject.put("params",params);
33+
}catch (Exception ex){}
34+
return sbxUrlComposer
35+
.addHeader(UrlHelper.HEADER_KEY_APP_KEY, appKey)
36+
// .addHeader(UrlHelper.HEADER_KEY_ENCODING, UrlHelper.HEADER_GZIP)
37+
.addHeader(UrlHelper.HEADER_KEY_CONTENT_TYPE, UrlHelper.HEADER_JSON)
38+
.addHeader(UrlHelper.HEADER_KEY_AUTORIZATION, UrlHelper.HEADER_BEARER+token)
39+
.addBody(jsonObject);
40+
}
41+
42+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.sbxcloud.android.sbxcloudsdk.net.observable;
2+
3+
4+
import android.databinding.Bindable;
5+
import android.databinding.Observable;
6+
import android.databinding.PropertyChangeRegistry;
7+
8+
import com.sbxcloud.android.sbxcloudsdk.net.model.SbxModel;
9+
10+
/**
11+
* Created by lgguzman on 12/06/17.
12+
*/
13+
14+
public class SbxModelObservable extends SbxModel implements Observable {
15+
16+
private transient PropertyChangeRegistry mCallbacks;
17+
18+
/**
19+
* Notifies listeners that a specific property has changed. The getter for the property
20+
* that changes should be marked with {@link Bindable} to generate a field in
21+
* <code>BR</code> to be used as <code>fieldId</code>.
22+
*
23+
* @param fieldId The generated BR id for the Bindable field.
24+
*/
25+
public void notifyPropertyChanged(int fieldId) {
26+
if (mCallbacks != null) {
27+
mCallbacks.notifyCallbacks(this, fieldId, null);
28+
}
29+
}
30+
31+
@Override
32+
public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
33+
if (mCallbacks == null) {
34+
mCallbacks = new PropertyChangeRegistry();
35+
}
36+
mCallbacks.add(callback);
37+
}
38+
39+
@Override
40+
public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
41+
if (mCallbacks != null) {
42+
mCallbacks.remove(callback);
43+
}
44+
}
45+
46+
/**
47+
* Notifies listeners that all properties of this instance have changed.
48+
*/
49+
public synchronized void notifyChange() {
50+
if (mCallbacks != null) {
51+
mCallbacks.notifyCallbacks(this, 0, null);
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.sbxcloud.android.sbxcloudsdk.net.observable;
2+
3+
import com.sbxcloud.android.sbxcloudsdk.net.auth.SbxUser;
4+
5+
import android.databinding.Bindable;
6+
import android.databinding.Observable;
7+
import android.databinding.PropertyChangeRegistry;
8+
9+
/**
10+
* Created by lgguzman on 29/05/17.
11+
*/
12+
13+
public class SbxUserObservable extends SbxUser implements Observable {
14+
15+
private transient PropertyChangeRegistry mCallbacks;
16+
17+
/**
18+
* Notifies listeners that a specific property has changed. The getter for the property
19+
* that changes should be marked with {@link Bindable} to generate a field in
20+
* <code>BR</code> to be used as <code>fieldId</code>.
21+
*
22+
* @param fieldId The generated BR id for the Bindable field.
23+
*/
24+
public void notifyPropertyChanged(int fieldId) {
25+
if (mCallbacks != null) {
26+
mCallbacks.notifyCallbacks(this, fieldId, null);
27+
}
28+
}
29+
30+
@Override
31+
public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
32+
if (mCallbacks == null) {
33+
mCallbacks = new PropertyChangeRegistry();
34+
}
35+
mCallbacks.add(callback);
36+
}
37+
38+
@Override
39+
public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
40+
if (mCallbacks != null) {
41+
mCallbacks.remove(callback);
42+
}
43+
}
44+
45+
/**
46+
* Notifies listeners that all properties of this instance have changed.
47+
*/
48+
public synchronized void notifyChange() {
49+
if (mCallbacks != null) {
50+
mCallbacks.notifyCallbacks(this, 0, null);
51+
}
52+
}
53+
}

app/src/main/java/com/sbxcloud/android/sbxcloudsdk/util/UrlHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public interface UrlHelper {
2424
public static final String MESSAGE_CHANNEL_MEMBER = "api/message/v1/channel/member";
2525
public static final String MESSAGE_LIST = "api/message/v1/list";
2626
public static final String MESSAGE_SEND = "api/message/v1/send";
27+
public static final String CLOUDSCRIPT_RUN = "api/cloudscript/v1/run";
2728
public static final String DOMAIN ="api/domain/v1/list/app";
2829
// public static final String HEADER_GZIP="gzip";
2930
public static final String HEADER_JSON="application/json";

0 commit comments

Comments
 (0)