forked from iridia/IRWebAPIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRWebAPIEngine.m
More file actions
221 lines (138 loc) · 7.2 KB
/
IRWebAPIEngine.m
File metadata and controls
221 lines (138 loc) · 7.2 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
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
//
// IRWebAPIEngine.m
// IRWebAPIKit
//
// Created by Evadne Wu on 11/19/10.
// Copyright 2010 Iridia Productions. All rights reserved.
//
#import <objc/runtime.h>
#import "IRWebAPIEngine.h"
#import "IRWebAPIRequestContext.h"
#import "IRWebAPIRequestOperation.h"
#import "IRWebAPIEngineContext.h"
@interface IRWebAPIEngine ()
@property (nonatomic, readwrite, retain) NSMutableArray *globalRequestPreTransformers;
@property (nonatomic, readwrite, retain) NSMutableDictionary *requestTransformers;
@property (nonatomic, readwrite, retain) NSMutableArray *globalRequestPostTransformers;
@property (nonatomic, readwrite, retain) NSMutableArray *globalResponsePreTransformers;
@property (nonatomic, readwrite, retain) NSMutableDictionary *responseTransformers;
@property (nonatomic, readwrite, retain) NSMutableArray *globalResponsePostTransformers;
- (IRWebAPIRequestContext *) requestContextByTransformingContext:(IRWebAPIRequestContext *)inContext forMethodNamed:(NSString *)inMethodName;
- (NSDictionary *) responseByTransformingResponse:(NSDictionary *)inResponse withRequestContext:(IRWebAPIRequestContext *)inRequestContext forMethodNamed:(NSString *)inMethodName;
@end
@implementation IRWebAPIEngine
@synthesize context = _context;
@synthesize queue = _queue;
@synthesize globalRequestPreTransformers = _globalRequestPreTransformers;
@synthesize requestTransformers = _requestTransformers;
@synthesize globalRequestPostTransformers = _globalRequestPostTransformers;
@synthesize globalResponsePreTransformers = _globalResponsePreTransformers;
@synthesize responseTransformers = _responseTransformers;
@synthesize globalResponsePostTransformers = _globalResponsePostTransformers;
- (id) initWithContext:(IRWebAPIEngineContext *)inContext {
self = [super init];
if (!self)
return nil;
_context = inContext;
_queue = [[NSOperationQueue alloc] init];
_queue.maxConcurrentOperationCount = 8;
_globalRequestPreTransformers = [NSMutableArray array];
_requestTransformers = [NSMutableDictionary dictionary];
_globalRequestPostTransformers = [NSMutableArray array];
_globalResponsePreTransformers = [NSMutableArray array];
_responseTransformers = [NSMutableDictionary dictionary];
_globalResponsePostTransformers = [NSMutableArray array];
return self;
}
- (id) init {
return [self initWithContext:nil];
}
- (IRWebAPIRequestOperation *) operationForMethod:(NSString *)method arguments:(NSDictionary *)arguments validator:(IRWebAPIResponseValidator)validator successBlock:(IRWebAPICallback)successBlock failureBlock:(IRWebAPICallback)failureBlock {
return [self operationForMethod:method arguments:arguments contextOverride:nil validator:validator successBlock:successBlock failureBlock:failureBlock];
}
- (IRWebAPIRequestOperation *) operationForMethod:(NSString *)method arguments:(NSDictionary *)arguments contextOverride:(void(^)(IRWebAPIRequestContext *))overrideBlock validator:(IRWebAPIResponseValidator)validator successBlock:(IRWebAPICallback)successBlock failureBlock:(IRWebAPICallback)failureBlock {
__weak IRWebAPIEngine *wSelf = self;
IRWebAPIRequestContext *baseContext = [IRWebAPIRequestContext new];
baseContext.baseURL = [self.context baseURLForMethodNamed:method];
baseContext.engineMethod = method;
[arguments enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[baseContext setValue:obj forQueryParam:key];
}];
if (overrideBlock)
overrideBlock(baseContext);
IRWebAPIRequestContext *finalizedContext = [self requestContextByTransformingContext:baseContext forMethodNamed:method];
IRWebAPIRequestOperation *operation = [[IRWebAPIRequestOperation alloc] initWithContext:finalizedContext];
__weak IRWebAPIRequestOperation *wOperation = operation;
[operation setCompletionBlock:^{
IRWebAPIRequestState state = wOperation.state;
IRWebAPIRequestContext *context = wOperation.context;
NSDictionary *response = (NSDictionary *)wOperation.result;
@try {
NSCParameterAssert((state == IRWebAPIRequestStateSucceeded) || (state == IRWebAPIRequestStateFailed));
NSCParameterAssert(!response || [response isKindOfClass:[NSDictionary class]]);
NSDictionary *transformedResponse = [wSelf responseByTransformingResponse:response withRequestContext:context forMethodNamed:method];
if (state == IRWebAPIRequestStateSucceeded) {
if ((validator != nil) && (!validator(transformedResponse, context))) {
if (failureBlock)
failureBlock(transformedResponse, context);
} else {
if (successBlock)
successBlock(transformedResponse, context);
}
} else {
if (failureBlock)
failureBlock(transformedResponse, context);
}
} @catch (NSException *exception) {
if (failureBlock)
failureBlock(nil, context);
}
}];
return operation;
}
- (NSMutableArray *) requestTransformersForMethodNamed:(NSString *)inMethodName {
NSMutableArray *returnedArray = nil;
@synchronized(self.requestTransformers) {
returnedArray = [self.requestTransformers objectForKey:inMethodName];
if (!returnedArray) {
returnedArray = [NSMutableArray array];
[self.requestTransformers setObject:returnedArray forKey:inMethodName];
}
}
return returnedArray;
}
- (NSMutableArray *) responseTransformersForMethodNamed:(NSString *)inMethodName {
NSMutableArray *returnedArray = nil;
@synchronized(self.responseTransformers) {
returnedArray = [self.responseTransformers objectForKey:inMethodName];
if (!returnedArray) {
returnedArray = [NSMutableArray array];
[self.responseTransformers setObject:returnedArray forKey:inMethodName];
}
}
return returnedArray;
}
- (IRWebAPIRequestContext *) requestContextByTransformingContext:(IRWebAPIRequestContext *)inContext forMethodNamed:(NSString *)inMethodName {
NSMutableArray *allTransformers = [NSMutableArray array];
[allTransformers addObjectsFromArray:self.globalRequestPreTransformers];
NSArray *methodSpecificTransformers = [self requestTransformersForMethodNamed:inMethodName];
if (methodSpecificTransformers) {
[allTransformers addObjectsFromArray:methodSpecificTransformers];
}
[allTransformers addObjectsFromArray:self.globalRequestPostTransformers];
IRWebAPIRequestContext *currentContext = inContext;
for (IRWebAPIRequestContextTransformer aTransformer in allTransformers)
currentContext = aTransformer(currentContext);
return currentContext;
}
- (NSDictionary *) responseByTransformingResponse:(NSDictionary *)inResponse withRequestContext:(IRWebAPIRequestContext *)inRequestContext forMethodNamed:(NSString *)inMethodName {
NSMutableArray *allTransformers = [NSMutableArray array];
[allTransformers addObjectsFromArray:self.globalResponsePreTransformers];
[allTransformers addObjectsFromArray:[self responseTransformersForMethodNamed:inMethodName]];
[allTransformers addObjectsFromArray:self.globalResponsePostTransformers];
NSDictionary *currentResponse = inResponse;
for (IRWebAPIResponseContextTransformer aTransformer in allTransformers)
currentResponse = aTransformer(currentResponse, inRequestContext);
return currentResponse;
}
@end