
com.yunpian.sdk.api.CallApi Maven / Gradle / Ivy
/**
*
*/
package com.yunpian.sdk.api;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import com.google.gson.reflect.TypeToken;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.constant.Code;
import com.yunpian.sdk.model.CallBill;
import com.yunpian.sdk.model.CallBind;
import com.yunpian.sdk.model.Result;
/**
* https://www.yunpian.com/api/anonymous.html
*
* @author dzh
* @date Nov 23, 2016 1:13:49 PM
* @since 1.2.0
*/
public class CallApi extends YunpianApi {
public static final String NAME = "call";
@Override
public String name() {
return NAME;
}
@Override
public void init(YunpianClient clnt) {
super.init(clnt);
host(clnt.getConf().getConf(YP_CALL_HOST, "https://call.yunpian.com"));
}
/**
* 绑定号码
*
*
* 参数名 类型 是否必须 描述 示例
*
*
* apikey String 是 用户唯一标识 9b11127a9701975c734b8aee81ee3526
*
*
* from String 是 需要绑定的号码 +8615012341234
*
*
* to String 是 需要绑定的号码 +8615011112222
*
*
* duration Intger 是 有效时长,单位:秒 600
*
*
* area_code String 否 区号,期望anonymous_number所属的地区 +8621(021)
*
*
* @param param
* @return
*/
public Result bind(Map param) {
Result r = new Result<>();
List list = param2pair(param, r, APIKEY, FROM, TO, DURATION);
if (r.getCode() != Code.OK)
return r;
String data = format2Form(list);
MapResultHandler h = new MapResultHandler() {
@Override
public CallBind data(Map rsp) {
switch (version()) {
case VERSION_V2:
return map2CallBind(rsp);
}
return null;
}
@Override
public Integer code(Map rsp) {
return YunpianApi.code(rsp, CallApi.this.version());
}
};
try {
return path("bind.json").post(uri(), data, h, r);
} catch (Exception e) {
return h.catchExceptoin(e, r);
}
}
/**
* 解绑号码
*
*
* 参数名 类型 是否必须 描述 示例
*
*
* apikey String 是 用户唯一标识 9b11127a9701975c734b8aee81ee3526
*
*
* from String 是 需要绑定的号码 +8615012341234
*
*
* to String 是 需要绑定的号码 +8615011112222
*
*
* duration Intger 是 延迟解绑的时间,单位:秒,0表示立即解除绑定 0
*
*
* @param param
* @return
*/
public Result unbind(Map param) {
Result r = new Result<>();
List list = param2pair(param, r, APIKEY, FROM, TO);
if (r.getCode() != Code.OK)
return r;
String data = format2Form(list);
MapResultHandler h = new MapResultHandler() {
@Override
public Void data(Map rsp) {
return null;
}
@Override
public Integer code(Map rsp) {
return YunpianApi.code(rsp, CallApi.this.version());
}
};
try {
return path("unbind.json").post(uri(), data, h, r);
} catch (Exception e) {
return h.catchExceptoin(e, r);
}
}
/**
* 话单获取
*
* 参数名 类型 是否必须 描述 示例
*
*
* apikey String 是 用户唯一标识 9b11127a9701975c734b8aee81ee3526
*
*
* page_size Integer 否 每页个数,最大100个,默认20个 20
*
*
* @param param
* @return
*/
public Result> pull(Map param) {
Result> r = new Result<>();
List list = param2pair(param, r, APIKEY);
if (r.getCode() != Code.OK)
return r;
String data = format2Form(list);
SimpleListResultHandler h = new SimpleListResultHandler() {
@Override
public List data(List rsp) {
switch (version()) {
case VERSION_V2:
return rsp;
}
return Collections.emptyList();
}
@Override
public Integer code(List rsp) {
if (rspMap != null) {
return YunpianApi.code(rspMap, CallApi.this.version());
}
return Code.OK;
}
@Override
Type rspType() {
return new TypeToken>() {
}.getType();
}
};
try {
return path("pull.json").post(uri(), data, h, r);
} catch (Exception e) {
return h.catchExceptoin(e, r);
}
}
protected CallBind map2CallBind(Map rsp) {
if (rsp == null)
return null;
CallBind bind = new CallBind();
bind.setMessage_id(rsp.get(MESSAGE_ID));
bind.setAnonymous_number(rsp.get(ANONYMOUS_NUMBER));
return bind;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy