-
Notifications
You must be signed in to change notification settings - Fork 36
/
ANAPICall.m
249 lines (182 loc) · 8.38 KB
/
ANAPICall.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
//
// ANAPICall.m
// AppApp
//
// Created by Zach Holmquist on 8/10/12.
// Copyright (c) 2012 Sneakyness. All rights reserved.
//
#import "ANAPICall.h"
@interface ANAPICall()
{
id delegate;
NSString *accessToken;
NSString *userID;
}
-(void)readTokenFromDefaults;
@end
@implementation ANAPICall
+ (ANAPICall *)sharedAppAPI
{
static dispatch_once_t oncePred;
static ANAPICall *sharedInstance = nil;
dispatch_once(&oncePred, ^{
sharedInstance = [[[self class] alloc] initWithSpecification:@"ANAPI"];
});
return sharedInstance;
}
- (id)initWithSpecification:(NSString *)specificationName
{
self = [super initWithSpecification:specificationName];
// do some stuff here later.
return self;
}
- (BOOL)hasAccessToken
{
[self readTokenFromDefaults];
if (accessToken && self.userID)
return YES;
return NO;
}
// TODO: redo these later..
- (void)readTokenFromDefaults
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *token = [defaults objectForKey:@"access_token"];
accessToken = token;
}
- (NSString *)userID
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *idValue = [defaults objectForKey:@"userID"];
return idValue;
}
- (SDWebServiceDataCompletionBlock)defaultJSONProcessingBlock
{
// refactor SDWebService so error's are passed around properly. -- BKS
SDWebServiceDataCompletionBlock result = ^(int responseCode, NSString *response, NSError *error) {
NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError = nil;
id dataObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
return dataObject;
};
return result;
}
- (void)makePostWithText:(NSString*)text uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
// App.net guys (? Alex K. and Mathew Phillips) say we should put accessToken in the headers, like so:
// "Authorization: Bearer " + access_token
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"text" : text };
[self performRequestWithMethod:@"postToStream" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)makePostWithText:(NSString*)text replyToPostID:(NSString *)postID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
// App.net guys (? Alex K. and Mathew Phillips) say we should put accessToken in the headers, like so:
// "Authorization: Bearer " + access_token
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"text" : text, @"post_id" : postID };
[self performRequestWithMethod:@"postToStreamAsReply" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getGlobalStream:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken };
[self performRequestWithMethod:@"getGlobalStream" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUserStream:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken };
[self performRequestWithMethod:@"getUserStream" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUserPosts:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"getUserPosts" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUserPosts:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self getUserPosts:self.userID uiCompletionBlock:uiCompletionBlock];
}
- (void)getUserMentions:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"getUserMentions" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUserMentions:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self getUserMentions:self.userID uiCompletionBlock:uiCompletionBlock];
}
- (void)getCurrentUser:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken };
[self performRequestWithMethod:@"getCurrentUser" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUser:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"getUser" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUserFollowers:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"getUserFollowers" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getUserFollowing:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"getUserFollowing" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)getPostReplies:(NSString *)postID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"post_id" : postID };
[self performRequestWithMethod:@"getPostReplies" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)followUser:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
// App.net guys (? Alex K. and Mathew Phillips) say we should put accessToken in the headers, like so:
// "Authorization: Bearer " + access_token
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"followUser" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
- (void)unfollowUser:(NSString *)ID uiCompletionBlock:(SDWebServiceUICompletionBlock)uiCompletionBlock
{
[self readTokenFromDefaults];
if (!accessToken)
return;
NSDictionary *replacements = @{ @"accessToken" : accessToken, @"user_id" : ID };
[self performRequestWithMethod:@"unfollowUser" routeReplacements:replacements dataProcessingBlock:[self defaultJSONProcessingBlock] uiUpdateBlock:uiCompletionBlock shouldRetry:YES];
}
@end