Skip to content

Commit

Permalink
Merge pull request #10 from sbxcloud/annotationlib
Browse files Browse the repository at this point in the history
with chat, push, and changepassword
  • Loading branch information
lgguzman authored Mar 25, 2017
2 parents 2edf104 + 3de1c37 commit be1fa78
Show file tree
Hide file tree
Showing 12 changed files with 822 additions and 4 deletions.
2 changes: 1 addition & 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.1.0'
compile 'com.github.sbxcloud:androidlib:v2.2.0'
}
Esta librería se basa en annotaciones. Para crear tu propia Clase usuario puedes hacerla así:
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

minSdkVersion 15
targetSdkVersion 25
versionCode 0
versionName "2.1.0"
versionCode 3
versionName "2.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer;
import com.sbxcloud.android.sbxcloudsdk.util.UrlHelper;

import org.json.JSONObject;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URL;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
* Created by lgguzman on 18/02/17.
*/
Expand Down Expand Up @@ -415,5 +420,50 @@ public SbxUrlComposer getUrlSigIn(Object o)throws SbxConfigException, SbxAuthExc
// .addHeader(UrlHelper.HEADER_KEY_ENCODING, UrlHelper.HEADER_GZIP);
}

public static SbxUrlComposer getUrlRequestPasswordCode(String emailTemplate, String subject, String from) throws Exception{
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
String token = SbxAuth.getDefaultSbxAuth().getToken();
int domain = SbxAuth.getDefaultSbxAuth().getDomain();
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer(
UrlHelper.PASSWORD_REQUEST
, UrlHelper.POST
);
JSONObject jsonObject = new JSONObject();
jsonObject.put("domain", domain);
jsonObject.put("email_template", emailTemplate);
jsonObject.put("subject", subject);
jsonObject.put("user_email", from);
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);
}


public static SbxUrlComposer getUrlChangePasswordCode(int userId, int code, String password) throws Exception{
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
String token = SbxAuth.getDefaultSbxAuth().getToken();
int domain = SbxAuth.getDefaultSbxAuth().getDomain();
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer(
UrlHelper.PASSWORD_REQUEST
, UrlHelper.PUT
);
JSONObject jsonObject = new JSONObject();
jsonObject.put("domain", domain);
jsonObject.put("user_id", userId);
jsonObject.put("code", code);
jsonObject.put("password", password);
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,91 @@
package com.sbxcloud.android.sbxcloudsdk.message;

import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth;
import com.sbxcloud.android.sbxcloudsdk.query.SbxQueryBuilder;
import com.sbxcloud.android.sbxcloudsdk.util.SbxJsonModeler;
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer;
import com.sbxcloud.android.sbxcloudsdk.util.UrlHelper;

import org.json.JSONArray;
import org.json.JSONObject;

/**
* Created by lgguzman on 24/03/17.
*/

public class ChannelHelper {

public static SbxUrlComposer getUrlCreateChannel(String channelName) throws Exception{
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
String token = SbxAuth.getDefaultSbxAuth().getToken();
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer(
UrlHelper.MESSAGE_CHANNEL
, UrlHelper.POST
);
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", channelName);
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);
}

public static SbxUrlComposer getUrlAddMember(int channelId, int []idUsers) throws Exception{
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
String token = SbxAuth.getDefaultSbxAuth().getToken();
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer(
UrlHelper.MESSAGE_CHANNEL_MEMBER
, UrlHelper.POST
);
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (int i=0;i<idUsers.length;i++){
jsonArray.put(idUsers[i]);
}
jsonObject.put("channel_id", channelId);
jsonObject.put("members", jsonArray);
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);
}

public static SbxUrlComposer getUrlListMessage(int channelId) throws Exception{
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
String token = SbxAuth.getDefaultSbxAuth().getToken();
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer(
UrlHelper.MESSAGE_LIST
, UrlHelper.GET
);
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)
.setUrlParam("channel_id",channelId+"");

}

public static SbxUrlComposer getUrlSendMessage(int channelId, SbxJsonModeler body) throws Exception{
String appKey = SbxAuth.getDefaultSbxAuth().getAppKey();
String token = SbxAuth.getDefaultSbxAuth().getToken();
SbxUrlComposer sbxUrlComposer = new SbxUrlComposer(
UrlHelper.MESSAGE_SEND
, UrlHelper.POST
);

JSONObject jsonObject = new JSONObject();
jsonObject.put("channel_id", channelId);
jsonObject.put("body", body.toJson());
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)
.setUrlParam("channel_id",channelId+"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,114 @@ public String getHomeFolder() {
public JSONArray getMemberOf() {
return memberOf;
}

public static Single<String> sendRequesCode(String emailTemplate, String subject, String from) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getUrlRequestPasswordCode(emailTemplate,subject,from);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<String>() {
@Override
public void subscribe(final SingleEmitter<String> 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")) {
e.onSuccess("success");
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});
}


public static void sendRequesCodeInBackground(String emailTemplate, String subject, String from,final SbxSimpleResponse simpleResponse) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getUrlRequestPasswordCode(emailTemplate,subject,from);
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")) {
simpleResponse.onSuccess("success");
}else{
simpleResponse.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception e ){
simpleResponse.onError(e);
}
}
});
}

public static Single<String> changePassword(int userId, int code, String password) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getUrlChangePasswordCode(userId,code,password);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<String>() {
@Override
public void subscribe(final SingleEmitter<String> 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")) {
e.onSuccess("success");
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});
}


public static void sendRequesCodeInBackground(int userId, int code, String password, final SbxSimpleResponse simpleResponse) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getUrlChangePasswordCode(userId,code,password);
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")) {
simpleResponse.onSuccess("success");
}else{
simpleResponse.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception e ){
simpleResponse.onError(e);
}
}
});
}
}
Loading

0 comments on commit be1fa78

Please sign in to comment.