All Products
Search
Document Center

Use SDK

Last Updated: Mar 24, 2021
Important: Since June 28, 2020, mPaaS has stopped support for the baseline 10.1.32. Please use 10.1.68 or 10.1.60 instead. For how to upgrade the baseline from version 10.1.32 to 10.1.68 or 10.1.60, see mPaaS 10.1.68 upgrade guide or mPaaS 10.1.60 upgrade guide.

The RPC related modules include APMobileNetwork.framework and MPMgsAdapter. APIs in the MPMgsAdapter module are recommended.

This topic describes how to use the mobile gateway SDK.

  1. Initialize gateway services
  2. Generate the RPC code
  3. Send a request
  4. Customize a request
  5. Customize an RPC interceptor
  6. Encrypt data

Initialize gateway services

Call the following method to initialize gateway services:

 
  1. [MPRpcInterface initRpc];

Upgrade precautions

The Category file of the DTRpcInterface class does not need to be added since version 10.1.32. The middle tier implements package reading from meta.config. After an upgrade, check whether there is any configuration of the earlier version in the project. If yes, remove it. The following figure shows the Category file of the DTRpcInterface class to be removed from an upgraded version.

gateway

Generate the RPC code

When the app accesses the backend service in the mobile gateway console, you can download the RPC code for the client. For more information, see Generate code.

The following figure shows the structure of the downloaded RPC code.

code structure

In the code:

  • RPCDemoCloudpay_accountClient: RPC configuration.
  • RPCDemoAuthLoginPostReq: request model.
  • RPCDemoLoginResult: response model.

Send a request

RPC requests must be called in subthreads. The subthread encapsulated with MPRpcInterface at the middle tier can be used to call the API. The main thread is used as the callback method by default. The code sample is as follows:

 
  1. - (void)sendRpc
  2. {
  3. __block RPCDemoLoginResult *result = nil;
  4. [MPRpcInterface callAsyncBlock:^{
  5. @try
  6. {
  7. RPCDemoLoginRequest *req = [[RPCDemoLoginRequest alloc] init];
  8. req.loginId = @"alipayAdmin";
  9. req.loginPassword = @"123456";
  10. RPCDemoAuthLoginPostReq *loginPostReq = [[RPCDemoAuthLoginPostReq alloc] init];
  11. loginPostReq._requestBody = req;
  12. RPCDemoCloudpay_accountClient *service = [[RPCDemoCloudpay_accountClient alloc] init];
  13. result = [service authLoginPost:loginPostReq];
  14. }
  15. @catch (NSException *exception) {
  16. NSLog(@"%@", exception);
  17. NSError *error = [userInfo objectForKey:@"kDTRpcErrorCauseError"]; // Obtain the detailed information of the exception
  18. NSInteger code = error.code; // Obtain the error code of the exception
  19. }
  20. } completion:^{
  21. NSString *str = @"";
  22. if (result && result.success) {
  23. str = @"Successful login";
  24. } else {
  25. str = @"Login failure";
  26. }
  27. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str message:nil delegate:nil
  28. cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
  29. [alert show];
  30. }];
  31. }
Note: Use try catch to catch exceptions. When the gateway is abnormal, an exception is generated. In this case, check the cause of the exception based on the [result code] ( 64260).

Customize a request

DTRpcMethod describes RPC request methods. It records the method name, arguments, and return types of RPC requests.

  • If no signature is required for request sending, set the signCheck attribute of DTRpcMethod to NO.
 
  1. -(MPDemoUserInfo *) dataPostSetTimeout:(MPDemoPostPostReq *)requestParam
  2. {
  3. DTRpcMethod *method = [[DTRpcMethod alloc] init];
  4. method.operationType = @"com.antcloud.request.post";
  5. method.checkLogin = NO ;
  6. method.signCheck = NO ;
  7. method.returnType = @"@\"MPDemoUserInfo\"";
  8. return [[DTRpcClient defaultClient] executeMethod:method params:@[ ]];
  9. }
  • To set a timeout interval, set the timeoutInterval attribute of DTRpcMethod.
 
  1. -(MPDemoUserInfo *) dataPostSetTimeout:(MPDemoPostPostReq *)requestParam
  2. {
  3. DTRpcMethod *method = [[DTRpcMethod alloc] init];
  4. method.operationType = @"com.antcloud.request.post";
  5. method.checkLogin = NO ;
  6. method.signCheck = YES ;
  7. method.timeoutInterval = 1; //The timeout interval defines the period in which the client receives a response from the gateway. The timeout interval configured on the server defines the period in which the backend business system delivers a response. The default value is 20, in seconds. A value smaller than 1 is invalid, in which case the default value is used.
  8. method.returnType = @"@\"MPDemoUserInfo\"";
  9. return [[DTRpcClient defaultClient] executeMethod:method params:@[ ]];
  10. }
  • To add a header to an API, use the following extension method of DTRpcClient:
 
  1. -(MPDemoUserInfo *) dataPostAddHeader:(MPDemoPostPostReq *)requestParam
  2. {
  3. DTRpcMethod *method = [[DTRpcMethod alloc] init];
  4. method.operationType = @"com.antcloud.request.postAddHeader";
  5. method.checkLogin = NO ;
  6. method.signCheck = YES ;
  7. method.returnType = @"@\"MPDemoUserInfo\"";
  8. // Add a header to the API.
  9. NSDictionary *customHeader = @{@"testKey": @"testValue"};
  10. return [[DTRpcClient defaultClient] executeMethod:method params:@[ ] requestHeaderField:customHeader responseHeaderFields:nil];
  11. }
  • To add a header to all APIs, you can use the interceptor. For the specific implementation method, please refer to the mobile gateway code example.
  • checkLogin attribute is used to verify the API session and must work with the gateway console. The default value NO is recommended.

Customize an RPC interceptor

Based on your business requirements, logical processing may be required before an RPC request is sent or after RPC processing. The RPC module provides an interceptor mechanism to meet such requirements.

Customize an interceptor

Create an interceptor and implement the <DTRpcInterceptor> protocol method to handle related operations before RPC requests are sent or after they are processed.

 
  1. @interface HXRpcInterceptor : NSObject<DTRpcInterceptor>
  2. @end
  3. @implementation HXRpcInterceptor
  4. - (DTRpcOperation *)beforeRpcOperation:(DTRpcOperation *)operation{
  5. // TODO
  6. return operation;
  7. }
  8. - (DTRpcOperation *)afterRpcOperation:(DTRpcOperation *)operation{
  9. // TODO
  10. return operation;
  11. }
  12. @end

Register the interceptor

Call the extension API at the middle tier to register the customized child interceptor in the interceptor container.

 
  1. HXRpcInterceptor *mpTestIntercaptor = [[HXRpcInterceptor alloc] init]; // Customized child interceptor
  2. [MPRpcInterface addRpcInterceptor:mpTestIntercaptor];

Encrypt data

The RPC provides a variety of data encryption configurations. For more information, see Encrypt data.