-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSLRESTAdapter.m
218 lines (178 loc) · 8.28 KB
/
SLRESTAdapter.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
/**
* @file SLRESTAdapter.m
*
* @author Michael Schoonmaker
* @copyright (c) 2013 StrongLoop. All rights reserved.
*/
#import "SLRESTAdapter.h"
#import "SLStreamParam.h"
#import "SLAFHTTPClient.h"
#import "SLAFJSONRequestOperation.h"
static NSString * const DEFAULT_DEV_BASE_URL = @"http://localhost:3001";
@interface SLRESTAdapter() {
SLAFHTTPClient *client;
}
@property (readwrite, nonatomic) BOOL connected;
- (void)requestWithPath:(NSString *)path
verb:(NSString *)verb
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
multipart:(BOOL)multipart
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure;
- (void)appendPartToMultiPartForm:(id <AFMultipartFormData>)formData
withParameters:(NSDictionary *)parameters;
@end
@implementation SLRESTAdapter
@synthesize connected;
- (instancetype)initWithURL:(NSURL *)url allowsInvalidSSLCertificate : (BOOL) allowsInvalidSSLCertificate {
self = [super initWithURL:url allowsInvalidSSLCertificate:allowsInvalidSSLCertificate];
if (self) {
self.contract = [SLRESTContract contract];
}
return self;
}
- (void)connectToURL:(NSURL *)url {
// Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
url = [url URLByAppendingPathComponent:@"/"];
}
client = [SLAFHTTPClient clientWithBaseURL:url];
client.allowsInvalidSSLCertificate = self.allowsInvalidSSLCertificate;
self.connected = YES;
client.parameterEncoding = AFJSONParameterEncoding;
[client registerHTTPOperationClass:[SLAFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
}
- (void)invokeStaticMethod:(NSString *)method
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
NSAssert(self.contract, @"Invalid contract.");
NSMutableDictionary *mutableParams = [NSMutableDictionary dictionaryWithDictionary:parameters];
NSString *verb = [self.contract verbForMethod:method];
NSString *path = [self.contract urlForMethod:method parameters:mutableParams];
BOOL multipart = [self.contract multipartForMethod:method];
[self requestWithPath:path
verb:verb
parameters:mutableParams
bodyParameters:bodyParameters
multipart:multipart
outputStream:outputStream
success:success
failure:failure];
}
- (void)invokeInstanceMethod:(NSString *)method
constructorParameters:(NSDictionary *)constructorParameters
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
// TODO(schoon) - Break out and document error description.
NSAssert(self.contract, @"Invalid contract.");
NSMutableDictionary *combinedParameters = [NSMutableDictionary dictionary];
[combinedParameters addEntriesFromDictionary:constructorParameters];
[combinedParameters addEntriesFromDictionary:parameters];
NSString *verb = [self.contract verbForMethod:method];
NSString *path = [self.contract urlForMethod:method parameters:combinedParameters];
BOOL multipart = [self.contract multipartForMethod:method];
[self requestWithPath:path
verb:verb
parameters:combinedParameters
bodyParameters:bodyParameters
multipart:multipart
outputStream:outputStream
success:success
failure:failure];
}
- (void)requestWithPath:(NSString *)path
verb:(NSString *)verb
parameters:(NSDictionary *)parameters
bodyParameters:(NSDictionary *)bodyParameters
multipart:(BOOL)multipart
outputStream:(NSOutputStream *)outputStream
success:(SLSuccessBlock)success
failure:(SLFailureBlock)failure {
NSAssert(self.connected, SLAdapterNotConnectedErrorDescription);
// Remove the leading / so that the path is treated as relative to the baseURL
if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1];
}
NSMutableURLRequest *request;
if (!multipart) {
if ([verb isEqualToString:@"GET"] || [verb isEqualToString:@"HEAD"] || [verb isEqualToString:@"DELETE"]) {
parameters = ([parameters count] > 0) ? parameters : nil;
request = [client requestWithMethod:verb path:path parameters:parameters];
} else {
client.parameterEncoding = AFJSONParameterEncoding;
request = [client requestWithMethod:verb path:path parameters:bodyParameters];
if (parameters) {
NSURL *url = [NSURL URLWithString:[[request.URL absoluteString]
stringByAppendingFormat:@"?%@",
SLAFQueryStringFromParametersWithEncoding(parameters,
client.stringEncoding)]];
[request setURL:url];
}
}
} else {
request = [client multipartFormRequestWithMethod:verb
path:path
parameters:parameters
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[self appendPartToMultiPartForm:formData
withParameters:parameters];
}];
}
SLAFHTTPRequestOperation *operation;
// Synchronize the block so that the invocations of client's [un]registerHTTPOperationClass:
// and HTTPRequestOperationWithRequest:success: methods become atomic.
@synchronized(self) {
if (outputStream != nil) {
// The following is needed to force the received binary payload always go to the stream
[client unregisterHTTPOperationClass:[SLAFJSONRequestOperation class]];
}
operation = [client HTTPRequestOperationWithRequest:request
success:^(SLAFHTTPRequestOperation *operation,
id responseObject) {
success(responseObject);
} failure:^(SLAFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
if (outputStream != nil) {
// Re-register the response handler class
[client registerHTTPOperationClass:[SLAFJSONRequestOperation class]];
operation.outputStream = outputStream;
}
}
[client enqueueHTTPRequestOperation:operation];
}
- (void)appendPartToMultiPartForm:(id <AFMultipartFormData>)formData
withParameters:(NSDictionary *)parameters {
for (id key in parameters) {
id value = parameters[key];
if ([value isKindOfClass:[SLStreamParam class]]) {
SLStreamParam *streamParam = (SLStreamParam *)value;
[formData appendPartWithInputStream:streamParam.inputStream
name:key
fileName:streamParam.fileName
length:streamParam.length
mimeType:streamParam.contentType];
} else {
NSLog(@"%s: Ignored non SLStreamParam parameter %@ specified for multipart form",
__FUNCTION__, [value class]);
}
}
}
- (NSString*)accessToken
{
return [client defaultValueForHeader:@"Authorization"];
}
- (void)setAccessToken:(NSString *)accessToken
{
[client setDefaultHeader:@"Authorization" value:accessToken];
}
@end