-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathInstapaperPost.m
176 lines (141 loc) · 4.73 KB
/
InstapaperPost.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
/**
* @file InstapaperPost.m
*/
#import "InstapaperPost.h"
#import "NSString+Tumblrful.h"
#import "UserSettings.h"
#import "DebugLog.h"
static NSString * API_ADD_ENDPOINT = @"https://www.instapaper.com/api/add";
#define TIMEOUT (30.0)
@interface InstapaperPost ()
- (NSURLRequest *)createRequest:(NSDictionary *)params;
- (void)callbackOnMainThread:(SEL)selector withObject:(id)obj;
@end
@implementation InstapaperPost
#pragma mark -
#pragma mark Custom Public Methods
+ (BOOL)enabled
{
return [[UserSettings sharedInstance] boolForKey:@"instapaperEnabled"];
}
#pragma mark -
#pragma mark Override Methods
+ (NSString *)username
{
return [[UserSettings sharedInstance] stringForKey:@"instapaperUsername"];
}
+ (NSString *)password
{
return [[UserSettings sharedInstance] stringForKey:@"instapaperPassword"];
}
- (id)initWithCallback:(NSObject<PostCallback> *)callback
{
if ((self = [super init]) != nil) {
callback_ = [callback retain];
}
return self;
}
- (void)dealloc
{
[callback_ release], callback_ = nil;
[data_ release], data_ = nil;
[super dealloc];
}
- (NSMutableDictionary *)createMinimumRequestParams
{
NSString * username = [InstapaperPost username];
NSString * password = [InstapaperPost password];
return [NSMutableDictionary dictionaryWithObjectsAndKeys:username, @"username", password, @"password", nil];
}
- (BOOL)privated
{
return NO;
}
- (void)postWith:(NSDictionary *)params
{
data_ = [[NSMutableData data] retain];
NSURLRequest * request = [self createRequest:params]; // request は connection に指定した時点で reatin upする
D(@"request:%@", [request description]);
NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection == nil) {
[self callbackOnMainThread:@selector(failedWithError:) withObject:nil];
[data_ release], data_ = nil;
}
}
#pragma mark -
#pragma mark Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
#pragma unused (connection)
D_METHOD;
NSString * user = [InstapaperPost username];
NSString * pass = [InstapaperPost password];
NSURLCredential * crendential = [NSURLCredential credentialWithUser:user password:pass persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:crendential forAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
#pragma unused (connection)
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response; // この cast は正しい
if ([httpResponse statusCode] == 200 || [httpResponse statusCode] == 201) {
D(@"success. HTTP status=%d", [httpResponse statusCode]);
}
else {
D(@"failed. HTTP status=%d, allHeaderFields=%@", [httpResponse statusCode], [[httpResponse allHeaderFields] description]);
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
#pragma unused (connection)
[data_ appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
#pragma unused (connection)
D(@"response data is %d bytes", [data_ length]);
if (callback_ != nil) {
NSString * statusCode = [[[NSString alloc] initWithData:data_ encoding:NSUTF8StringEncoding] autorelease];
[self callbackOnMainThread:@selector(successed:) withObject:statusCode];
}
else {
D(@"Callback object should be set");
}
[self autorelease];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
#pragma unused (connection)
D0([error description]);
[self callbackOnMainThread:@selector(failedWithError:) withObject:error];
[self autorelease];
}
#pragma mark -
#pragma mark Private Methods
- (NSURLRequest *)createRequest:(NSDictionary *)params
{
@try {
// create URL with parameters
NSMutableString * ps = [NSMutableString string];
NSEnumerator * enumerator = [params keyEnumerator];
for (NSString * key; (key = [enumerator nextObject]) != nil; ) {
D(@"%@=%@", key, [params objectForKey:key]);
[ps appendFormat:@"&%@=%@", key, [[params objectForKey:key] stringByURLEncoding:NSUTF8StringEncoding]];
}
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", API_ADD_ENDPOINT, ps]];
// create the POST request
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:TIMEOUT];
[request setValue:@"Tumblrful" forHTTPHeaderField:@"User-Agent"];
return request;
}
@catch (NSException * e) {
D0([e description]);
}
return nil;
}
- (void)callbackOnMainThread:(SEL)selector withObject:(id)obj
{
if (callback_ != nil && [callback_ respondsToSelector:selector]) {
[callback_ performSelectorOnMainThread:selector withObject:obj waitUntilDone:NO];
}
}
@end