This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ORSSession.m
344 lines (303 loc) · 11.1 KB
/
ORSSession.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// ORSSession.m
// Twitter Engine
//
// Created by Nicholas Toumpelis on 12/04/2009.
// Copyright 2009 Ocean Road Software. All rights reserved.
//
// Version 0.7
#import "ORSSession.h"
@implementation ORSSession
@synthesize userID, password;
// Initialiser
- (id) initWithUserID:(NSString *)newUserID
andPassword:(NSString *)newPassword {
self = [super init];
if (self != nil) {
userID = newUserID;
password = newPassword;
}
return self;
}
// Copy with zone
- (id) copyWithZone:(NSZone *)zone {
return [[ORSSession alloc] initWithUserID:userID andPassword:password];
}
// Executes any request both synchronously and asynchronously. For asynchronous
// requests it requires the help of NSURLConnection delegates. For synchronous
// requests the data is returned directly while for asynchronous the
// setStatuses method in the controller is called upon finishing.
- (NSData *) executeRequestOfType:(NSString *)type
atPath:(NSString *)path
synchronously:(BOOL)synchr {
@synchronized(self) {
// Forming the first part of the request URL
NSMutableString *requestURL = [NSMutableString
stringWithFormat:@"https://%@:%@@twitter.com/", userID, password];
// Appending the second part of the request URL, the "path"
[requestURL appendString:path];
// Correcting the encoding
NSString *encodedRequestURL = [requestURL
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Creating the request
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:encodedRequestURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:48.0];
// Adding some extra values to the request
[request addValue:@"Canary" forHTTPHeaderField:@"X-Twitter-Client"];
[request addValue:@"0.6"
forHTTPHeaderField:@"X-Twitter-Client-Version"];
[request addValue:@"http://macsphere.wordpress.com/"
forHTTPHeaderField:@"X-Twitter-Client-URL"];
// Setting the request method (except GET - doesn't need to be specified
// explicitly)
if (![type isEqualToString:@"GET"]) {
[request setHTTPMethod:type];
}
// Checking whether the request should be synchronous or asynchronous
if (!synchr) {
// If asynchronous... setting up the connection
mainConnection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (mainConnection) {
// the data buffer
dataReceived = [NSMutableData data];
} else {
}
return NULL;
} else {
// If synchronous
NSURLResponse *response = NULL;
NSError *error = NULL;
// the data is returned "immediately"
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
return data;
} else {
}
return NULL;
}
}
return NULL;
}
// Executes any request without URL encoding
- (NSData *) executeUnencodedRequestOfType:(NSString *)type
atPath:(NSString *)path
synchronously:(BOOL)synchr {
@synchronized(self) {
// Forming the first part of the request URL
NSMutableString *requestURL = [NSMutableString
stringWithFormat:@"https://%@:%@@twitter.com/", userID, password];
// Appending the second part of the request URL, the "path"
[requestURL appendString:path];
// Creating the request
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:requestURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:48.0];
// Adding some extra values to the request
[request addValue:@"Canary" forHTTPHeaderField:@"X-Twitter-Client"];
[request addValue:@"0.6"
forHTTPHeaderField:@"X-Twitter-Client-Version"];
[request addValue:@"http://macsphere.wordpress.com/"
forHTTPHeaderField:@"X-Twitter-Client-URL"];
// Setting the request method (except GET - doesn't need to be specified
// explicitly)
if (![type isEqualToString:@"GET"]) {
[request setHTTPMethod:type];
}
// Checking whether the request should be synchronous or asynchronous
if (!synchr) {
// If asynchronous... setting up the connection
mainConnection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (mainConnection) {
// the data buffer
dataReceived = [NSMutableData data];
} else {
}
return NULL;
} else {
// If synchronous
NSURLResponse *response = NULL;
NSError *error = NULL;
// the data is returned "immediately"
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
return data;
} else {
}
return NULL;
}
}
return NULL;
}
// Executes any request both synchronously and asynchronously. Works like above
// but does not return any data. Used in cases where returned data is not a
// concern.
- (void) simpleExecuteRequestOfType:(NSString *)type
atPath:(NSString *)path
synchronously:(BOOL)synchr {
@synchronized(self) {
// Forming the first part of the request URL
NSMutableString *requestURL = [NSMutableString
stringWithFormat:@"https://%@:%@@twitter.com/", userID, password];
// Appending the second part of the request URL, the "path"
[requestURL appendString:path];
// Correcting the encoding
NSString *encodedRequestURL = [requestURL
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Creating the request
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:encodedRequestURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:48.0];
// Adding some extra values to the request
[request addValue:@"Canary" forHTTPHeaderField:@"X-Twitter-Client"];
[request addValue:@"0.6"
forHTTPHeaderField:@"X-Twitter-Client-Version"];
[request addValue:@"http://macsphere.wordpress.com/"
forHTTPHeaderField:@"X-Twitter-Client-URL"];
// Setting the request method (except GET - doesn't need to be specified
// explicitly)
if (![type isEqualToString:@"GET"]) {
[request setHTTPMethod:type];
}
// Checking whether the request should be synchronous or asynchronous
if (!synchr) {
// If asynchronous... setting up the connection
mainConnection = [[NSURLConnection alloc] initWithRequest:request
delegate:nil];
} else {
return;
}
}
}
// Returns an XML document from the given data
- (NSXMLDocument *) getXMLDocumentFromData:(NSData *)data {
NSError *error = NULL;
NSString *xml = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSXMLDocument *xmlDocument = [[NSXMLDocument alloc] initWithXMLString:xml
options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA)
error:&error];
if (xmlDocument == NULL) {
xmlDocument = [[NSXMLDocument alloc] initWithXMLString:xml
options:NSXMLDocumentTidyXML error:&error];
}
if (xmlDocument == NULL) {
if (error) {
// Error handling
}
return NULL;
}
if (error) {
// Error handling
}
return xmlDocument;
}
// Returns the node from the data received from the connection
- (NSXMLNode *) getNodeFromData:(NSData *)data {
NSXMLDocument *xmlDocument = [self getXMLDocumentFromData:data];
return [xmlDocument rootElement];
}
// Returns all the statuses as an array from the data received from the
// connection.
- (NSArray *) getAllStatusesFromData:(NSData *)statuses {
NSError *error = NULL;
NSXMLNode *root = [self getNodeFromData:statuses];
return [root nodesForXPath:@".//status" error:&error];
}
// Returns all the users as an array from the data received from the
// connection.
- (NSArray *) getAllUsersFromData:(NSData *)users {
NSError *error = NULL;
NSXMLNode *root = [self getNodeFromData:users];
return [root nodesForXPath:@".//user" error:&error];
}
// Returns all the users as an array from the data received from the
// connection.
- (NSArray *) getAllDMsFromData:(NSData *)directMessages {
NSError *error = NULL;
NSXMLNode *root = [self getNodeFromData:directMessages];
return [root nodesForXPath:@".//direct_message" error:&error];
}
#pragma mark NSURLConnection delegates for async conn
// NSURLConnection delegates for asynchronous connections
// is called when the server requests (additional) authentication.
- (void) connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
@synchronized(self) {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential = [NSURLCredential
credentialWithUser:userID
password:password
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
}
// is called when the connection receives a response from the server.
- (void) connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
@synchronized(self) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"OTEReceivedResponse"
object:response];
[dataReceived setLength:0];
}
}
// is called when the connection receives some data from the server.
- (void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *)dataReceivedArg {
@synchronized(self) {
[dataReceived appendData:dataReceivedArg];
}
}
// is called when the connection faces some kind of error.
- (void) connection:(NSURLConnection *)connection
didFailWithError:(NSError *)connectionError {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"OTEConnectionFailure"
object:connectionError];
}
// is called when the data received from the server finishes.
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
@synchronized(self) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSXMLNode *node = [self getNodeFromData:dataReceived];
if ([[node name] isEqualToString:@"statuses"]) {
[nc postNotificationName:@"OTEStatusesDidFinishLoading"
object:[self getAllStatusesFromData:dataReceived]];
} else if ([[node name] isEqualToString:@"users"]) {
[nc postNotificationName:@"OTEUsersDidFinishLoading"
object:[self getAllUsersFromData:dataReceived]];
} else if ([[node name] isEqualToString:@"direct-messages"] ||
[[node name] isEqualToString:@"nilclasses"]) {
[nc postNotificationName:@"OTEDMsDidFinishLoading"
object:[self getAllDMsFromData:dataReceived]];
} else if ([[node name] isEqualToString:@"direct_message"]) {
NSError *error = NULL;
[nc postNotificationName:@"OTEDMDidFinishSending"
object:[node nodesForXPath:@".//recipientScreenName"
error:&error]];
} else if ([[node name] isEqualToString:@"status"]) {
[nc postNotificationName:@"OTEStatusDidFinishLoading"
object:node];
} else if ([[node name] isEqualToString:@"hash"]) {
NSError *error = NULL;
[nc postNotificationName:@"OTEErrorMsgDidFinishLoading"
object:[node nodesForXPath:@".//error"
error:&error]];
}
}
}
@end