-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCInstapaper.m
97 lines (72 loc) · 2.82 KB
/
TCInstapaper.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
#import "TCInstapaper.h"
@implementation TCInstapaper
@synthesize delegate, isAuthenticated, secure;
- (id)init
{
self = [super init];
if (self != nil) {
isAuthenticated = NO;
secure = YES;
}
return self;
}
- (id)initWithUsername:(NSString *)username
{
return [self initWithUsername:username andPassword:@""];
}
- (id)initWithUsername:(NSString *)username andPassword:(NSString *)password
{
_username = username;
_password = password;
//NSLog(@"*** TCInstapaper: user = %@, password = %@",_username,_password);
return [self init];
}
- (void)authenticate
{
NSString *requestString = [NSString stringWithFormat:@"username=%@&password=%@",_username,_password];
//NSLog(@"*** TCInstapaper: %@",[requestString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]);
NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]];
NSString *secureHTTP = (self.secure)?(@"https"):(@"http");
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://www.instapaper.com/api/authenticate",secureHTTP]]];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)addURLString:(NSString *)urlString title:(NSString *)title
{
NSString *requestString = [NSString stringWithFormat:@"username=%@&password=%@&url=%@",_username,_password,urlString];
if (title)
{
requestString = [requestString stringByAppendingFormat:@"&title=%@",title];
}
NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]];
NSString *secureHTTP = (self.secure)?(@"https"):(@"http");
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://www.instapaper.com/api/add",secureHTTP]]];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)addURLString:(NSString *)urlString
{
[self addURLString:urlString title:nil];
}
#pragma mark -
#pragma mark NSURLConnectionDelegate Methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// TODO: error handling
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[_connection cancel];
_connection = nil;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
[self.delegate instapaper:self didDidFinishRequestWithCode:[httpResponse statusCode]];
}
- (void)dealloc
{
[_connection release];
[super dealloc];
}
@end