/
UATagGroupsAPIClient.m
110 lines (88 loc) · 4.56 KB
/
UATagGroupsAPIClient.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
/* Copyright 2018 Urban Airship and Contributors */
#import "UATagGroupsAPIClient+Internal.h"
#import "UATagGroupsMutation+Internal.h"
#import "UAConfig.h"
#import "NSJSONSerialization+UAAdditions.h"
#import "NSURLResponse+UAAdditions.h"
#define kUAChannelTagGroupsPath @"/api/channels/tags/"
#define kUANamedUserTagsPath @"/api/named_users/tags/"
#define kUATagGroupsAudienceKey @"audience"
#define kUATagGroupsIosChannelKey @"ios_channel"
#define kUATagGroupsNamedUserIdKey @"named_user_id"
#define kUATagGroupsResponseObjectWarningsKey @"warnings"
#define kUATagGroupsResponseObjectErrorKey @"error"
@interface UATagGroupsAPIClient()
@property (nonatomic,strong) NSString *tagGroupsPath;
@property (nonatomic,strong) NSString *tagGroupsKey;
@end
@implementation UATagGroupsAPIClient
+ (instancetype)channelClientWithConfig:(UAConfig *)config {
UATagGroupsAPIClient *client = [self channelClientWithConfig:config session:[UARequestSession sessionWithConfig:config]];
return client;
}
+ (instancetype)channelClientWithConfig:(UAConfig *)config session:(UARequestSession *)session {
UATagGroupsAPIClient *client = [[self alloc] initWithConfig:config session:session];
client.tagGroupsPath = kUAChannelTagGroupsPath;
client.tagGroupsKey = kUATagGroupsIosChannelKey;
return client;
}
+ (instancetype)namedUserClientWithConfig:(UAConfig *)config {
UATagGroupsAPIClient *client = [self namedUserClientWithConfig:config session:[UARequestSession sessionWithConfig:config]];
return client;
}
+ (instancetype)namedUserClientWithConfig:(UAConfig *)config session:(UARequestSession *)session {
UATagGroupsAPIClient *client = [[self alloc] initWithConfig:config session:session];
client.tagGroupsPath = kUANamedUserTagsPath;
client.tagGroupsKey = kUATagGroupsNamedUserIdKey;
return client;
}
- (void)updateTagGroupsForId:(NSString *)identifier
tagGroupsMutation:(UATagGroupsMutation *)mutation
completionHandler:(void (^)(NSUInteger status))completionHandler {
[self performTagGroupsMutation:mutation
path:self.tagGroupsPath
audience:@{self.tagGroupsKey : identifier}
completionHandler:completionHandler];
}
- (void)performTagGroupsMutation:(UATagGroupsMutation *)mutation
path:(NSString *)path
audience:(NSDictionary *)audience
completionHandler:(void (^)(NSUInteger status))completionHandler {
if (!self.enabled) {
UA_LDEBUG(@"Disabled");
return;
}
NSMutableDictionary *payload = [[mutation payload] mutableCopy];
[payload setValue:audience forKey:kUATagGroupsAudienceKey];
UARequest *request = [UARequest requestWithBuilderBlock:^(UARequestBuilder *builder) {
NSString *urlString = [NSString stringWithFormat:@"%@%@", self.config.deviceAPIURL, path];
builder.URL = [NSURL URLWithString:urlString];
builder.method = @"POST";
builder.username = self.config.appKey;
builder.password = self.config.appSecret;
builder.body = [NSJSONSerialization dataWithJSONObject:payload options:NSJSONWritingPrettyPrinted error:nil];
[builder setValue:@"application/vnd.urbanairship+json; version=3;" forHeader:@"Accept"];
[builder setValue:@"application/json" forHeader:@"Content-Type"];
}];
UA_LTRACE(@"Updating tag groups with payload: %@", payload);
[self.session dataTaskWithRequest:request retryWhere:^BOOL(NSData * _Nullable data, NSURLResponse * _Nullable response) {
return [response hasRetriableStatus];
} completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *httpResponse = nil;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
httpResponse = (NSHTTPURLResponse *) response;
}
NSInteger status = httpResponse.statusCode;
if (data) {
NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (responseBody[kUATagGroupsResponseObjectWarningsKey]) {
UA_LINFO(@"Tag group request for: %@ completed with warnings: %@", response.URL, responseBody[kUATagGroupsResponseObjectWarningsKey]);
}
if (responseBody[kUATagGroupsResponseObjectErrorKey]) {
UA_LINFO(@"Tag group request for: %@ completed with errors: %@", response.URL, responseBody[kUATagGroupsResponseObjectErrorKey]);
}
}
completionHandler(status);
}];
}
@end