/
LiveOperationCore.m
executable file
·330 lines (277 loc) · 8.49 KB
/
LiveOperationCore.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
//
// LiveOperationCore.m
// Live SDK for iOS
//
// Copyright (c) 2011 Microsoft Corp. All rights reserved.
//
#import "LiveApiHelper.h"
#import "LiveAuthHelper.h"
#import "LiveConnectClientCore.h"
#import "LiveConnectionHelper.h"
#import "LiveConstants.h"
#import "LiveOperationCore.h"
#import "StringHelper.h"
#import "UrlHelper.h"
@class LiveConnectClientCore;
@implementation LiveOperationCore
@synthesize method = _method,
path = _path,
requestBody = _requestBody,
delegate = _delegate,
userState = _userState,
liveClient = _liveClient,
inputStream = _inputStream,
streamReader,
request,
publicOperation,
rawResult,
result,
connection,
httpResponse,
responseData,
completed,
httpError;
- (id) initWithMethod:(NSString *)method
path:(NSString *)path
requestBody:(NSData *)requestBody
delegate:(id)delegate
userState:(id)userState
liveClient:(LiveConnectClientCore *)liveClient
{
self = [super init];
if (self)
{
_method = [method copy];
_path = [path copy];
_requestBody = [requestBody retain];
_delegate = delegate;
_userState = [userState retain];
_liveClient = [liveClient retain];
httpError = nil;
completed = NO;
}
return self;
}
- (id) initWithMethod:(NSString *)method
path:(NSString *)path
inputStream:(NSInputStream *)inputStream
delegate:(id)delegate
userState:(id)userState
liveClient:(LiveConnectClientCore *)liveClient
{
self = [super init];
if (self)
{
_method = [method copy];
_path = [path copy];
_inputStream = [inputStream retain];
_delegate = delegate;
_userState = [userState retain];
_liveClient = [liveClient retain];
completed = NO;
}
return self;
}
- (void)dealloc
{
[_method release];
[_path release];
[_requestBody release];
[_userState release];
[_liveClient release];
[_inputStream release];
[streamReader release];
[request release];
[rawResult release];
[result release];
[connection release];
[responseData release];
[publicOperation release];
[httpResponse release];
[httpError release];
[super dealloc];
}
- (void) execute
{
[_liveClient refreshSessionWithDelegate:self
userState:nil];
}
- (void) authCompleted: (LiveConnectSessionStatus) status
session: (LiveConnectSession *) session
userState: (id) userState
{
[self sendRequest];
}
- (void) cancel
{
NSError *error = [LiveAuthHelper createAuthError:LIVE_ERROR_CODE_API_CANCELED
errorStr:LIVE_ERROR_CODE_S_REQUEST_CANCELED
description:LIVE_ERROR_DESC_API_CANCELED
innerError:nil];
[self operationFailed:error];
[self dismissCurrentRequest];
}
- (void) dismissCurrentRequest
{
[connection cancel];
}
- (NSURL *)requestUrl
{
return [LiveApiHelper buildAPIUrl:self.path
params:[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"true", LIVE_API_PARAM_SUPPRESS_RESPONSE_CODES,
@"true", LIVE_API_PARAM_SUPPRESS_REDIRECTS,
nil ]];
}
- (void) setRequestContentType
{
[request setValue:LIVE_API_HEADER_CONTENTTYPE_JSON
forHTTPHeaderField:LIVE_API_HEADER_CONTENTTYPE];
}
- (void) readInputStream
{
self.streamReader = [[[StreamReader alloc]initWithStream:_inputStream
delegate:self]
autorelease ];
[self.streamReader start];
}
- (void)streamReadingCompleted:(NSData *)data
{
self.requestBody = data;
[self sendRequest];
}
- (void)streamReadingFailed:(NSError *)error
{
[self operationFailed:error];
}
- (void) sendRequest
{
if (completed)
{
return;
}
if (_inputStream != nil && _requestBody == nil)
{
// We have a stream to read.
[self readInputStream];
return;
}
self.request = [NSMutableURLRequest requestWithURL:self.requestUrl
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:HTTP_REQUEST_TIMEOUT_INTERVAL];
[request setHTTPMethod:self.method];
if ([LiveAuthHelper isSessionValid:_liveClient.session])
{
[request setValue:[NSString stringWithFormat:@"bearer %@", self.liveClient.session.accessToken ]
forHTTPHeaderField:LIVE_API_HEADER_AUTHORIZATION];
}
// Set this header for SDK usage tracking purpose.
[request setValue: [LiveApiHelper getXHTTPLiveLibraryHeaderValue]
forHTTPHeaderField:LIVE_API_HEADER_X_HTTP_LIVE_LIBRARY];
if (self.requestBody != nil)
{
[self setRequestContentType];
[request setHTTPBody:self.requestBody];
}
self.connection = [LiveConnectionHelper createConnectionWithRequest:request delegate:self];
}
- (NSMutableData *)responseData
{
if (responseData == nil)
{
responseData = [[NSMutableData alloc] init];
}
return responseData;
}
#pragma mark methods on response handling
- (void) operationFailed:(NSError *)error
{
if (completed) {
return;
}
completed = YES;
if ([_delegate respondsToSelector:@selector(liveOperationFailed:operation:)])
{
[_delegate liveOperationFailed:error operation:publicOperation];
}
// LiveOperation was returned in the interface return. However, the app may not retain the object
// In order to keep it alive, we keep LiveOperationCore and LiveOperation in circular reference.
// After the event raised, we set this property to nil to break the circle, so that they are recycled.
self.publicOperation = nil;
}
- (void) operationCompleted
{
if (completed)
{
return;
}
NSString *textResponse;
NSDictionary *response;
NSError *error = nil;
[LiveApiHelper parseApiResponse:responseData
textResponse:&textResponse
response:&response
error:&error];
if (error == nil)
{
error = self.httpError;
}
if (error == nil)
{
self.rawResult = textResponse ;
self.result = response;
if ([_delegate respondsToSelector:@selector(liveOperationSucceeded:)])
{
[_delegate liveOperationSucceeded:self.publicOperation];
}
// LiveOperation was returned in the interface return. However, the app may not retain the object
// In order to keep it alive, we keep LiveOperationCore and LiveOperation in circular reference.
// After the event raised, we set this property to nil to break the circle, so that they are recycled.
self.publicOperation = nil;
}
else
{
[self operationFailed:error];
}
completed = YES;
self.responseData = nil;
}
- (void) operationReceivedData:(NSData *)data
{
[self.responseData appendData:data];
}
#pragma mark NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
self.httpResponse = (NSHTTPURLResponse *)response;
if ((httpResponse.statusCode / 100) != 2)
{
NSString *message = [NSString stringWithFormat:@"HTTP error %zd", (ssize_t)httpResponse.statusCode];
self.httpError = [LiveApiHelper createAPIError:LIVE_ERROR_CODE_S_REQUEST_FAILED
message:message
innerError:nil];
}
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[self operationReceivedData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse
{
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self operationCompleted];
self.connection = nil;
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
self.connection = nil;
[self operationFailed:error];
}
@end