-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDeliciousPost.m
192 lines (159 loc) · 5.3 KB
/
DeliciousPost.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
/**
* @file DeliciousPost.m
* @brief DeliciousPost class implementation
* @author Masayuki YAMAYA
* @date 2008-03-28
*/
// /System/Library/Frameworks/Foundation.framework/Headers/NSXMLDocument.h
// /System/Library/Frameworks/Foundation.framework/Headers/NSXMLNode.h
#import "DeliciousPost.h"
#import "NSDataBase64.h"
#import "UserSettings.h"
#import "NSString+Tumblrful.h"
#import "DebugLog.h"
#import <Foundation/NSXMLDocument.h>
static NSString * API_ADD_ENDPOINT = @"https://api.del.icio.us/v1/posts/add?";
#define TIMEOUT (30.0)
@interface DeliciousPost ()
- (NSURLRequest *)createRequest:(NSDictionary *)params;
- (void)callback:(SEL)selector withObject:(id)obj;
@end
@implementation DeliciousPost
#pragma mark -
#pragma mark Custom Public Methods
+ (BOOL)enabled
{
return [[UserSettings sharedInstance] boolForKey:@"deliciousEnabled"];
}
#pragma mark -
#pragma mark Override Methods
+ (NSString *)username
{
return [[UserSettings sharedInstance] stringForKey:@"deliciousUsername"];
}
+ (NSString *)password
{
return [[UserSettings sharedInstance] stringForKey:@"deliciousPassword"];
}
- (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];
}
- (BOOL)privated
{
return [[UserSettings sharedInstance] boolForKey:@"deliciousPrivateEnabled"];
}
- (NSMutableDictionary *)createMinimumRequestParams
{
NSString * shared = [NSString stringWithFormat:@"%@", ([self privated] ? @"no" : @"yes")];
return [NSMutableDictionary dictionaryWithObjectsAndKeys:shared, @"shared", nil];
}
- (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 callback:@selector(failedWithError:) withObject:nil];
[data_ release], data_ = nil;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
#pragma unused (connection)
D_METHOD;
NSString * user = [DeliciousPost username];
NSString * pass = [DeliciousPost 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;
if ([httpResponse statusCode] != 201) {
D(@"Abnormal! statusCode: %d", [httpResponse statusCode]);
D(@"allHeaderFields: %@", [[httpResponse allHeaderFields] description]);
}
[data_ setLength:0]; // initialize receive buffer
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
#pragma unused (connection)
[data_ appendData:data]; // append data to receive buffer
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
#pragma unused (connection)
D(@"response data is %d bytes", [data_ length]);
@try {
if (callback_ != nil) {
NSError * error = nil;
NSXMLDocument * xmlDoc = [[[NSXMLDocument alloc] initWithData:data_ options:NSXMLDocumentTidyHTML error:&error] autorelease];
if (error != nil) {
[self callback:@selector(failedWithError:) withObject:error];
return;
}
NSString * resultCode = nil;
NSXMLNode * node = [[xmlDoc rootElement] attributeForName:@"code"];
if (node != nil) {
resultCode = [node stringValue];
}
D(@"resultCode:%@", resultCode);
[self callback:@selector(successed:) withObject:resultCode];
}
else {
D(@"Callback object should be set");
}
}
@catch (NSException * e) {
[self callback:@selector(failedWithException:) withObject:e];
}
@finally {
[self autorelease];
}
}
- (NSURLRequest *)createRequest:(NSDictionary *)params
{
@try {
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)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
#pragma unused (connection)
D0([error description]);
[self callback:@selector(failedWithError:) withObject:error];
[self autorelease];
}
- (void)callback:(SEL)selector withObject:(id)obj
{
if (callback_ != nil && [callback_ respondsToSelector:selector]) {
[callback_ performSelectorOnMainThread:selector withObject:obj waitUntilDone:NO];
}
}
@end