/
UAChannelAPIClient.m
109 lines (87 loc) · 4.64 KB
/
UAChannelAPIClient.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
/* Copyright 2018 Urban Airship and Contributors */
#import "UAChannelAPIClient+Internal.h"
#import "UAChannelRegistrationPayload+Internal.h"
#import "UAConfig.h"
#import "UAUtils+Internal.h"
#import "UAirship.h"
#import "UAAnalytics+Internal.h"
#import "NSJSONSerialization+UAAdditions.h"
#import "NSURLResponse+UAAdditions.h"
#define kUAChannelCreateLocation @"/api/channels/"
@implementation UAChannelAPIClient
+ (instancetype)clientWithConfig:(UAConfig *)config {
return [UAChannelAPIClient clientWithConfig:config session:[UARequestSession sessionWithConfig:config]];
}
+ (instancetype)clientWithConfig:(UAConfig *)config session:(UARequestSession *)session {
return [[UAChannelAPIClient alloc] initWithConfig:config session:session];
}
- (void)createChannelWithPayload:(UAChannelRegistrationPayload *)payload
onSuccess:(UAChannelAPIClientCreateSuccessBlock)successBlock
onFailure:(UAChannelAPIClientFailureBlock)failureBlock {
UA_LTRACE(@"Creating channel with: %@.", payload);
UARequest *request = [UARequest requestWithBuilderBlock:^(UARequestBuilder *builder) {
NSString *urlString = [NSString stringWithFormat:@"%@%@", self.config.deviceAPIURL, kUAChannelCreateLocation];
builder.URL = [NSURL URLWithString:urlString];
builder.method = @"POST";
builder.username = self.config.appKey;
builder.password = self.config.appSecret;
builder.body = [payload asJSONData];
[builder setValue:@"application/vnd.urbanairship+json; version=3;" forHeader:@"Accept"];
[builder setValue:@"application/json" forHeader:@"Content-Type"];
}];
[self.session dataTaskWithRequest:request retryWhere:^BOOL(NSData *data, NSURLResponse *response) {
return [response hasRetriableStatus];
} completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = nil;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
httpResponse = (NSHTTPURLResponse *) response;
}
// Failure
if (httpResponse.statusCode != 200 && httpResponse.statusCode != 201) {
UA_LTRACE(@"Channel creation failed with status: %ld error: %@", (unsigned long)httpResponse.statusCode, error);
failureBlock(httpResponse.statusCode);
return;
}
// Success
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
UA_LTRACE(@"Channel creation succeeded with status: %ld jsonResponse: %@", (unsigned long)httpResponse.statusCode, jsonResponse);
// Parse the response
NSString *channelID = [jsonResponse valueForKey:@"channel_id"];
NSString *channelLocation = [httpResponse.allHeaderFields valueForKey:@"Location"];
BOOL existing = httpResponse.statusCode == 200;
successBlock(channelID, channelLocation, existing);
}];
}
- (void)updateChannelWithLocation:(NSString *)channelLocation
withPayload:(UAChannelRegistrationPayload *)payload
onSuccess:(UAChannelAPIClientUpdateSuccessBlock)successBlock
onFailure:(UAChannelAPIClientFailureBlock)failureBlock {
UA_LTRACE(@"Updating channel with: %@.", payload);
UARequest *request = [UARequest requestWithBuilderBlock:^(UARequestBuilder *builder) {
builder.URL = [NSURL URLWithString:channelLocation];
builder.method = @"PUT";
builder.username = self.config.appKey;
builder.password = self.config.appSecret;
builder.body = [payload asJSONData];
[builder setValue:@"application/vnd.urbanairship+json; version=3;" forHeader:@"Accept"];
[builder setValue:@"application/json" forHeader:@"Content-Type"];
}];
[self.session dataTaskWithRequest:request retryWhere:^BOOL(NSData *data, NSURLResponse *response) {
return [response hasRetriableStatus];
} completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = nil;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
httpResponse = (NSHTTPURLResponse *) response;
}
// Failure
if (httpResponse.statusCode != 200 && httpResponse.statusCode != 201) {
UA_LTRACE(@"Channel update failed with status: %ld error: %@", (unsigned long)httpResponse.statusCode, error);
failureBlock(httpResponse.statusCode);
return;
}
// Success
UA_LTRACE(@"Channel update succeeded with status: %ld", (unsigned long)httpResponse.statusCode);
successBlock();
}];
}
@end