更新时间:2020-09-02 14:54
重要:自 2020 年 6 月 28 日起,mPaaS 停止维护 10.1.32 基线。请使用 10.1.68 或 10.1.60 系列基线。可以参考 mPaaS 10.1.68 升级指南 或 mPaaS 10.1.60 升级指南 进行基线版本升级。 |
RPC 相关模块为 APMobileNetwork.framework
、MPMgsAdapter
,推荐使用 MPMgsAdapter
中的接口。
本文引导您通过以下步骤使用移动网关 SDK:
调用以下方法初始化网关服务:
[MPRpcInterface initRpc];
10.1.32 版本之后不再需要添加 DTRpcInterface
类的 Category
文件,中间层会实现包装从 meta.config
中读取,升级版本后请检查工程中是否存在旧版本配置,如果有请移除。下面为新版本应移除的 DTRpcInterface
类的 Category
文件。
当 App 在移动网关控制台接入后台服务后,即可下载客户端的 RPC 代码。更多信息请参考 生成代码。
下载的 RPC 代码结构如下:
其中:
RPCDemoCloudpay_accountClient
为 RPC 配置。RPCDemoAuthLoginPostReq
为 request 模型。RPCDemoLoginResult
为 response 模型。RPC 请求必须在子线程调用,可使用中间层中 MPRpcInterface
封装的子线程调用接口,回调方法默认为主线程。示例代码如下:
- (void)sendRpc
{
__block RPCDemoLoginResult *result = nil;
[MPRpcInterface callAsyncBlock:^{
@try
{
RPCDemoLoginRequest *req = [[RPCDemoLoginRequest alloc] init];
req.loginId = @"alipayAdmin";
req.loginPassword = @"123456";
RPCDemoAuthLoginPostReq *loginPostReq = [[RPCDemoAuthLoginPostReq alloc] init];
loginPostReq._requestBody = req;
RPCDemoCloudpay_accountClient *service = [[RPCDemoCloudpay_accountClient alloc] init];
result = [service authLoginPost:loginPostReq];
}
@catch (NSException *exception) {
NSLog(@"%@", exception);
NSError *error = [userInfo objectForKey:@"kDTRpcErrorCauseError"]; // 获取异常详细信息
NSInteger code = error.code; // 获取异常详细信息错误码
}
} completion:^{
NSString *str = @"";
if (result && result.success) {
str = @"登录成功";
} else {
str = @"登录失败";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str message:nil delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
[alert show];
}];
}
try catch
捕获异常;当网关异常时会抛出,根据 结果码 查询原因。DTRpcMethod
为 RPC 请求方法描述,记录 RPC 请求的方法名、参数、返回类型等信息。
如果发送请求时,不需要加签,可以将 DTRpcMethod
的 signCheck
属性设置为 NO。
-(MPDemoUserInfo *) dataPostSetTimeout:(MPDemoPostPostReq *)requestParam
{
DTRpcMethod *method = [[DTRpcMethod alloc] init];
method.operationType = @"com.antcloud.request.post";
method.checkLogin = NO ;
method.signCheck = NO ;
method.returnType = @"@\"MPDemoUserInfo\"";
return [[DTRpcClient defaultClient] executeMethod:method params:@[ ]];
}
如果需要设置超时时间,可以配置 DTRpcMethod
的 timeoutInterval
属性。
-(MPDemoUserInfo *) dataPostSetTimeout:(MPDemoPostPostReq *)requestParam
{
DTRpcMethod *method = [[DTRpcMethod alloc] init];
method.operationType = @"com.antcloud.request.post";
method.checkLogin = NO ;
method.signCheck = YES ;
method.timeoutInterval = 1; // 这个超时时间是客户端收到网关返回的时间,服务端配置的超时时间是后端业务系统的返回时间;默认 20s,设置小于 1 时无效即为默认值
method.returnType = @"@\"MPDemoUserInfo\"";
return [[DTRpcClient defaultClient] executeMethod:method params:@[ ]];
}
如果需要为接口添加 Header,可以使用下面 DTRpcClient
的扩展方法。
-(MPDemoUserInfo *) dataPostAddHeader:(MPDemoPostPostReq *)requestParam
{
DTRpcMethod *method = [[DTRpcMethod alloc] init];
method.operationType = @"com.antcloud.request.postAddHeader";
method.checkLogin = NO ;
method.signCheck = YES ;
method.returnType = @"@\"MPDemoUserInfo\"";
// 针对接口添加 header
NSDictionary *customHeader = @{@"testKey": @"testValue"};
return [[DTRpcClient defaultClient] executeMethod:method params:@[ ] requestHeaderField:customHeader responseHeaderFields:nil];
}
checkLogin
属性为接口 session
校验使用,需要配合网关控制台完成,默认设置为 NO 即可。基于业务需求,可能需要在 RPC 发送前,或 RPC 处理完成后进行相关逻辑处理,RPC 模块提供拦截器机制处理此类需求。
创建拦截器,并实现 <DTRpcInterceptor>
协议的方法,用来处理 RPC 请求前后的相关操作。
@interface HXRpcInterceptor : NSObject<DTRpcInterceptor>
@end
@implementation HXRpcInterceptor
- (DTRpcOperation *)beforeRpcOperation:(DTRpcOperation *)operation{
// TODO
return operation;
}
- (DTRpcOperation *)afterRpcOperation:(DTRpcOperation *)operation{
// TODO
return operation;
}
@end
您可通过调用中间层的扩展接口,在拦截器容器中注册自定义的子拦截器。
HXRpcInterceptor *mpTestIntercaptor = [[HXRpcInterceptor alloc] init]; // 自定义子拦截器
[MPRpcInterface addRpcInterceptor:mpTestIntercaptor];
RPC 提供多种数据加密配置功能,详情参考 数据加密配置。
在文档使用中是否遇到以下问题
更多建议
匿名提交