forked from dav/stackmob-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSMDataStore.m
491 lines (411 loc) · 30 KB
/
SMDataStore.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright 2012-2013 StackMob
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "SMDataStore.h"
#import "SMDataStore+Protected.h"
#import "SMQuery.h"
#import "AFNetworking.h"
#import "SMJSONRequestOperation.h"
#import "SMError.h"
#import "SMOAuth2Client.h"
#import "NSDictionary+AtomicCounter.h"
#import "SMRequestOptions.h"
#import "SMClient.h"
#import "SMUserSession.h"
#import "SMCustomCodeRequest.h"
#import "SMResponseBlocks.h"
@interface SMDataStore ()
@property(nonatomic, readwrite, copy) NSString *apiVersion;
@end
@implementation SMDataStore
@synthesize apiVersion = _SM_apiVersion;
@synthesize session = _SM_session;
- (id)initWithAPIVersion:(NSString *)apiVersion session:(SMUserSession *)session
{
self = [super init];
if (self) {
self.apiVersion = apiVersion;
self.session = session;
}
return self;
}
- (void)createObject:(NSDictionary *)object inSchema:(NSString *)schema onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreFailureBlock)failureBlock
{
[self createObject:object inSchema:schema options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)createObject:(NSDictionary *)object inSchema:(NSString *)schema options:(SMRequestOptions *)options onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreFailureBlock)failureBlock
{
[self createObject:object inSchema:schema options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)createObject:(NSDictionary *)object
inSchema:(NSString *)schema
options:(SMRequestOptions *)options
successCallbackQueue:(dispatch_queue_t)successCallbackQueue
failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue
onSuccess:(SMDataStoreSuccessBlock)successBlock
onFailure:(SMDataStoreFailureBlock)failureBlock
{
if (object == nil || schema == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, object, schema);
}
} else {
NSString *theSchema = schema;
if ([schema rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]].location == NSNotFound) {
// lowercase the schema for StackMob
theSchema = [theSchema lowercaseString];
}
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"POST" path:theSchema parameters:object];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForSchema:schema withSuccessBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObject:object ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (void)createObjects:(NSArray *)objects
inSchema:(NSString *)schema
onSuccess:(SMDataStoreBulkSuccessBlock)successBlock
onFailure:(SMDataStoreBulkFailureBlock)failureBlock
{
[self createObjects:objects inSchema:schema options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)createObjects:(NSArray *)objects
inSchema:(NSString *)schema
options:(SMRequestOptions *)options
onSuccess:(SMDataStoreBulkSuccessBlock)successBlock
onFailure:(SMDataStoreBulkFailureBlock)failureBlock
{
[self createObjects:objects inSchema:schema options:[SMRequestOptions options] successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)createObjects:(NSArray *)objects
inSchema:(NSString *)schema
options:(SMRequestOptions *)options
successCallbackQueue:(dispatch_queue_t)successCallbackQueue
failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue
onSuccess:(SMDataStoreBulkSuccessBlock)successBlock
onFailure:(SMDataStoreBulkFailureBlock)failureBlock
{
if (objects == nil || schema == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objects, schema);
}
} else {
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"POST" path:schema parameters:nil];
if (![NSJSONSerialization isValidJSONObject:objects]) {
NSError *jsonError = nil;
if (jsonError) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objects, schema);
}
}
return;
}
NSError *jsonError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:objects options:0 error:&jsonError];
if (jsonError) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objects, schema);
}
return;
}
[request setHTTPBody:jsonData];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForBulkObjectsOfSchema:schema successBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForBulkObjects:objects ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (void)readObjectWithId:(NSString *)objectId
inSchema:(NSString *)schema
onSuccess:(SMDataStoreSuccessBlock)successBlock
onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self readObjectWithId:objectId inSchema:schema options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)readObjectWithId:(NSString *)objectId inSchema:(NSString *)schema options:(SMRequestOptions *)options onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self readObjectWithId:objectId inSchema:schema options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)readObjectWithId:(NSString *)objectId inSchema:(NSString *)schema options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self readObjectWithId:objectId inSchema:schema parameters:nil options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
}
- (void)updateObjectWithId:(NSString *)objectId inSchema:(NSString *)schema update:(NSDictionary *)updatedFields onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreFailureBlock)failureBlock
{
[self updateObjectWithId:objectId inSchema:schema update:updatedFields options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)updateObjectWithId:(NSString *)objectId inSchema:(NSString *)schema update:(NSDictionary *)updatedFields options:(SMRequestOptions *)options onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreFailureBlock)failureBlock
{
[self updateObjectWithId:objectId inSchema:schema update:updatedFields options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)updateObjectWithId:(NSString *)objectId inSchema:(NSString *)schema update:(NSDictionary *)updatedFields options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreFailureBlock)failureBlock
{
if (objectId == nil || schema == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, updatedFields, schema);
}
} else {
NSString *path = [[schema lowercaseString] stringByAppendingPathComponent:[self URLEncodedStringFromValue:objectId]];
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"PUT" path:path parameters:updatedFields];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForSchema:schema withSuccessBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObject:updatedFields ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (void)updateAtomicCounterWithId:(NSString *)objectId
field:(NSString *)field
inSchema:(NSString *)schema
by:(int)increment
onSuccess:(SMDataStoreSuccessBlock)successBlock
onFailure:(SMDataStoreFailureBlock)failureBlock
{
[self updateAtomicCounterWithId:objectId field:field inSchema:schema by:increment options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)updateAtomicCounterWithId:(NSString *)objectId
field:(NSString *)field
inSchema:(NSString *)schema
by:(int)increment
options:(SMRequestOptions *)options
onSuccess:(SMDataStoreSuccessBlock)successBlock
onFailure:(SMDataStoreFailureBlock)failureBlock
{
[self updateAtomicCounterWithId:objectId field:field inSchema:schema by:increment options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)updateAtomicCounterWithId:(NSString *)objectId field:(NSString *)field inSchema:(NSString *)schema by:(int)increment options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreFailureBlock)failureBlock
{
NSDictionary *args = [[NSDictionary dictionary] dictionaryByAppendingCounterUpdateForField:field by:increment];
[self updateObjectWithId:objectId inSchema:schema update:args options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
}
- (void)createAndAppendRelatedObjects:(NSArray *)objects toObjectWithId:(NSString *)objectId inSchema:(NSString *)schema relatedField:(NSString *)field onSuccess:(SMDataStoreBulkSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self createAndAppendRelatedObjects:objects toObjectWithId:objectId inSchema:schema relatedField:field options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)createAndAppendRelatedObjects:(NSArray *)objects toObjectWithId:(NSString *)objectId inSchema:(NSString *)schema relatedField:(NSString *)field options:(SMRequestOptions *)options onSuccess:(SMDataStoreBulkSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self createAndAppendRelatedObjects:objects toObjectWithId:objectId inSchema:schema relatedField:field options:[SMRequestOptions options] successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)createAndAppendRelatedObjects:(NSArray *)objects toObjectWithId:(NSString *)objectId inSchema:(NSString *)schema relatedField:(NSString *)field options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreBulkSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
if (objectId == nil || schema == nil || field == nil || objects == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
} else {
NSString *path = [[[schema lowercaseString] stringByAppendingPathComponent:[self URLEncodedStringFromValue:objectId]] stringByAppendingPathComponent:field];
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"POST" path:path parameters:nil];
if (![NSJSONSerialization isValidJSONObject:objects]) {
NSError *jsonError = nil;
if (jsonError) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
}
return;
}
NSError *jsonError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:objects options:0 error:&jsonError];
if (jsonError) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
return;
}
[request setHTTPBody:jsonData];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForBulkObjectsOfSchema:schema successBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObjectId:objectId ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (void)appendObjects:(NSArray *)objects toObjectWithId:(NSString *)objectId inSchema:(NSString *)schema field:(NSString *)field onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self appendObjects:objects toObjectWithId:objectId inSchema:schema field:field options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)appendObjects:(NSArray *)objects toObjectWithId:(NSString *)objectId inSchema:(NSString *)schema field:(NSString *)field options:(SMRequestOptions *)options onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self appendObjects:objects toObjectWithId:objectId inSchema:schema field:field options:[SMRequestOptions options] successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)appendObjects:(NSArray *)objects toObjectWithId:(NSString *)objectId inSchema:(NSString *)schema field:(NSString *)field options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
if (objectId == nil || schema == nil || field == nil || objects == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
} else {
NSString *path = [[[schema lowercaseString] stringByAppendingPathComponent:[self URLEncodedStringFromValue:objectId]] stringByAppendingPathComponent:field];
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"PUT" path:path parameters:nil];
if (![NSJSONSerialization isValidJSONObject:objects]) {
NSError *jsonError = nil;
if (jsonError) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
}
return;
}
NSError *jsonError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:objects options:0 error:&jsonError];
if (jsonError) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
return;
}
[request setHTTPBody:jsonData];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForSchema:schema withSuccessBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObjectId:objectId ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (void)deleteObjects:(NSArray *)objects fromObjectWithId:(NSString *)objectId inSchema:(NSString *)schema field:(NSString *)field cascadeDelete:(BOOL)cascadeDelete onSuccess:(SMSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self deleteObjects:objects fromObjectWithId:objectId inSchema:schema field:field cascadeDelete:cascadeDelete options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)deleteObjects:(NSArray *)objects fromObjectWithId:(NSString *)objectId inSchema:(NSString *)schema field:(NSString *)field cascadeDelete:(BOOL)cascadeDelete options:(SMRequestOptions *)options onSuccess:(SMSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self deleteObjects:objects fromObjectWithId:objectId inSchema:schema field:field cascadeDelete:cascadeDelete options:[SMRequestOptions options] successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)deleteObjects:(NSArray *)objects fromObjectWithId:(NSString *)objectId inSchema:(NSString *)schema field:(NSString *)field cascadeDelete:(BOOL)cascadeDelete options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
if (objectId == nil || schema == nil || field == nil || objects == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
} else {
NSString *objectsString = [objects componentsJoinedByString:@","];
NSString *path = [[[[schema lowercaseString] stringByAppendingPathComponent:[self URLEncodedStringFromValue:objectId]] stringByAppendingPathComponent:field] stringByAppendingPathComponent:objectsString];
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"DELETE" path:path parameters:nil];
if (cascadeDelete) {
[options setValue:@"true" forHeaderKey:@"X-StackMob-CascadeDelete"];
} else {
[options setValue:@"false" forHeaderKey:@"X-StackMob-CascadeDelete"];
}
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForSuccessBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObjectId:objectId ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (void)deleteObjectId:(NSString *)objectId inSchema:(NSString *)schema onSuccess:(SMDataStoreObjectIdSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self deleteObjectId:objectId inSchema:schema options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)deleteObjectId:(NSString *)objectId inSchema:(NSString *)schema options:(SMRequestOptions *)options onSuccess:(SMDataStoreObjectIdSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
[self deleteObjectId:objectId inSchema:schema options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)deleteObjectId:(NSString *)objectId inSchema:(NSString *)schema options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreObjectIdSuccessBlock)successBlock onFailure:(SMDataStoreObjectIdFailureBlock)failureBlock
{
if (objectId == nil || schema == nil) {
if (failureBlock) {
NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
failureBlock(error, objectId, schema);
}
} else {
NSString *path = [[schema lowercaseString] stringByAppendingPathComponent:[self URLEncodedStringFromValue:objectId]];
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"DELETE" path:path parameters:nil];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForObjectId:objectId ofSchema:schema withSuccessBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObjectId:objectId ofSchema:schema withFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
}
- (NSMutableURLRequest *)requestFromQuery:(SMQuery *)query options:(SMRequestOptions *)options
{
NSDictionary *requestHeaders = [query requestHeaders];
NSDictionary *requestParameters = [query requestParameters];
NSString *requestPath = [query schemaName];
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] requestWithMethod:@"GET" path:requestPath parameters:requestParameters];
[requestHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[request setValue:(NSString *)obj forHTTPHeaderField:(NSString *)key];
}];
return request;
}
- (void)performQuery:(SMQuery *)query onSuccess:(SMResultsSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock
{
[self performQuery:query options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)performQuery:(SMQuery *)query options:(SMRequestOptions *)options onSuccess:(SMResultsSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock
{
[self performQuery:query options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)performQuery:(SMQuery *)query options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMResultsSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock
{
NSMutableURLRequest *request = [self requestFromQuery:query options:options];
SMFullResponseSuccessBlock urlSuccessBlock = [self SMFullResponseSuccessBlockForQuerySuccessBlock:successBlock];
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
- (void)performCount:(SMQuery *)query onSuccess:(SMCountSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock
{
[self performCount:query options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)performCount:(SMQuery *)query options:(SMRequestOptions *)options onSuccess:(SMCountSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock
{
[self performCount:query options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)performCount:(SMQuery *)query options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMCountSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock
{
SMQuery *countQuery = [[SMQuery alloc] initWithSchema:query.schemaName];
countQuery.requestParameters = query.requestParameters;
countQuery.requestHeaders = [query.requestHeaders copy];
[countQuery fromIndex:0 toIndex:0];
NSMutableURLRequest *request = [self requestFromQuery:countQuery options:options];
SMFullResponseSuccessBlock urlSuccessBlock = ^(NSURLRequest *successRequest, NSHTTPURLResponse *response, id JSON) {
NSString *rangeHeader = [response.allHeaderFields valueForKey:@"Content-Range"];
//No range header means we've got all the results right here (1 or 0)
int count = [self countFromRangeHeader:rangeHeader results:JSON];
if (count < 0) {
if (failureBlock) {
failureBlock([NSError errorWithDomain:SMErrorDomain code:SMErrorNoCountAvailable userInfo:nil]);
}
} else {
if (successBlock) {
successBlock([NSNumber numberWithInt:count]);
}
}
};
SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForFailureBlock:failureBlock];
[self queueRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
}
- (void)performCustomCodeRequest:(SMCustomCodeRequest *)customCodeRequest onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
[self performCustomCodeRequest:customCodeRequest options:[SMRequestOptions options] onSuccess:successBlock onFailure:failureBlock];
}
- (void)performCustomCodeRequest:(SMCustomCodeRequest *)customCodeRequest options:(SMRequestOptions *)options onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
[self performCustomCodeRequest:customCodeRequest options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)performCustomCodeRequest:(SMCustomCodeRequest *)customCodeRequest options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
NSMutableURLRequest *request = [[self.session oauthClientWithHTTPS:options.isSecure] customCodeRequest:customCodeRequest options:options];
[self queueCustomCodeRequest:request customCodeRequestInstance:customCodeRequest options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
}
- (void)retryCustomCodeRequest:(NSURLRequest *)request options:(SMRequestOptions *)options onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
[self retryCustomCodeRequest:request options:options successCallbackQueue:dispatch_get_main_queue() failureCallbackQueue:dispatch_get_main_queue() onSuccess:successBlock onFailure:failureBlock];
}
- (void)retryCustomCodeRequest:(NSURLRequest *)request options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
[self queueRequest:[self.session signRequest:request] options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
}
@end