forked from this-sam/myFlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadItLaterLite.m
215 lines (137 loc) · 6 KB
/
ReadItLaterLite.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
//
// ReadItLaterLite.m
// ReadItLaterAPI
// Version 1.0
//
// Created by Nathan Weiner on 5/9/09.
// Copyright 2009 Idea Shower. All rights reserved.
//
#import "ReadItLaterLite.h"
#import "RIL_apikeys.h"
@implementation ReadItLater
static NSString *apikey = RIL_APIKEYS; //Enter your apikey here : get one at http://readitlaterlist.com/api/
static NSString *nameOfYourApp = @"Tweetee"; //Enter the name of your application here (optional - for user-agent string)
@synthesize delegate, method, apiResponse, requestData, stringResponse;
/* -----------------
SEE THE ReadItLaterLite.h FILE FOR COMMENTS/DOCUMENTATION
ADDITIONAL DOCUMENTATION, SCREENSHOTS, EXAMPLES ARE AVAILABLE AT:
http://readitlaterlist.com/api_iphone/
-------------------- */
- (void)dealloc {
[method release];
[apiResponse release];
[requestData release];
[stringResponse release];
[super dealloc];
}
/* ----------------------------------- */
// Saving Methods, see ReadItLaterLite.h for comments/documentation
+(void)save:(NSURL *)url title:(NSString *)title delegate:(id)delegate {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[ReadItLater save:url title:title delegate:delegate username:[prefs objectForKey:@"ReadItLaterUsername"] password:[prefs objectForKey:@"ReadItLaterPassword"]];
}
+(void)save:(NSURL *)url title:(NSString *)title delegate:(id)delegate username:(NSString *)username password:(NSString *)password {
ReadItLater *readItLater = [[ReadItLater alloc] init];
readItLater.delegate = delegate;
[readItLater sendRequest:@"add" username:username password:password params:[NSString stringWithFormat:@"url=%@&title=%@", [ReadItLater encode:[url.absoluteString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]], [ReadItLater encode:[title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] ]];
}
/* ----------------------------------- */
// --- LOGIN/SETUP METHODS --- //
// see ReadItLaterLite.h for comments/documentation
+(void)authWithUsername:(NSString *)username password:(NSString *)password delegate:(id)delegate {
ReadItLater *readItLater = [[ReadItLater alloc] init];
readItLater.delegate = delegate;
[readItLater sendRequest:@"auth" username:username password:password params:nil];
}
+(void)signupWithUsername:(NSString *)username password:(NSString *)password delegate:(id)delegate {
ReadItLater *readItLater = [[ReadItLater alloc] init];
readItLater.delegate = delegate;
[readItLater sendRequest:@"signup" username:username password:password params:nil];
}
@end
/* ----- The following methods are used by the simple/auto methods listed above, you should not implement these --- */
@implementation ReadItLater (Private)
// -- //
-(void)sendRequest:(NSString *)newMethod username:(NSString *)username password:(NSString *)password params:(NSString *)additionalParams {
self.method = newMethod;
requestData = [[NSMutableData alloc] initWithLength:0];
// Create Request
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://readitlaterlist.com/v2/%@", method]];
NSMutableURLRequest *request = [ [NSMutableURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:90];
// Setup Request Data/Params
NSMutableString *params = [NSMutableString stringWithFormat:@"apikey=%@&username=%@&password=%@", apikey, [ReadItLater encode:username], [ReadItLater encode:password]];
if (additionalParams != nil) {
[params appendFormat:@"&%@", additionalParams];
}
NSData *paramsData = [ NSData dataWithBytes:[params UTF8String] length:[params length] ];
// Fill Request
[request setHTTPMethod:@"POST"];
[request setValue:nameOfYourApp!=nil?nameOfYourApp:@"RIL API iPhone Library v2.0" forHTTPHeaderField:@"User-Agent"];
[request setHTTPBody:paramsData];
// Start Connection
[[NSURLConnection alloc]
initWithRequest:request
delegate:self
startImmediately:YES];
[request release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
self.apiResponse = [[response allHeaderFields] mutableCopy];
[apiResponse setObject:[NSString stringWithFormat:@"%i", [response statusCode]] forKey:@"statusCode"];
[apiResponse release];
[requestData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[requestData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.stringResponse = [[NSString alloc] initWithData:requestData encoding:NSUTF8StringEncoding];
[self.stringResponse release];
[self finishConnection];
[connection release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
self.apiResponse = [NSDictionary dictionaryWithObjectsAndKeys:@"X-Error",[error localizedDescription],nil];
[self finishConnection];
[connection release];
}
-(void)finishConnection {
// Determine Result
NSString *statusCode = [apiResponse objectForKey:@"statusCode"];
int success = (statusCode != nil && [statusCode isEqualToString:@"200"]);
NSString *error = !success ? [apiResponse objectForKey:@"X-Error"] : nil;
// Send to delegate
SEL selector;
if ([method isEqualToString:@"auth"])
{
selector = @selector(readItLaterLoginFinished:error:);
}
else if ([method isEqualToString:@"signup"])
{
selector = @selector(readItLaterSignupFinished:error:);
}
else if ([method isEqualToString:@"add"])
{
selector = @selector(readItLaterSaveFinished:error:);
}
if ([delegate respondsToSelector:selector]) {
[delegate performSelector:selector withObject:stringResponse withObject:error];
}
[self done];
}
-(void)done {
[self release];
}
/* --- */
+(NSString *)encode:(NSString *)string {
CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(CFStringRef) string,
nil,
(CFStringRef)@"&+",
kCFStringEncodingUTF8);
return [((NSString*) encoded) autorelease];
}
@end