forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyRemoteConfig.m
217 lines (172 loc) · 6.84 KB
/
CountlyRemoteConfig.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// CountlyLocationManager.h
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
NSString* const kCountlyRCKeyFetchRemoteConfig = @"fetch_remote_config";
NSString* const kCountlyRCKeyKeys = @"keys";
NSString* const kCountlyRCKeyOmitKeys = @"omit_keys";
@interface CountlyRemoteConfig ()
@property (nonatomic) NSDictionary* cachedRemoteConfig;
@end
@implementation CountlyRemoteConfig
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyRemoteConfig* s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
self.cachedRemoteConfig = [CountlyPersistency.sharedInstance retrieveRemoteConfig];
}
return self;
}
#pragma mark ---
- (void)startRemoteConfig
{
if (!self.isEnabledOnInitialConfig)
return;
if (!CountlyConsentManager.sharedInstance.consentForRemoteConfig)
return;
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary)
return;
COUNTLY_LOG(@"Fetching remote config on start...");
[self fetchRemoteConfigForKeys:nil omitKeys:nil completionHandler:^(NSDictionary *remoteConfig, NSError *error)
{
if (!error)
{
COUNTLY_LOG(@"Fetching remote config on start is successful. \n%@", remoteConfig);
self.cachedRemoteConfig = remoteConfig;
[CountlyPersistency.sharedInstance storeRemoteConfig:self.cachedRemoteConfig];
}
else
{
COUNTLY_LOG(@"Fetching remote config on start failed: %@", error);
}
if (self.remoteConfigCompletionHandler)
self.remoteConfigCompletionHandler(error);
}];
}
- (void)updateRemoteConfigForKeys:(NSArray *)keys omitKeys:(NSArray *)omitKeys completionHandler:(void (^)(NSError * error))completionHandler
{
if (!CountlyConsentManager.sharedInstance.consentForRemoteConfig)
return;
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary)
return;
COUNTLY_LOG(@"Fetching remote config manually...");
[self fetchRemoteConfigForKeys:keys omitKeys:omitKeys completionHandler:^(NSDictionary *remoteConfig, NSError *error)
{
if (!error)
{
COUNTLY_LOG(@"Fetching remote config manually is successful. \n%@", remoteConfig);
if (!keys && !omitKeys)
{
self.cachedRemoteConfig = remoteConfig;
}
else
{
NSMutableDictionary* partiallyUpdatedRemoteConfig = self.cachedRemoteConfig.mutableCopy;
[partiallyUpdatedRemoteConfig addEntriesFromDictionary:remoteConfig];
self.cachedRemoteConfig = [NSDictionary dictionaryWithDictionary:partiallyUpdatedRemoteConfig];
}
[CountlyPersistency.sharedInstance storeRemoteConfig:self.cachedRemoteConfig];
}
else
{
COUNTLY_LOG(@"Fetching remote config manually failed: %@", error);
}
if (completionHandler)
completionHandler(error);
}];
}
- (id)remoteConfigValueForKey:(NSString *)key
{
return self.cachedRemoteConfig[key];
}
- (void)clearCachedRemoteConfig
{
self.cachedRemoteConfig = nil;
[CountlyPersistency.sharedInstance storeRemoteConfig:self.cachedRemoteConfig];
}
#pragma mark ---
- (void)fetchRemoteConfigForKeys:(NSArray *)keys omitKeys:(NSArray *)omitKeys completionHandler:(void (^)(NSDictionary* remoteConfig, NSError * error))completionHandler
{
if (!completionHandler)
return;
NSURLRequest* request = [self remoteConfigRequestForKeys:keys omitKeys:omitKeys];
NSURLSessionTask* task = [NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSDictionary* remoteConfig = nil;
if (!error)
{
remoteConfig = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
if (!error)
{
if (((NSHTTPURLResponse*)response).statusCode != 200)
{
NSMutableDictionary* userInfo = remoteConfig.mutableCopy;
userInfo[NSLocalizedDescriptionKey] = @"Remote config general API error";
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorRemoteConfigGeneralAPIError userInfo:userInfo];
}
}
if (error)
{
COUNTLY_LOG(@"Remote Config Request <%p> failed!\nError: %@", request, error);
dispatch_async(dispatch_get_main_queue(), ^
{
completionHandler(nil, error);
});
return;
}
COUNTLY_LOG(@"Remote Config Request <%p> successfully completed.", request);
dispatch_async(dispatch_get_main_queue(), ^
{
completionHandler(remoteConfig, nil);
});
}];
[task resume];
COUNTLY_LOG(@"Remote Config Request <%p> started:\n[%@] %@", (id)request, request.HTTPMethod, request.URL.absoluteString);
}
- (NSURLRequest *)remoteConfigRequestForKeys:(NSArray *)keys omitKeys:(NSArray *)omitKeys
{
NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials];
queryString = [queryString stringByAppendingFormat:@"&%@=%@", kCountlyQSKeyMethod, kCountlyRCKeyFetchRemoteConfig];
if (keys)
{
queryString = [queryString stringByAppendingFormat:@"&%@=%@", kCountlyRCKeyKeys, [keys cly_JSONify]];
}
else if (omitKeys)
{
queryString = [queryString stringByAppendingFormat:@"&%@=%@", kCountlyRCKeyOmitKeys, [omitKeys cly_JSONify]];
}
if (CountlyConsentManager.sharedInstance.consentForSessions)
{
queryString = [queryString stringByAppendingFormat:@"&%@=%@", kCountlyQSKeyMetrics, [CountlyDeviceInfo metrics]];
}
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSString* serverOutputSDKEndpoint = [CountlyConnectionManager.sharedInstance.host stringByAppendingFormat:@"%@%@",
kCountlyEndpointO,
kCountlyEndpointSDK];
if (CountlyConnectionManager.sharedInstance.alwaysUsePOST)
{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverOutputSDKEndpoint]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString cly_dataUTF8];
return request.copy;
}
else
{
NSString* withQueryString = [serverOutputSDKEndpoint stringByAppendingFormat:@"?%@", queryString];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:withQueryString]];
return request;
}
}
@end