9
9
NSString * const kCountlyRCKeyFetchRemoteConfig = @" fetch_remote_config" ;
10
10
NSString * const kCountlyRCKeyFetchVariant = @" ab_fetch_variants" ;
11
11
NSString * const kCountlyRCKeyEnrollVariant = @" ab_enroll_variant" ;
12
+ NSString * const kCountlyRCKeyFetchExperiments = @" ab_fetch_experiments" ;
12
13
NSString * const kCountlyRCKeyVariant = @" variant" ;
13
14
NSString * const kCountlyRCKeyKey = @" key" ;
14
15
NSString * const kCountlyRCKeyKeys = @" keys" ;
27
28
@interface CountlyRemoteConfigInternal ()
28
29
@property (nonatomic ) NSDictionary * localCachedVariants;
29
30
@property (nonatomic ) NSDictionary <NSString *, CountlyRCData *>* cachedRemoteConfig;
31
+ @property (nonatomic ) NSDictionary <NSString*, CountlyExperimentInformation*> * localCachedExperiments;
30
32
@end
31
33
32
34
@implementation CountlyRemoteConfigInternal
@@ -49,6 +51,8 @@ - (instancetype)init
49
51
self.cachedRemoteConfig = [CountlyPersistency.sharedInstance retrieveRemoteConfig ] ;
50
52
51
53
self.remoteConfigGlobalCallbacks = [[NSMutableArray alloc ] init ];
54
+
55
+ self.localCachedExperiments = NSMutableDictionary .new ;
52
56
}
53
57
54
58
return self;
@@ -448,6 +452,7 @@ - (void)testingDownloadAllVariants:(RCVariantCallback)completionHandler
448
452
}];
449
453
}
450
454
455
+
451
456
- (void )testingDownloadAllVariantsInternal : (void (^)(CLYRequestResult response, NSDictionary * variants, NSError * error))completionHandler
452
457
{
453
458
if (!CountlyServerConfig.sharedInstance .networkingEnabled )
@@ -649,6 +654,139 @@ - (NSURLRequest *)downloadVariantsRequest
649
654
}
650
655
}
651
656
657
+ - (void ) testingDownloadExperimentInformation : (RCVariantCallback)completionHandler
658
+ {
659
+ if (!CountlyConsentManager.sharedInstance .consentForRemoteConfig )
660
+ {
661
+ CLY_LOG_D (@" 'testingDownloadExperimentInformation' is aborted: RemoteConfig consent requires" );
662
+ return ;
663
+ }
664
+ if (CountlyDeviceInfo.sharedInstance .isDeviceIDTemporary )
665
+ {
666
+ CLY_LOG_D (@" 'testingDownloadExperimentInformation' is aborted: Due to temporary device id" );
667
+ return ;
668
+ }
669
+
670
+ CLY_LOG_D (@" Download experiments info..." );
671
+
672
+ [self testingDownloaExperimentInfoInternal: ^(CLYRequestResult response, NSDictionary *experimentInfo,NSError *error)
673
+ {
674
+ if (!error)
675
+ {
676
+ self.localCachedExperiments = experimentInfo;
677
+ CLY_LOG_D (@" Download experiments info is successful. \n %@ " , experimentInfo);
678
+
679
+ }
680
+ else
681
+ {
682
+ CLY_LOG_W (@" Download experiments info failed: %@ " , error);
683
+ }
684
+
685
+ if (completionHandler)
686
+ completionHandler (response, error);
687
+ }];
688
+ }
689
+
690
+
691
+ - (void )testingDownloaExperimentInfoInternal : (void (^)(CLYRequestResult response, NSDictionary * experimentsInfo, NSError * error))completionHandler
692
+ {
693
+ if (!CountlyServerConfig.sharedInstance .networkingEnabled )
694
+ {
695
+ CLY_LOG_D (@" 'testingDownloaExperimentInfoInternal' is aborted: SDK Networking is disabled from server config!" );
696
+ return ;
697
+ }
698
+ if (!completionHandler)
699
+ return ;
700
+
701
+ NSURLRequest * request = [self downloadExperimentInfoRequest ];
702
+ NSURLSessionTask * task = [NSURLSession .sharedSession dataTaskWithRequest: request completionHandler: ^(NSData * data, NSURLResponse * response, NSError * error)
703
+ {
704
+
705
+ NSMutableDictionary <NSString *, CountlyExperimentInformation*> * experiments = NSMutableDictionary .new ;
706
+
707
+ if (!error)
708
+ {
709
+
710
+ NSArray * experimentsInfo = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];
711
+ [experimentsInfo enumerateObjectsUsingBlock: ^(NSDictionary * value, NSUInteger idx, BOOL * stop)
712
+ {
713
+ CountlyExperimentInformation* experimentInfo = [[CountlyExperimentInformation alloc ] initWithID: value[@" id" ] experimentName: value[@" name" ] experimentDescription: value[@" description" ] currentVariant: value[@" currentVariant" ] variants: value[@" variants" ]];
714
+ experiments[experimentInfo.experimentID] = experimentInfo;
715
+
716
+ }];
717
+ }
718
+
719
+ if (!error)
720
+ {
721
+ if (((NSHTTPURLResponse *)response).statusCode != 200 )
722
+ {
723
+ NSMutableDictionary * userInfo = experiments.mutableCopy ;
724
+ userInfo[NSLocalizedDescriptionKey ] = @" Fetch variants general API error" ;
725
+ error = [NSError errorWithDomain: kCountlyErrorDomain code: CLYErrorRemoteConfigGeneralAPIError userInfo: userInfo];
726
+ }
727
+ }
728
+
729
+ if (error)
730
+ {
731
+ CLY_LOG_D (@" Download experiments Request <%p > failed!\n Error: %@ " , request, error);
732
+
733
+ dispatch_async (dispatch_get_main_queue (), ^
734
+ {
735
+ completionHandler (CLYResponseError, nil , error);
736
+ });
737
+
738
+ return ;
739
+ }
740
+
741
+ CLY_LOG_D (@" Download experiments Request <%p > successfully completed." , request);
742
+
743
+ dispatch_async (dispatch_get_main_queue (), ^
744
+ {
745
+ completionHandler (CLYResponseSuccess, experiments, nil );
746
+ });
747
+ }];
748
+
749
+ [task resume ];
750
+
751
+ CLY_LOG_D (@" Download experiments Request <%p > started:\n [%@ ] %@ " , (id )request, request.HTTPMethod , request.URL .absoluteString );
752
+ }
753
+
754
+ - (NSURLRequest *)downloadExperimentInfoRequest
755
+ {
756
+ NSString * queryString = [CountlyConnectionManager.sharedInstance queryEssentials ];
757
+
758
+ queryString = [queryString stringByAppendingFormat: @" &%@ =%@ " , kCountlyQSKeyMethod , kCountlyRCKeyFetchExperiments ];
759
+
760
+ if (CountlyConsentManager.sharedInstance .consentForSessions )
761
+ {
762
+ queryString = [queryString stringByAppendingFormat: @" &%@ =%@ " , kCountlyQSKeyMetrics , [CountlyDeviceInfo metrics ]];
763
+ }
764
+
765
+ queryString = [CountlyConnectionManager.sharedInstance appendChecksum: queryString];
766
+
767
+ NSString * serverOutputSDKEndpoint = [CountlyConnectionManager.sharedInstance.host stringByAppendingFormat: @" %@%@ " ,
768
+ kCountlyEndpointO ,
769
+ kCountlyEndpointSDK ];
770
+
771
+ if (CountlyConnectionManager.sharedInstance .alwaysUsePOST )
772
+ {
773
+ NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: serverOutputSDKEndpoint]];
774
+ request.HTTPMethod = @" POST" ;
775
+ request.HTTPBody = [queryString cly_dataUTF8 ];
776
+ return request.copy ;
777
+ }
778
+ else
779
+ {
780
+ NSString * withQueryString = [serverOutputSDKEndpoint stringByAppendingFormat: @" ?%@ " , queryString];
781
+ NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: withQueryString]];
782
+ return request;
783
+ }
784
+ }
785
+ - (NSDictionary <NSString*, CountlyExperimentInformation*> *) testingGetAllExperimentInfo
786
+ {
787
+ return self.localCachedExperiments ;
788
+ }
789
+
652
790
- (NSURLRequest *)enrollInVarianRequestForKey : (NSString *)key variantName : (NSString *)variantName
653
791
{
654
792
NSString * queryString = [CountlyConnectionManager.sharedInstance queryEssentials ];
0 commit comments