-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMAPIClient.m
259 lines (209 loc) · 9.98 KB
/
MAPIClient.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// MAPIClient.m
// Advocate
//
// Created by Ben Gotow on 6/29/13.
// Copyright (c) 2013 Bloganizer Inc. All rights reserved.
//
#import "MAPIClient.h"
#import "NSError+MErrors.h"
#define PATH_ACTIONS_STATE [@"~/Documents/Actions2.plist" stringByExpandingTildeInPath]
#define PATH_STORE_STATE [@"~/Documents/Store2.plist" stringByExpandingTildeInPath]
@implementation MAPIClient
+ (MAPIClient *)shared
{
static MAPIClient * sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURL * baseURL = [NSURL URLWithString: [[NSBundle mainBundle] objectForInfoDictionaryKey:@"APIRoot"]];
NSLog(@"%@", [baseURL absoluteString]);
sharedClient = [[MAPIClient alloc] initWithBaseURL: baseURL];
});
return sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
[self setRequestSerializer: [AFJSONRequestSerializer serializer]];
[[self requestSerializer] setHTTPShouldHandleCookies: NO];
[self setResponseSerializer: [AFJSONResponseSerializer serializerWithReadingOptions: NSJSONReadingAllowFragments]];
@try {
if ([[NSFileManager defaultManager] fileExistsAtPath: PATH_STORE_STATE]) {
NSDictionary * dict = [NSKeyedUnarchiver unarchiveObjectWithFile: PATH_STORE_STATE];
[self setUser: [dict objectForKey:@"user"]];
}
if ([[NSFileManager defaultManager] fileExistsAtPath: PATH_ACTIONS_STATE])
_transactionsQueue = [NSKeyedUnarchiver unarchiveObjectWithFile: PATH_ACTIONS_STATE];
} @catch (NSException * e) {
NSLog(@"%@", [e description]);
}
if (!_transactionsQueue)
_transactionsQueue = [NSMutableArray array];
[self performNextAction];
}
return self;
}
- (void)apiReachabilityChanged:(AFNetworkReachabilityStatus)status
{
if (status == AFNetworkReachabilityStatusNotReachable) {
if (!_hasDisplayedDisconnectionNotice) {
_hasDisplayedDisconnectionNotice = YES;
NSString * msg = @"You've been disconnected from the internet. Your activity will be saved offline until a connection can be established.";
UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"Offline" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[a show];
}
} else {
_hasDisplayedDisconnectionNotice = NO;
[self performNextAction];
}
}
- (void)updateDiskCache:(BOOL)immediate
{
if (immediate)
[self updateDiskCacheDebounced];
else if (!_updateDiskCacheTriggered) {
[self performSelector:@selector(updateDiskCacheDebounced) withObject:nil afterDelay: 1.0];
_updateDiskCacheTriggered = YES;
}
}
- (void)updateDiskCacheDebounced
{
if (_user) {
[NSKeyedArchiver archiveRootObject:@{@"user":_user} toFile:PATH_STORE_STATE];
[NSKeyedArchiver archiveRootObject:_transactionsQueue toFile: PATH_ACTIONS_STATE];
} else {
[[NSFileManager defaultManager] removeItemAtPath: PATH_ACTIONS_STATE error: nil];
[[NSFileManager defaultManager] removeItemAtPath: PATH_STORE_STATE error:nil];
}
_updateDiskCacheTriggered = NO;
}
#pragma mark Requesting Object Data
- (void)dictionaryAtPath:(NSString*)path userTriggered:(BOOL)triggered success:(void (^)(id responseObject))successCallback failure:(void (^)(NSError *err))failureCallback
{
[self requestPath:path withMethod:@"GET" withParameters:nil userTriggered:triggered expectedClass:[NSDictionary class] success:successCallback failure:failureCallback];
}
- (void)arrayAtPath:(NSString*)path userTriggered:(BOOL)triggered success:(void (^)(id responseObject))successCallback failure:(void (^)(NSError *err))failureCallback
{
[self requestPath:path withMethod:@"GET" withParameters:nil userTriggered:triggered expectedClass:[NSArray class] success:successCallback failure:failureCallback];
}
- (void)requestPath:(NSString*)path withMethod:(NSString*)method withParameters: params userTriggered:(BOOL)triggered expectedClass:(Class)expectation success:(void (^)(id responseObject))successCallback failure:(void (^)(NSError *err))failureCallback
{
if (!self.baseURL)
@throw [NSException exceptionWithName:@"Cannot make request" reason:@"Base URL is not defined." userInfo:nil];
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:params error:nil];
AFHTTPRequestOperation * operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (expectation && ([responseObject isKindOfClass: expectation] == NO)) {
NSError * error = [NSError errorWithExpectationFailure: [responseObject class]];
if (triggered)
[self displayNetworkError:error forOperation:operation withGoal:@"Request"];
if (failureCallback)
failureCallback(error);
return;
}
successCallback(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (triggered)
[self displayNetworkError:error forOperation:operation withGoal:@"Request"];
if (failureCallback)
failureCallback(error);
}];
[self.operationQueue addOperation:operation];
}
- (void)setUser:(MUser *)user
{
_user = user;
if (user)
[[self requestSerializer] setAuthorizationHeaderFieldWithUsername:[user credentialUsername] password:[user credentialPassword]];
else
[[self requestSerializer] clearAuthorizationHeader];
[self updateDiskCache: YES];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_USER_CHANGED object:nil];
}
#pragma mark Tracking API Access and Recovering from Offline State
- (NSUInteger)numberOfQueuedActions
{
return [_transactionsQueue count];
}
- (void)queueAPITransaction:(MAPITransaction*)a
{
// look for another item in the queue effecting the same item that has not started yet
if ([_transactionsQueue containsObject: a] == YES)
return;
[_transactionsQueue addObject: a];
if (([[self reachabilityManager] networkReachabilityStatus] != AFNetworkReachabilityStatusNotReachable) && (![a started]))
[a performDeferred];
NSLog(@"API: Queued API action: %@", [a description]);
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_API_QUEUE_CHANGED object:nil];
}
- (void)removeQueuedTransactionsFor:(MModel*)obj
{
for (int ii = (int)[_transactionsQueue count] - 1; ii >= 0; ii--) {
MAPITransaction * t = [_transactionsQueue objectAtIndex: ii];
if ([t object] == obj)
[_transactionsQueue removeObjectAtIndex: ii];
}
NSLog(@"API: Removed API actions for: %@", [obj description]);
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_API_QUEUE_CHANGED object:nil];
}
- (void)finishedAPITransaction:(MAPITransaction*)a withError:(NSError*)err
{
if ([_transactionsQueue containsObject: a] == NO)
return NSLog(@"Finished unknown API call.");
NSLog(@"API: Finished API action: %@ with error: %@", [a description], [err localizedDescription]);
if (!err) {
[self dequeueAPITransaction: a];
[self performNextAction];
} else if (err) {
// TODO: Additional logic was here... Do we always want to throw away the transaction if it fails once?
[self dequeueAPITransaction: a];
[self displayNetworkError:err forOperation:nil withGoal:@"Request"];
}
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_API_QUEUE_CHANGED object:nil];
}
- (void)dequeueAPITransaction:(MAPITransaction*)a
{
[_transactionsQueue removeObject: a];
[NSKeyedArchiver archiveRootObject:_transactionsQueue toFile:PATH_ACTIONS_STATE];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_API_QUEUE_CHANGED object:nil];
}
- (void)performNextActionIfReconnected
{
if ([[self reachabilityManager] networkReachabilityStatus] != AFNetworkReachabilityStatusNotReachable)
[self performNextAction];
else {
NSString * msg = @"Please connect to the internet and try to sync again.";
UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"Offline" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[a show];
}
}
- (void)performNextAction
{
for (MAPITransaction * a in _transactionsQueue)
if ([a started] == NO)
return [a performDeferred];
// triggers an update of the queue interface
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_API_QUEUE_CHANGED object:nil];
}
#pragma mark Handling Request Results
- (void)displayNetworkError:(NSError*)error forOperation:(AFHTTPRequestOperation*)operation withGoal:(NSString*)goal
{
NSString * message = nil;
BOOL responseIsDict = [[operation responseObject] isKindOfClass: [NSDictionary class]];
if (responseIsDict && [[[operation responseObject] objectForKey: @"error"] isKindOfClass: [NSString class]]) {
message = [[operation responseObject] objectForKey: @"error"];
} else if (responseIsDict && [[[operation responseObject] allKeys] count] == 1) {
id onlyValue = [[[operation responseObject] allValues] lastObject];
if ([onlyValue isKindOfClass: [NSArray class]])
message = [onlyValue lastObject];
else if ([onlyValue isKindOfClass: [NSString class]])
message = onlyValue;
} else if (([error code] == 401) || ([[operation response] statusCode] == 401)) {
message = @"Please check your email address and password.";
} else {
message = [error localizedDescription];
}
NSString * title = [goal stringByAppendingString: @" Failed"];
[[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
@end