-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJSONWebCommand.m
More file actions
186 lines (148 loc) · 5.12 KB
/
JSONWebCommand.m
File metadata and controls
186 lines (148 loc) · 5.12 KB
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
//
// JSONWebCommand.m
//
// Created by Jiva DeVoe on 9/22/10.
// Copyright (c) 2010 Random Ideas, LLC. All rights reserved.
//
#import "JSONWebCommand.h"
#import "NSMutableURLRequest+Posts.h"
@interface JSONWebCommand ()
@property (nonatomic) int statusCode;
@end
@implementation JSONWebCommand
@synthesize request;
@synthesize connection;
@synthesize data;
@synthesize resultDict;
@synthesize commandStatus;
@synthesize completionBlock;
@synthesize failureBlock;
@synthesize finallyBlock;
@synthesize showsNetworkActivity;
@synthesize threadedCompletionBlock;
@synthesize parseOpQueue;
-(id)initWithRequest:(NSMutableURLRequest *)inRequest;
{
if((self = [super init]))
{
[self setRequest:inRequest];
[self setShowsNetworkActivity:YES];
parseOpQueue = [[NSOperationQueue alloc] init];
[self.parseOpQueue setMaxConcurrentOperationCount:1];
}
return self;
}
-(id)initForPostToUrl:(NSString *)inUrlString withParams:(NSDictionary *)inParams
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:inParams options:0 error:nil];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:inUrlString];
NSMutableURLRequest *req = [NSMutableURLRequest postRequestForURL:url withJSON:json];
return [self initWithRequest:req];
}
-(void)start;
{
assert(!(completionBlock && threadedCompletionBlock)); // only one or the other should be set, never both!
[connection cancel];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[self setData:[NSMutableData data]];
[connection start];
}
-(void)cancel;
{
[connection cancel];
[self setConnection:nil];
if(finallyBlock)
finallyBlock();
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
self.statusCode = [resp statusCode];
}
-(void)finishLoadingResult
{
[self setConnection:nil];
if(finallyBlock)
finallyBlock();
}
-(void)handleSuccessfulResult:(NSDictionary *)inResult
{
if(threadedCompletionBlock)
{
NSOperation *completionOp = [NSBlockOperation blockOperationWithBlock:^
{
self.threadedCompletionBlock(inResult);
[self performSelectorOnMainThread:@selector(finishLoadingResult) withObject:nil waitUntilDone:YES];
}];
[[self parseOpQueue] addOperation:completionOp];
return;
}
else if(completionBlock)
completionBlock(inResult);
[self finishLoadingResult];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(self.statusCode >= 200 && self.statusCode < 300)
{
NSOperation *parseOperation = [NSBlockOperation blockOperationWithBlock:^
{
NSError *error = nil;
[self setResultDict:[NSJSONSerialization JSONObjectWithData:self.data options:0 error:&error]];
[[NSOperationQueue mainQueue] addOperationWithBlock:^
{
if(self.resultDict)
{
[self handleSuccessfulResult:self.resultDict];
}
else
{
if(failureBlock)
failureBlock(error);
[self finishLoadingResult];
}
}];
}];
[[self parseOpQueue] addOperation:parseOperation];
return;
}
else
{
NSError *error = nil;
[self setResultDict:[NSJSONSerialization JSONObjectWithData:self.data options:0 error:&error]];
if(resultDict)
{
NSError *serverError = [NSError errorWithDomain:@"com.randomideas.shibui.network" code:self.statusCode userInfo:resultDict];
if(failureBlock)
failureBlock(serverError);
}
else
{
NSString *errorMsg = [NSHTTPURLResponse localizedStringForStatusCode:self.statusCode];
NSDictionary *dict = [NSDictionary dictionaryWithObject:errorMsg forKey:NSLocalizedDescriptionKey];
NSError *serverError = [NSError errorWithDomain:@"com.randomideas.shibui.network" code:self.statusCode userInfo:dict];
if(failureBlock)
failureBlock(serverError);
}
[self finishLoadingResult];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if(failureBlock)
failureBlock(error);
[self setConnection:nil];
if(finallyBlock)
finallyBlock();
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)inData
{
[data appendData:inData];
}
@end