-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDrupal8RESTSessionManager.m
443 lines (265 loc) · 16.9 KB
/
Drupal8RESTSessionManager.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//
// Drupal8RESTSessionManager.m
// TipsAndTricksAFNetworking
//
// Created by Vivek Pandya on 7/16/14.
/*
The MIT License (MIT)
Copyright (c) 2014 Vivek Pandya. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "Drupal8RESTSessionManager.h"
@implementation Drupal8RESTSessionManager
-(AFHTTPSessionManager *)sessionManager{
if (!_sessionManager ) {
_sessionManager = [AFHTTPSessionManager manager];// A singleton AFHTTPSessionManager object will be returned.
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
_sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
_sessionManager.responseSerializer.acceptableContentTypes = [_sessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/hal+json"];
}
return _sessionManager;
}
#pragma mark configuration methods implementation for SessionManagerObject
-(void)setValue:(NSString *)value forHTTPRequestHeader:(NSString *)header{
[self.sessionManager.requestSerializer setValue:value forHTTPHeaderField:header];
}
-(void)clearAuthorizationHeader{
[self.sessionManager.requestSerializer clearAuthorizationHeader];
}
#pragma mark CRUD methods for Entity - implementation
-(NSURLSessionDataTask *)POSTEntity:(NSString *)url
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *,id resopnseObject))success
failure:(void (^)(NSURLSessionDataTask *, NSError *error))failure{
NSURLSessionDataTask *postEntityTask = [self.sessionManager POST:url
parameters:parameters
success:success
failure:failure];
return postEntityTask;
}
-(NSURLSessionDataTask *)GETEntity:(NSString *)url
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *,id resopnseObject))success
failure:(void (^)(NSURLSessionDataTask *, NSError *error))failure{
NSURLSessionDataTask *getEntityTask = [self.sessionManager GET:url
parameters:parameters
success:success
failure:failure];
return getEntityTask;
}
-(NSURLSessionDataTask *)PATCHEntity:(NSString *)url
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *,id resopnseObject))success
failure:(void (^)(NSURLSessionDataTask *, NSError *error))failure{
NSURLSessionDataTask *patchEntityTask = [self.sessionManager PATCH:url
parameters:parameters
success:success
failure:failure];
return patchEntityTask;
}
-(NSURLSessionDataTask *)DELETEEntity:(NSString *)url
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *,id resopnseObject))success
failure:(void (^)(NSURLSessionDataTask *, NSError *error))failure{
NSURLSessionDataTask *deleteEntityTask = [self.sessionManager DELETE:url
parameters:parameters
success:success
failure:failure];
return deleteEntityTask;
}
#pragma mark CRUD methods for node - implementation
-(NSURLSessionDataTask *)POSTNode:(NSString *)url
bundleType:(NSString *)bundleType
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
[self setValue:@"application/hal+json" forHTTPRequestHeader:@"Content-Type"];
NSString *nodeRESTEndpointURL = [NSString stringWithFormat:@"%@/entity/node",url];
NSString *linkType = [NSString stringWithFormat:@"%@/rest/type/node/%@",url,bundleType];
NSMutableDictionary *requestBody = [(NSDictionary *)parameters mutableCopy];
[requestBody addEntriesFromDictionary:@{@"_links":@{@"type":@{@"href":linkType}}}];
NSURLSessionDataTask *postNodeTask = [self POSTEntity:nodeRESTEndpointURL
parameters:requestBody
success:success
failure:failure];
return postNodeTask;
}
-(NSURLSessionDataTask *)DELETENode:(NSString *)url
nodeId:(NSString *)nid
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *deleteRESTEndpointURL = [NSString stringWithFormat:@"%@/node/%@",url,nid];
NSURLSessionDataTask *deleteNodeTask = [self DELETEEntity:deleteRESTEndpointURL
parameters:nil
success:success
failure:failure];
return deleteNodeTask;
}
-(NSURLSessionDataTask *)GETNode:(NSString *)url
nodeId:(NSString *)nid
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *getRESTEndpointURL = [NSString stringWithFormat:@"%@/node/%@",url,nid];
NSURLSessionDataTask *getNodeTask = [self GETEntity:getRESTEndpointURL parameters:nil success:success failure:failure];
return getNodeTask;
}
-(NSURLSessionDataTask *)PATCHNode:(NSString *)url
bundleType:(NSString *)bundleType
nodeId:(NSString *)nid
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
[self setValue:@"application/hal+json" forHTTPRequestHeader:@"Content-Type"];
NSString *nodeRESTEndpointURL = [NSString stringWithFormat:@"%@/node/%@",url,nid];
NSString *linkType = [NSString stringWithFormat:@"%@/rest/type/node/%@",url,bundleType];
NSMutableDictionary *requestBody = [(NSDictionary *)parameters mutableCopy];
[requestBody addEntriesFromDictionary:@{@"_links":@{@"type":@{@"href":linkType}}}];
NSURLSessionDataTask *patchNodeTask = [self PATCHEntity:nodeRESTEndpointURL
parameters:requestBody
success:success
failure:failure];
return patchNodeTask;
}
#pragma mark GET method for View - implementation
-(NSURLSessionDataTask *)GETView:(NSString *)url
viewName:(NSString *)viewName
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *viewURL = [NSString stringWithFormat:@"%@/%@",url,viewName];
NSURLSessionDataTask *getViewDataTask = [self.sessionManager GET:viewURL
parameters:parameters
success:success failure:failure];
return getViewDataTask;
}
#pragma mark CRUD methods for User - implementation
-(NSURLSessionDataTask *)GETUser:(NSString *)url
userId:(NSString *)uid
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *getRESTEndpointURL = [NSString stringWithFormat:@"%@/user/%@",url,uid];
NSURLSessionDataTask *getUserTask = [self GETEntity:getRESTEndpointURL
parameters:nil
success:success
failure:failure];
return getUserTask;
}
-(NSURLSessionDataTask *)DELETEUser:(NSString *)url
userId:(NSString *)uid
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *deleteRESTEndpointURL = [NSString stringWithFormat:@"%@/user/%@",url,uid];
NSURLSessionDataTask *deleteUserTask =[self DELETEEntity:deleteRESTEndpointURL
parameters:nil
success:success
failure:failure];
return deleteUserTask;
}
-(NSURLSessionDataTask *)POSTUser:(NSString *)url
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
[self setValue:@"application/hal+json" forHTTPRequestHeader:@"Content-Type"];
NSString *userRESTEndpointURL = [NSString stringWithFormat:@"%@/entity/user",url];
NSString *linkType = [NSString stringWithFormat:@"%@/rest/type/user/user",url];
NSMutableDictionary *requestBody = [(NSDictionary *)parameters mutableCopy];
[requestBody addEntriesFromDictionary:@{@"_links":@{@"type":@{@"href":linkType}}}];
NSURLSessionDataTask *postUserTask = [self POSTEntity:userRESTEndpointURL
parameters:requestBody
success:success
failure:failure];
return postUserTask;
}
-(NSURLSessionDataTask *)PATCHUser:(NSString *)url
userId:(NSString *)uid
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
[self setValue:@"application/hal+json" forHTTPRequestHeader:@"Content-Type"];
NSString *userRESTEndpointURL = [NSString stringWithFormat:@"%@/user/%@",url,uid];
NSString *linkType = [NSString stringWithFormat:@"%@/rest/type/user/user",url];
NSMutableDictionary *requestBody = [(NSDictionary *)parameters mutableCopy];
[requestBody addEntriesFromDictionary:@{@"_links":@{@"type":@{@"href":linkType}}}];
NSURLSessionDataTask *patchUserTask = [self PATCHEntity:userRESTEndpointURL
parameters:requestBody
success:success
failure:failure];
return patchUserTask;
}
#pragma mark CRUD methods for comments - implementataion
-(NSURLSessionDataTask *)GETCommment:(NSString *)url
commentId:(NSString *)cid
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *getRESTEndpoint = [NSString stringWithFormat:@"%@/comment/%@",url,cid];
NSURLSessionDataTask *getCommentTask = [self GETEntity:getRESTEndpoint
parameters:nil
success:success
failure:failure];
return getCommentTask;
}
-(NSURLSessionDataTask *)PATCHCommment:(NSString *)url
commentId:(NSString *)cid
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
// NSString *relationKey = [NSString stringWithFormat:@"%@/rest/relation/comment/node__comment/entity_id",url];
// NSString *relationValue = [NSString stringWithFormat:@"%@/node/%@",url,entityID];
[self setValue:@"application/hal+json" forHTTPRequestHeader:@"Content-Type"];
NSString *patchRESTEndpoint = [NSString stringWithFormat:@"%@/comment/%@",url,cid];
NSString *linkType = [NSString stringWithFormat:@"%@/rest/type/comment/node__comment",url];
NSMutableDictionary *requestBody = [(NSDictionary *)parameters mutableCopy];
[requestBody addEntriesFromDictionary:@{@"_links":@{@"type":@{@"href":linkType}}}];
NSURLSessionDataTask *patchCommentTask = [self PATCHEntity:patchRESTEndpoint
parameters:requestBody
success:success
failure:failure];
return patchCommentTask;
}
-(NSURLSessionDataTask *)POSTComment:(NSString *)url
targetEntityId:(NSString *)entityID
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
[self setValue:@"application/hal+json" forHTTPRequestHeader:@"Content-Type"];
NSString *postRESTEndpoint = [NSString stringWithFormat:@"%@/entity/comment",url];
NSString *linkType = [NSString stringWithFormat:@"%@/rest/type/comment/node__comment",url];
NSString *relationKey = [NSString stringWithFormat:@"%@/rest/relation/comment/node__comment/entity_id",url];
NSString *relationValue = [NSString stringWithFormat:@"%@/node/%@",url,entityID];
NSMutableDictionary *requestBody = [(NSDictionary *)parameters mutableCopy];
[requestBody addEntriesFromDictionary:@{@"_links":@{@"type":@{@"href":linkType},relationKey:relationValue},@"entity_id":@[@{@"target_id":entityID,@"revision_id":@""}]}];
NSURLSessionDataTask *postCommentTask = [self POSTEntity:postRESTEndpoint
parameters:requestBody
success:success
failure:failure];
return postCommentTask;
}
-(NSURLSessionDataTask *)DELETEComment:(NSString *)url
commentId:(NSString *)cid
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
NSString *deleteRESTEndpoint = [NSString stringWithFormat:@"%@/comment/%@",url,cid];
NSURLSessionDataTask *deleteCommentTask = [self DELETEEntity:deleteRESTEndpoint
parameters:nil
success:success
failure:failure];
return deleteCommentTask;
}
@end