-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIRWebAPITwitterInterface+Friendships.m
More file actions
144 lines (79 loc) · 4.75 KB
/
IRWebAPITwitterInterface+Friendships.m
File metadata and controls
144 lines (79 loc) · 4.75 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
//
// IRWebAPITwitterInterface+Friendships.m
// IRWebAPIKit
//
// Created by Evadne Wu on 4/3/11.
// Copyright 2011 Iridia Productions. All rights reserved.
//
#import "IRWebAPITwitterInterface+Friendships.h"
@implementation IRWebAPITwitterInterface (Friendships)
+ (BOOL) repeatedlyCalledSuccessHandlerResponseExhausted:(NSDictionary *)response {
if (!response || ![[response objectForKey:@"ids"] count])
return YES;
if ([[response objectForKey:@"next_cursor"] isEqual:[response objectForKey:@"previous_cursor"]])
return YES;
return NO;
}
- (void) retrieveFriendsOfUser:(IRWebAPITwitterUserID)userID withConcatenatedSuccessHandler:(IRWebAPIInterfaceCallback)inSuccessCallback failureHandler:(IRWebAPIInterfaceCallback)inFailureCallback {
[self retrieveFriendsOfUser:userID withCallbackStyle:IRWebAPIInterfaceCallbackStyleConcatenatedCallback successHandler:inSuccessCallback failureHandler:inFailureCallback];
}
- (void) retrieveFriendsOfUser:(IRWebAPITwitterUserID)userID withRepeatedlyCalledSuccessHandler:(IRWebAPIInterfaceCallback)inSuccessCallback failureHandler:(IRWebAPIInterfaceCallback)inFailureCallback {
[self retrieveFriendsOfUser:userID withCallbackStyle:IRWebAPIInterfaceCallbackStyleManyCallbacks successHandler:inSuccessCallback failureHandler:inFailureCallback];
}
- (void) retrieveFriendsOfUser:(IRWebAPITwitterUserID)userID withCallbackStyle:(IRWebAPIInterfaceCallbackStyle)style successHandler:(IRWebAPIInterfaceCallback)inSuccessCallback failureHandler:(IRWebAPIInterfaceCallback)inFailureCallback {
NSMutableDictionary *actualResponseOrNil = [NSMutableDictionary dictionary]; // Get everything
void (^enqueueIdentifiersFromResponse)(NSDictionary *response) = ^ (NSDictionary *response) {
NSMutableArray *actualIDs = [actualResponseOrNil objectForKey:@"ids"];
if (![actualIDs isKindOfClass:[NSMutableArray class]]) {
actualIDs = [[actualIDs mutableCopy] autorelease];
[actualResponseOrNil setObject:actualIDs forKey:@"ids"];
}
[actualIDs addObjectsFromArray:[response objectForKey:@"ids"]];
};
BOOL (^responseExhausted)(NSDictionary *responseBody) = ^ (NSDictionary *responseBody) { return [[self class] repeatedlyCalledSuccessHandlerResponseExhausted:responseBody]; };
__block void (^workingBlock)(unsigned long long queuedCursorID);
workingBlock = ^ (unsigned long long queuedCursorID) {
[self retrieveFriendsOfUser:userID withCursor:queuedCursorID successHandler: ^ (NSDictionary *inResponseOrNil, BOOL *outNotifyDelegate, BOOL *outShouldRetry) {
switch (style) {
case IRWebAPIInterfaceCallbackStyleConcatenatedCallback: {
enqueueIdentifiersFromResponse(inResponseOrNil);
if (responseExhausted(inResponseOrNil)) {
if (inSuccessCallback)
inSuccessCallback(actualResponseOrNil, outNotifyDelegate, outShouldRetry);
return;
}
break;
}
case IRWebAPIInterfaceCallbackStyleManyCallbacks : {
if (inSuccessCallback)
inSuccessCallback(inResponseOrNil, outNotifyDelegate, outShouldRetry);
if (responseExhausted(inResponseOrNil)) {
return;
}
break;
}
default: {
NSParameterAssert(NO);
}
}
workingBlock([[inResponseOrNil objectForKey:@"next_cursor"] unsignedLongLongValue]);
} failureHandler: ^ (NSDictionary *inResponseOrNil, BOOL *outNotifyDelegate, BOOL *outShouldRetry) {
NSLog(@"FAIL %@", inResponseOrNil);
}];
};
workingBlock(-1); // -1 starts pagination, and the cursor defaults to -1 per official docs
}
- (void) retrieveFriendsOfUser:(IRWebAPITwitterUserID)userID withCursor:(unsigned long long)cursorID successHandler:(IRWebAPIInterfaceCallback)inSuccessCallback failureHandler:(IRWebAPIInterfaceCallback)inFailureCallback {
NSAssert(self.authenticator.currentCredentials.identifier, @"Error: %s requires that the current credentials be present.", __PRETTY_FUNCTION__);
[self.engine fireAPIRequestNamed:[NSString stringWithFormat:@"1/friends/ids.json"] withArguments:[NSDictionary dictionaryWithObjectsAndKeys:
IRWebAPIKitNumberOrNull([NSNumber numberWithUnsignedLongLong:cursorID]), @"cursor",
IRWebAPIKitNumberOrNull([NSNumber numberWithUnsignedLongLong:userID]), @"user_id",
nil] options:nil validator:[self defaultNoErrorValidator] successHandler: ^ (NSDictionary *inResponseOrNil, NSDictionary *inResponseContext, BOOL *outNotifyDelegate, BOOL *outShouldRetry) {
if (inSuccessCallback)
inSuccessCallback(inResponseOrNil, outNotifyDelegate, outShouldRetry);
} failureHandler: ^ (NSDictionary *inResponseOrNil, NSDictionary *inResponseContext, BOOL *outNotifyDelegate, BOOL *outShouldRetry) {
if (inFailureCallback)
inFailureCallback(inResponseOrNil, outNotifyDelegate, outShouldRetry);
}];
}
@end