Update XMLRPCHelper with success/error blocks management

This commit is contained in:
Brieuc Viel 2016-11-10 10:32:15 +01:00
parent 57e60268cb
commit 157baba4b9
4 changed files with 47 additions and 16 deletions

View file

@ -54,6 +54,7 @@
warnBeforeExpiryPeriod = [LinphoneManager.instance lpConfigIntForKey:@"warn_before_expiry_period" inSection:@"in_app_purchase"];
lastCheck = 0;
[XMLRPCHelper.self initArray];
//========// for test only
// int testExpiry = [LinphoneManager.instance lpConfigIntForKey:@"expiry_time_test"
// inSection:@"in_app_purchase"];

View file

@ -11,6 +11,8 @@
* All the rest is implemented to do nothing
*/
@property(nonatomic, strong) NSMutableArray *personsArray;
+ (void)sendXMLRPCRequest:(NSString *)method;
+ (void)sendXMLRPCRequestWithParams:(NSString *)method withParams:(NSArray *)params;
+ (void)sendXMLRPCRequestWithParams:(NSString *)method
@ -23,5 +25,6 @@
- (void)dealWithXmlRpcResponse:(LinphoneXmlRpcRequest *)request;
- (void)displayErrorPopup:(NSString *)error;
+ (void)initArray;
@end

View file

@ -13,17 +13,31 @@
#import "Utils.h"
#import "PhoneMainView.h"
/* This subclass allows use to store the block to execute on success */
@interface XMLRPCRequestObject : NSObject
@property(copy, nonatomic) void (^XMLRPCHelperBlockSuccess)(NSString *something);
@property(copy, nonatomic) void (^XMLRPCHelperBlockError)(NSString *something);
@property LinphoneXmlRpcRequest *xmlRpcRequest;
@end
@implementation XMLRPCRequestObject
@end
@implementation XMLRPCHelper
#pragma mark - API
typedef void (^XMLRPCHelperBlock)(NSString *something);
// typedef void (^XMLRPCHelperBlock)(NSString *something);
XMLRPCHelperBlock successBlock = nil;
XMLRPCHelperBlock errorBlock = nil;
// XMLRPCHelperBlock successBlock = nil;
// XMLRPCHelperBlock errorBlock = nil;
NSMutableArray *personsArray;
/*****************************************************************************************/
+ (void)initArray {
personsArray = [[NSMutableArray alloc] init];
}
+ (void)sendXMLRPCRequest:(NSString *)method {
[self sendXMLRPCRequestWithParams:method withParams:nil onSuccess:nil onError:nil];
@ -46,33 +60,36 @@ XMLRPCHelperBlock errorBlock = nil;
onSuccess:(void (^)(NSString *))successBk
onError:(void (^)(NSString *req))errorBk {
LOGI(@"XMLRPC %@ - %@", method, params);
XMLRPCRequestObject *requestObject = [XMLRPCRequestObject alloc];
const char *URL =
[LinphoneManager.instance lpConfigStringForKey:@"receipt_validation_url" inSection:@"in_app_purchase"]
.UTF8String;
successBlock = successBk;
errorBlock = errorBk;
requestObject.XMLRPCHelperBlockSuccess = successBk;
requestObject.XMLRPCHelperBlockError = errorBk;
// Create LinphoneXMLRPCRequest
LinphoneXmlRpcSession *requestSession = linphone_xml_rpc_session_new(LC, URL);
LinphoneXmlRpcRequest *request = linphone_xml_rpc_request_new(method.UTF8String, LinphoneXmlRpcArgString);
// LinphoneXmlRpcRequest *request = linphone_xml_rpc_request_new(method.UTF8String, LinphoneXmlRpcArgString);
requestObject.xmlRpcRequest = linphone_xml_rpc_request_new(method.UTF8String, LinphoneXmlRpcArgString);
[personsArray addObject:requestObject];
// Set argument to this LinphoneXMLRPCRequest
for (NSString *item in params) {
NSLog(@"Linphone XMLRPC Request with argument: %@", item);
linphone_xml_rpc_request_add_string_arg(request, item.UTF8String);
linphone_xml_rpc_request_add_string_arg(requestObject.xmlRpcRequest, item.UTF8String);
}
// Ref and send the LinphoneXMLRPCRequest
requestSession = linphone_xml_rpc_session_ref(requestSession);
linphone_xml_rpc_session_send_request(requestSession, request);
linphone_xml_rpc_session_send_request(requestSession, requestObject.xmlRpcRequest);
// Set the callbacks to this LinphoneXMLRPCRequest
LinphoneXmlRpcRequestCbs *cbs = linphone_xml_rpc_request_get_callbacks(request);
LinphoneXmlRpcRequestCbs *cbs = linphone_xml_rpc_request_get_callbacks(requestObject.xmlRpcRequest);
// Register XMLRPCHelper in user data to get it back on Callback rised
XMLRPCHelper *xMLRPCHelper = [[XMLRPCHelper alloc] init];
linphone_xml_rpc_request_set_user_data(request, ((void *)CFBridgingRetain(xMLRPCHelper)));
linphone_xml_rpc_request_set_user_data(requestObject.xmlRpcRequest, ((void *)CFBridgingRetain(xMLRPCHelper)));
// Set the response Callback
linphone_xml_rpc_request_cbs_set_response(cbs, linphone_xmlrpc_call_back_received);
@ -83,21 +100,31 @@ static void linphone_xmlrpc_call_back_received(LinphoneXmlRpcRequest *request) {
}
- (void)dealWithXmlRpcResponse:(LinphoneXmlRpcRequest *)request {
XMLRPCRequestObject *xmlrpcObject;
NSInteger index = 0;
for (int i = 0; i < [personsArray count]; i++) {
xmlrpcObject = [personsArray objectAtIndex:i];
if (xmlrpcObject.xmlRpcRequest == request)
break;
index++;
}
NSString *responseString =
[NSString stringWithFormat:@"%s", (linphone_xml_rpc_request_get_string_response(request))];
LOGI(@"XMLRPC query: %@", responseString);
if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusOk) {
// Call success block
successBlock(responseString);
xmlrpcObject.XMLRPCHelperBlockSuccess(responseString);
} else if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusFailed) {
if (errorBlock != nil) {
if (xmlrpcObject.XMLRPCHelperBlockError != nil) {
LOGI(@"XMLRPC query ErrorBlock rised");
errorBlock(responseString);
xmlrpcObject.XMLRPCHelperBlockError(responseString);
}
// Display Error alert
[self displayErrorPopup:@"LinphoneXMLRPC Request Failed"];
}
linphone_xml_rpc_request_unref(request);
[personsArray removeObjectAtIndex:index];
}
#pragma mark - Error alerts

View file

@ -36,7 +36,7 @@
<key>AutocorrectionType</key>
<string>No</string>
<key>DefaultValue</key>
<string></string>
<true/>
<key>IASKTextAlignment</key>
<string>IASKUITextAlignmentRight</string>
</dict>
@ -48,7 +48,7 @@
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>DefaultValue</key>
<false/>
<true/>
</dict>
<dict>
<key>Title</key>
@ -58,7 +58,7 @@
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>DefaultValue</key>
<false/>
<true/>
</dict>
<dict>
<key>Key</key>