-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyServerConfig.m
159 lines (129 loc) · 5.08 KB
/
CountlyServerConfig.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
// CountlyServerConfig.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyServerConfig ()
@property (nonatomic) BOOL trackingEnabled;
@property (nonatomic) BOOL networkingEnabled;
@end
NSString* const kCountlySCKeySC = @"sc";
@implementation CountlyServerConfig
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyServerConfig* 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.trackingEnabled = YES;
self.networkingEnabled = YES;
NSDictionary* serverConfigObject = [CountlyPersistency.sharedInstance retrieveServerConfig];
if (serverConfigObject) {
[self populateServerConfig:serverConfigObject];
}
}
return self;
}
- (BOOL)trackingEnabled
{
if (!CountlyCommon.sharedInstance.enableServerConfiguration)
return YES;
return _trackingEnabled;
}
- (BOOL)networkingEnabled
{
if (!CountlyCommon.sharedInstance.enableServerConfiguration)
return YES;
return _networkingEnabled;
}
- (void)populateServerConfig:(NSDictionary *)dictionary
{
if (dictionary[@"tracking"])
{
self.trackingEnabled = [dictionary[@"tracking"] boolValue];
}
if (dictionary[@"networking"])
{
self.networkingEnabled = [dictionary[@"networking"] boolValue];
}
CLY_LOG_D(@"tracking : %@", self.trackingEnabled ? @"YES" : @"NO");
CLY_LOG_D(@"networking : %@", self.networkingEnabled ? @"YES" : @"NO");
}
- (void)fetchServerConfig
{
CLY_LOG_D(@"Fetching server configs...");
if (!CountlyCommon.sharedInstance.enableServerConfiguration)
{
CLY_LOG_D(@"'fetchServerConfig' enable server configuration during init time configuration.");
return;
}
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary)
return;
NSURLSessionTask* task = [CountlyCommon.sharedInstance.URLSession dataTaskWithRequest:[self serverConfigRequest] completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSDictionary *serverConfigResponse = nil;
if (!error)
{
serverConfigResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
CLY_LOG_D(@"Server Config Fetched: %@", serverConfigResponse.description);
}
if (!error)
{
if (((NSHTTPURLResponse*)response).statusCode != 200)
{
NSMutableDictionary* serverConfig = serverConfigResponse.mutableCopy;
serverConfig[NSLocalizedDescriptionKey] = @"Server configuration general API error";
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorServerConfigGeneralAPIError userInfo:serverConfig];
}
}
if (error)
{
CLY_LOG_E(@"Error while fetching server configs: %@",error.description);
return;
}
NSDictionary* serverConfigObject = serverConfigResponse[@"c"];
if (serverConfigObject) {
[self populateServerConfig:serverConfigObject];
[CountlyPersistency.sharedInstance storeServerConfig:serverConfigObject];
}
}];
[task resume];
}
- (NSURLRequest *)serverConfigRequest
{
NSString* queryString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&%@=%@",
kCountlyQSKeyMethod, kCountlySCKeySC,
kCountlyQSKeyAppKey, CountlyConnectionManager.sharedInstance.appKey.cly_URLEscaped,
kCountlyQSKeyDeviceID, CountlyDeviceInfo.sharedInstance.deviceID.cly_URLEscaped,
kCountlyQSKeySDKName, CountlyCommon.sharedInstance.SDKName,
kCountlyQSKeySDKVersion, CountlyCommon.sharedInstance.SDKVersion];
queryString = [queryString stringByAppendingFormat:@"&%@=%@",
kCountlyAppVersionKey, CountlyDeviceInfo.appVersion];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSMutableString* URL = CountlyConnectionManager.sharedInstance.host.mutableCopy;
[URL appendString:kCountlyEndpointO];
[URL appendString:kCountlyEndpointSDK];
if (queryString.length > kCountlyGETRequestMaxLength || CountlyConnectionManager.sharedInstance.alwaysUsePOST)
{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString cly_dataUTF8];
return request.copy;
}
else
{
[URL appendFormat:@"?%@", queryString];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
return request;
}
CLY_LOG_D(@"serverConfigRequest URL :%@", URL);
}
@end