API通道SDK
API通道SDK提供IoT业务协议封装的HTTPS请求能力,并通过整合安全组件来提升通道的安全性。
初始化
使用方式
API调用示例如下。
#import <IMSApiClient/IMSApiClient.h>
// 构建请求
NSDictionary *params = @{@"input":@"测试"};
IMSIoTRequestBuilder *builder = [[IMSIoTRequestBuilder alloc] initWithPath:@"/kit/debug/ping"
apiVersion:@"1.0.0"
params:params];
// 可选参数
// [builder setHost:@"xxx"];//指定API host
// [builder setScheme:@"https"];
//通过 IMSRequestClient 发送请求
[IMSRequestClient asyncSendRequest:builder.build responseHandler:^(NSError * _Nullable error, IMSResponse * _Nullable response) {
if (error) {
//处理Error,非服务端返回的错误都通过该Error回调
}
else {
if (response.code == 200) {
//成功,处理response.data
}
else {
//处理服务端错误,可通过response.localizedMsg展示错误Toast
}
}
}];
设置超时时长
指定API请求的超时时长。
// 设置全局超时时长,对所有API请求都生效,不设置默认10s
[IMSConfiguration sharedInstance].timeoutInterval = 10;
// 针对局部请求超时时长设置
NSDictionary *params = @{@"input":@"测试"};
IMSIoTRequestBuilder *builder = [[IMSIoTRequestBuilder alloc] initWithPath:@"/kit/debug/ping"
apiVersion:@"1.0.0"
params:params];
// 可选参数,不设置默认使用全局配置超时时长
builder.timeoutInterval = 10;
//通过 IMSRequestClient 发送请求
[IMSRequestClient asyncSendRequest:builder.build responseHandler:^(NSError * _Nullable error, IMSResponse * _Nullable response) {
if (error) {
//处理Error,非服务端返回的错误都通过该Error回调
}
else {
if (response.code == 200) {
//成功,处理response.data
}
else {
//处理服务端错误,可通过response.localizedMsg展示错误Toast
}
}
}];