-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMPIAdobe.m
More file actions
188 lines (143 loc) · 8.22 KB
/
MPIAdobe.m
File metadata and controls
188 lines (143 loc) · 8.22 KB
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
// Be sure to apply changes to both copies of this file in the repository
#import "MPIAdobe.h"
#if defined(__has_include) && __has_include(<mParticle_Apple_SDK/mParticle.h>)
#import <mParticle_Apple_SDK/mParticle.h>
#import <mParticle_Apple_SDK/mParticle_Apple_SDK-Swift.h>
#elif defined(__has_include) && __has_include(<mParticle_Apple_SDK_NoLocation/mParticle.h>)
#import <mParticle_Apple_SDK_NoLocation/mParticle.h>
#import <mParticle_Apple_SDK_NoLocation/mParticle_Apple_SDK-Swift.h>
#else
#import "mParticle.h"
#import "mParticle_Apple_SDK-Swift.h"
#endif
NSString *const MPIAdobeErrorKey = @"MPIAdobeErrorKey";
static NSString * host = @"dpm.demdex.net";
static NSString *const protocol = @"https";
static NSString *const path = @"/id?";
static NSString *const marketingCloudIdKey = @"d_mid";
static NSString *const organizationIdKey = @"d_orgid";
static NSString *const deviceIdKey = @"d_cid";
static NSString *const userIdentityKey = @"d_cid_ic";
static NSString *const regionKey = @"dcs_region";
static NSString *const blobKey = @"d_blob";
static NSString *const platformKey = @"d_ptfm";
static NSString *const versionKey = @"d_ver";
static NSString *const platform = @"ios";
static NSString *const version = @"2";
static NSString *const advertiserIdDeviceKey = @"20915";
static NSString *const pushTokenDeviceKey = @"20920";
static NSString *const idSuffix = @"%01";
static NSString *const errorResponseKey = @"error_msg";
static NSString *const errorDomain = @"mParticle-Adobe";
static NSString *const serverErrorDomain = @"mParticle-Adobe Server Response";
static NSString *const marketingCloudIdUserDefaultsKey = @"ADBMOBILE_PERSISTED_MID";
@interface MPIAdobeError ()
- (id)initWithCode:(MPIAdobeErrorCode)code message:(NSString *)message error:(NSError *)error;
@end
@implementation MPIAdobeError
- (id)initWithCode:(MPIAdobeErrorCode)code message:(NSString *)message error:(NSError *)error {
self = [super init];
if (self) {
_code = code;
_message = message;
_innerError = error;
}
return self;
}
- (NSString *)description {
NSMutableString *description = [[NSMutableString alloc] initWithString:@"MPIAdobeError {\n"];
[description appendFormat:@" code: %@\n", @(_code)];
[description appendFormat:@" message: %@\n", _message];
[description appendFormat:@" inner error: %@\n", _innerError];
[description appendString:@"}"];
return description;
}
@end
@interface MPIAdobe ()
@property (nonatomic, copy) NSString *region;
@property (nonatomic, copy) NSString *blob;
@end
@implementation MPIAdobe
- (void)sendRequestWithMarketingCloudId:(NSString *)marketingCloudId advertiserId:(NSString *)advertiserId pushToken:(NSString *)pushToken organizationId:(NSString *)organizationId userIdentities:(NSDictionary<NSNumber *, NSString *> *)userIdentities audienceManagerServer:(NSString *)audienceManagerServer completion:(void (^)(NSString *marketingCloudId, NSString *locationHint, NSString *blob, NSError *))completion {
if (audienceManagerServer != nil && audienceManagerServer.length > 0) {
host = audienceManagerServer;
}
NSDictionary *userIdentityMappings = @{
@(MPUserIdentityOther): @"other",
@(MPUserIdentityCustomerId): @"customerid",
@(MPUserIdentityFacebook): @"facebook",
@(MPUserIdentityTwitter): @"twitter",
@(MPUserIdentityGoogle): @"google",
@(MPUserIdentityMicrosoft): @"microsoft",
@(MPUserIdentityYahoo): @"yahoo",
@(MPUserIdentityEmail): @"email",
@(MPUserIdentityAlias): @"alias",
@(MPUserIdentityFacebookCustomAudienceId): @"facebookcustomaudienceid"
};
NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@://%@%@", protocol, host, path];
NSURLComponents *components = [NSURLComponents componentsWithString:urlString];
NSMutableArray *queryItems = [NSMutableArray array];
if (marketingCloudId) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:marketingCloudIdKey value:marketingCloudId]];
}
if (advertiserId) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:deviceIdKey value:[NSString stringWithFormat:@"%@%@%@", advertiserIdDeviceKey, idSuffix, advertiserId]]];
}
if (pushToken) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:deviceIdKey value:[NSString stringWithFormat:@"%@%@%@", pushTokenDeviceKey, idSuffix, pushToken]]];
}
if (userIdentities) {
[userIdentities enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSString *mappedKey = userIdentityMappings[key];
if (mappedKey.length) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:userIdentityKey value:[NSString stringWithFormat:@"%@%@%@", mappedKey, idSuffix, obj]]];
}
}];
}
if (self.blob) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:blobKey value:self.blob]];
}
if (self.region) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:regionKey value:self.region]];
}
[queryItems addObject:[NSURLQueryItem queryItemWithName:organizationIdKey value:organizationId]];
[queryItems addObject:[NSURLQueryItem queryItemWithName:platformKey value:platform]];
[queryItems addObject:[NSURLQueryItem queryItemWithName:versionKey value:version]];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
__weak MPIAdobe *weakSelf = self;
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
void (^callbackWithCode)(MPIAdobeErrorCode code, NSString *message, NSError *error) = ^void(MPIAdobeErrorCode code, NSString *message, NSError *error) {
MPIAdobeError *adobeError = [[MPIAdobeError alloc] initWithCode:code message:message error:error];
NSError *compositeError = [NSError errorWithDomain:errorDomain code:adobeError.code userInfo:@{MPIAdobeErrorKey:adobeError}];
completion(nil, nil, nil, compositeError);
};
if (error) {
return callbackWithCode(MPIAdobeErrorCodeClientFailedRequestError, @"Request failed", error);
}
NSDictionary *dictionary = nil;
@try {
dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
} @catch (NSException *exception) {
return callbackWithCode(MPIAdobeErrorCodeClientSerializationError, @"Deserializing the response failed", nil);
}
NSDictionary *errorDictionary = dictionary[errorResponseKey];
if (errorDictionary) {
NSError *error = [NSError errorWithDomain:serverErrorDomain code:0 userInfo:errorDictionary];
return callbackWithCode(MPIAdobeErrorCodeServerError, @"Server returned an error", error);
}
NSString *marketingCloudId = [dictionary[marketingCloudIdKey] isKindOfClass:[NSString class]] ? dictionary[marketingCloudIdKey] : nil;
NSString *region = [dictionary[regionKey] isKindOfClass:[NSString class]] ? dictionary[regionKey] : nil;
NSString *blob = [dictionary[blobKey] isKindOfClass:[NSString class]] ? dictionary[blobKey] : nil;
weakSelf.region = region;
weakSelf.blob = blob;
completion([marketingCloudId copy], [region copy], [blob copy], nil);
}] resume];
}
- (NSString *)marketingCloudIdFromUserDefaults {
return [[NSUserDefaults standardUserDefaults] objectForKey:marketingCloudIdUserDefaultsKey];
}
@end