Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 569 lines (505 sloc) 31.85 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 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/*
* 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+Protected.h"
#import "SMError.h"
#import "SMJSONRequestOperation.h"
#import "SMRequestOptions.h"
#import "SMNetworkReachability.h"
#import "SMCustomCodeRequest.h"
#import "SMHTTPRequestOperation.h"
#import "AFHTTPRequestOperation+RemoveContentType.h"

#define SM_VENDOR_SPECIFIC_JSON @"application/vnd.stackmob+json"
#define SM_JSON @"application/json"
#define SM_TEXT_PLAIN @"text/plain"
#define SM_OCTET_STREAM @"application/octet-stream"

@implementation SMDataStore (SpecialCondition)

- (NSError *)errorFromResponse:(NSHTTPURLResponse *)response JSON:(id)JSON
{
    return [NSError errorWithDomain:HTTPErrorDomain code:response.statusCode userInfo:JSON];
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForSchema:(NSString *)schema withSuccessBlock:(SMDataStoreSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            successBlock(JSON, schema);
        }
    };
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForObjectId:(NSString *)objectId ofSchema:(NSString *)schema withSuccessBlock:(SMDataStoreObjectIdSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            successBlock(objectId, schema);
        }
    };
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForSuccessBlock:(SMSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            successBlock();
        }
    };
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForResultSuccessBlock:(SMResultSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            successBlock(JSON);
        }
    };
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForResultsSuccessBlock:(SMResultsSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            successBlock(JSON);
        }
    };
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForQuerySuccessBlock:(SMResultsSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            successBlock((NSArray *)JSON);
        }
    };
}

- (SMFullResponseSuccessBlock)SMFullResponseSuccessBlockForBulkObjectsOfSchema:(NSString *)schema successBlock:(SMDataStoreBulkSuccessBlock)successBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    {
        if (successBlock) {
            NSDictionary *jsonDict = (NSDictionary *)JSON;
            successBlock([jsonDict objectForKey:@"succeeded"], [jsonDict objectForKey:@"failed"], schema);
        }
    };
}

- (SMFullResponseFailureBlock)SMFullResponseFailureBlockForBulkObjects:(NSArray *)objects ofSchema:(NSString *)schema withFailureBlock:(SMDataStoreBulkFailureBlock)failureBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
    {
        if (failureBlock) {
            response == nil ? failureBlock(error, objects, schema) : failureBlock([self errorFromResponse:response JSON:JSON], objects, schema);
        }
    };
}

- (SMFullResponseFailureBlock)SMFullResponseFailureBlockForObject:(NSDictionary *)object ofSchema:(NSString *)schema withFailureBlock:(SMDataStoreFailureBlock)failureBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
    {
        if (failureBlock) {
            response == nil ? failureBlock(error, object, schema) : failureBlock([self errorFromResponse:response JSON:JSON], object, schema);
        }
    };
}

- (SMFullResponseFailureBlock)SMFullResponseFailureBlockForObjectId:(NSString *)objectId ofSchema:(NSString *)schema withFailureBlock:(SMDataStoreObjectIdFailureBlock)failureBlock;
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
    {
        if (failureBlock) {
            response == nil ? failureBlock(error, objectId, schema) : failureBlock([self errorFromResponse:response JSON:JSON], objectId, schema);
        }
    };
}

- (SMFullResponseFailureBlock)SMFullResponseFailureBlockForFailureBlock:(SMFailureBlock)failureBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
    {
        if (failureBlock) {
            response == nil ? failureBlock(error) : failureBlock([self errorFromResponse:response JSON:JSON]);
        }
    };
}

- (SMFullResponseFailureBlock)SMFullResponseFailureBlockForObject:(NSDictionary *)object options:(SMRequestOptions *)options originalSuccessBlock:(SMResultSuccessBlock)originalSuccessBlock coreDataSaveFailureBlock:(SMCoreDataSaveFailureBlock)failureBlock
{
    return ^void(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
    {
        if (failureBlock) {
            response == nil ? failureBlock(request, error, object, options, originalSuccessBlock) : failureBlock(request, [self errorFromResponse:response JSON:JSON], object, options, originalSuccessBlock);
        }
    };
}

- (int)countFromRangeHeader:(NSString *)rangeHeader results:(NSArray *)results
{
    if (rangeHeader == nil) {
        //No range header means we've got all the results right here (1 or 0)
        return (int)[results count];
    } else {
        NSArray* parts = [rangeHeader componentsSeparatedByString: @"/"];
        if ([parts count] != 2) return -1;
        NSString *lastPart = [parts objectAtIndex: 1];
        if ([lastPart isEqualToString:@"*"]) return -2;
        if ([lastPart isEqualToString:@"0"]) return 0;
        int count = [lastPart intValue];
        if (count == 0) return -1; //real zero was filtered out above
        return count;
    }
}

- (void)readObjectWithId:(NSString *)objectId inSchema:(NSString *)schema parameters:(NSDictionary *)parameters options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMDataStoreSuccessBlock)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:@"GET" path:path parameters:parameters];
        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)refreshAndRetry:(NSURLRequest *)request originalError:(NSError *)originalError requestSuccessCallbackQueue:(dispatch_queue_t)successCallbackQueue requestFailureCallbackQueue:(dispatch_queue_t)failureCallbackQueue options:(SMRequestOptions *)options onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
    if (self.session.refreshing) {
        if (failureBlock) {
            dispatch_async(failureCallbackQueue, ^{
                NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorRefreshTokenInProgress userInfo:nil];
                failureBlock(request, nil, error, nil);
            });
        }
    } else {
        [options setTryRefreshToken:NO];
        __block dispatch_queue_t newQueueForRefresh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
        [self.session refreshTokenWithSuccessCallbackQueue:newQueueForRefresh failureCallbackQueue:newQueueForRefresh onSuccess:^(NSDictionary *userObject) {
            [self queueRequest:[self.session signRequest:request] options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
         
        } onFailure:^(NSError *error) {
            NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:error, SMRefreshErrorObjectKey, @"Attempt to refresh access token failed.", NSLocalizedDescriptionKey, nil];
            if (originalError) {
                [userInfo setObject:originalError forKey:SMOriginalErrorCausingRefreshKey];
            }
            __block NSError *refreshError = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorRefreshTokenFailed userInfo:userInfo];
            if (self.session.tokenRefreshFailureBlock) {
                dispatch_async(failureCallbackQueue, ^{
                    SMFailureBlock newFailureBlock = ^(NSError *anError){
                        failureBlock(nil, nil, anError, nil);
                    };
                    self.session.tokenRefreshFailureBlock(refreshError, newFailureBlock);
                });
            } else if (failureBlock) {
                dispatch_async(failureCallbackQueue, ^{
                    failureBlock(request, nil, refreshError, nil);
                });
            }
        }];
    }
}

- (void)refreshAndRetryCustomCode:(NSURLRequest *)request customCodeRequestInstance:(SMCustomCodeRequest *)customCodeRequest originalError:(NSError *)originalError requestSuccessCallbackQueue:(dispatch_queue_t)successCallbackQueue requestFailureCallbackQueue:(dispatch_queue_t)failureCallbackQueue options:(SMRequestOptions *)options onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
    if (self.session.refreshing) {
        if (failureBlock) {
            dispatch_async(failureCallbackQueue, ^{
                NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorRefreshTokenInProgress userInfo:nil];
                failureBlock(request, nil, error, nil);
            });
        }
    } else {
        [options setTryRefreshToken:NO];
        __block dispatch_queue_t newQueueForRefresh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
        [self.session refreshTokenWithSuccessCallbackQueue:newQueueForRefresh failureCallbackQueue:newQueueForRefresh onSuccess:^(NSDictionary *userObject) {
            [self queueCustomCodeRequest:[self.session signRequest:request] customCodeRequestInstance:customCodeRequest options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
            
        } onFailure:^(NSError *error) {
            NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:error, SMRefreshErrorObjectKey, @"Attempt to refresh access token failed.", NSLocalizedDescriptionKey, nil];
            if (originalError) {
                [userInfo setObject:originalError forKey:SMOriginalErrorCausingRefreshKey];
            }
            __block NSError *refreshError = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorRefreshTokenFailed userInfo:userInfo];
            if (self.session.tokenRefreshFailureBlock) {
                dispatch_async(failureCallbackQueue, ^{
                    SMFailureBlock newFailureBlock = ^(NSError *anError){
                        failureBlock(nil, nil, anError, nil);
                    };
                    self.session.tokenRefreshFailureBlock(refreshError, newFailureBlock);
                });
            } else if (failureBlock) {
                dispatch_async(failureCallbackQueue, ^{
                    failureBlock(request, nil, refreshError, nil);
                });
            }
        }];
    }
}

- (AFJSONRequestOperation *)newOperationForRequest:(NSURLRequest *)request options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMFullResponseSuccessBlock)successBlock onFailure:(SMFullResponseFailureBlock)failureBlock
{
    if (options.headers && [options.headers count] > 0) {
        // Enumerate through options and add them to the request header.
        NSMutableURLRequest *tempRequest = [request mutableCopy];
        [options.headers enumerateKeysAndObjectsUsingBlock:^(id headerField, id headerValue, BOOL *stop) {
            [tempRequest setValue:headerValue forHTTPHeaderField:headerField];
        }];
        request = tempRequest;
        
        // Set the headers dictionary to empty, to prevent unnecessary enumeration during recursion.
        options.headers = [NSDictionary dictionary];
    }
    
    SMFullResponseFailureBlock retryBlock = ^(NSURLRequest *originalRequest, NSHTTPURLResponse *response, NSError *error, id JSON) {
        if ([response statusCode] == SMErrorServiceUnavailable && options.numberOfRetries > 0) {
            NSString *retryAfter = [[response allHeaderFields] valueForKey:@"Retry-After"];
            if (retryAfter) {
                [options setNumberOfRetries:(options.numberOfRetries - 1)];
                double delayInSeconds = [retryAfter doubleValue];
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    if (options.retryBlock) {
                        options.retryBlock(originalRequest, response, error, JSON, options, successBlock, failureBlock);
                    } else {
                        [self queueRequest:[self.session signRequest:originalRequest] options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:successBlock onFailure:failureBlock];
                    }
                });
            } else {
                if (failureBlock) {
                    failureBlock(originalRequest, response, error, JSON);
                }
            }
        } else if ([error domain] == NSURLErrorDomain && [error code] == -1009) {
            if (failureBlock) {
                NSError *networkNotReachableError = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorNetworkNotReachable userInfo:[error userInfo]];
                failureBlock(originalRequest, response, networkNotReachableError, JSON);
            }
        } else {
            if (failureBlock) {
                failureBlock(originalRequest, response, error, JSON);
            }
        }
    };
    
    AFJSONRequestOperation *op = [SMJSONRequestOperation JSONRequestOperationWithRequest:request success:successBlock failure:retryBlock];
    if (successCallbackQueue) {
        [op setSuccessCallbackQueue:successCallbackQueue];
    }
    if (failureCallbackQueue) {
        [op setFailureCallbackQueue:failureCallbackQueue];
    }
    
    return op;
    
}

- (void)queueRequest:(NSURLRequest *)request options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMFullResponseSuccessBlock)onSuccess onFailure:(SMFullResponseFailureBlock)onFailure
{
    if (options.headers && [options.headers count] > 0) {
        // Enumerate through options and add them to the request header.
        NSMutableURLRequest *tempRequest = [request mutableCopy];
        [options.headers enumerateKeysAndObjectsUsingBlock:^(id headerField, id headerValue, BOOL *stop) {
            
            // Error checks for functionality not supported
            if ([headerField isEqualToString:@"X-StackMob-Expand"]) {
                if ([[request HTTPMethod] isEqualToString:@"POST"] || [[request HTTPMethod] isEqualToString:@"PUT"]) {
                    [NSException raise:SMExceptionIncompatibleObject format:@"Expand depth is not supported for creates or updates. Please check your requests and edit accordingly."];
                }
            }
            
            [tempRequest setValue:headerValue forHTTPHeaderField:headerField];
        }];
        request = tempRequest;
        
        // Set the headers dictionary to empty, to prevent unnecessary enumeration during recursion.
        options.headers = [NSDictionary dictionary];
    }
    
    
    
    if ([self.session eligibleForTokenRefresh:options]) {
        [self refreshAndRetry:request originalError:nil requestSuccessCallbackQueue:successCallbackQueue requestFailureCallbackQueue:failureCallbackQueue options:options onSuccess:onSuccess onFailure:onFailure];
    }
    else {
        SMFullResponseFailureBlock retryBlock = ^(NSURLRequest *originalRequest, NSHTTPURLResponse *response, NSError *error, id JSON) {
            if ([response statusCode] == SMErrorUnauthorized && options.tryRefreshToken && self.session.refreshToken != nil) {
                [self refreshAndRetry:originalRequest originalError:[self errorFromResponse:response JSON:JSON] requestSuccessCallbackQueue:successCallbackQueue requestFailureCallbackQueue:failureCallbackQueue options:options onSuccess:onSuccess onFailure:onFailure];
            } else if ([response statusCode] == SMErrorServiceUnavailable && options.numberOfRetries > 0) {
                NSString *retryAfter = [[response allHeaderFields] valueForKey:@"Retry-After"];
                if (retryAfter) {
                    dispatch_queue_t retryQueue = dispatch_queue_create("com.stackmob.retry", NULL);
                    [options setNumberOfRetries:(options.numberOfRetries - 1)];
                    double delayInSeconds = [retryAfter doubleValue];
                    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                    dispatch_after(popTime, retryQueue, ^(void){
                        if (options.retryBlock) {
                            options.retryBlock(originalRequest, response, error, JSON, options, onSuccess, onFailure);
                        } else {
                            [self queueRequest:[self.session signRequest:originalRequest] options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:onSuccess onFailure:onFailure];
                        }
                    });
                } else {
                    if (onFailure) {
                        onFailure(originalRequest, response, error, JSON);
                    }
                }
            } else if ([error domain] == NSURLErrorDomain && [error code] == -1009) {
                if (onFailure) {
                    NSError *networkNotReachableError = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorNetworkNotReachable userInfo:[error userInfo]];
                    onFailure(originalRequest, response, networkNotReachableError, JSON);
                }
            } else {
                if (onFailure) {
                    onFailure(originalRequest, response, error, JSON);
                }
            }
        };
        
        AFJSONRequestOperation *op = [SMJSONRequestOperation JSONRequestOperationWithRequest:request success:onSuccess failure:retryBlock];
        if (successCallbackQueue) {
            [op setSuccessCallbackQueue:successCallbackQueue];
        }
        if (failureCallbackQueue) {
            [op setFailureCallbackQueue:failureCallbackQueue];
        }
        [[self.session oauthClientWithHTTPS:options.isSecure] enqueueHTTPRequestOperation:op];
    }
    
}

- (void)queueCustomCodeRequest:(NSURLRequest *)request customCodeRequestInstance:(SMCustomCodeRequest *)customCodeRequest options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMFullResponseSuccessBlock)onSuccess onFailure:(SMFullResponseFailureBlock)onFailure
{
    if ([self.session eligibleForTokenRefresh:options]) {
        [self refreshAndRetryCustomCode:request customCodeRequestInstance:customCodeRequest originalError:nil requestSuccessCallbackQueue:successCallbackQueue requestFailureCallbackQueue:failureCallbackQueue options:options onSuccess:onSuccess onFailure:onFailure];
    }
    else {
        AFHTTPOperationSuccessBlock successBlock = ^(AFHTTPRequestOperation *operation, id responseObject){
            
            // Remove any custom content types
            if (customCodeRequest.responseContentType) {
                [AFHTTPRequestOperation removeAcceptableContentType:customCodeRequest.responseContentType];
            }
            
            NSString *contentType = [[[operation response] allHeaderFields] objectForKey:@"Content-Type"];
            if ([contentType rangeOfString:SM_VENDOR_SPECIFIC_JSON].location != NSNotFound || [contentType rangeOfString:SM_JSON].location != NSNotFound) {
                id returnValue = nil;
                NSError *error = nil;
                returnValue = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
                if (error) {
                    if (onFailure) {
                        dispatch_async(failureCallbackQueue, ^{
                            onFailure([operation request], [operation response], error, nil);
                        });
                    }
                } else if (onSuccess) {
                    onSuccess(operation.request, operation.response, returnValue);
                }
                
            } else if ([contentType rangeOfString:SM_TEXT_PLAIN].location != NSNotFound) {
                if (onSuccess) {
                    NSString *returnValue = nil;
                    returnValue = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
                    onSuccess(operation.request, operation.response, returnValue);
                }
            } else if (onSuccess) {
                onSuccess(operation.request, operation.response, responseObject);
            }
        };
        
        AFHTTPOperationFailureBlock retryBlock = ^(AFHTTPRequestOperation *operation, NSError *error){
            
            // Remove any custom content types
            if (customCodeRequest.responseContentType) {
                [AFHTTPRequestOperation removeAcceptableContentType:customCodeRequest.responseContentType];
            }
            
            if ([[operation response] statusCode] == SMErrorUnauthorized && options.tryRefreshToken && self.session.refreshToken != nil) {
                [self refreshAndRetryCustomCode:[operation request] customCodeRequestInstance:customCodeRequest originalError:error requestSuccessCallbackQueue:successCallbackQueue requestFailureCallbackQueue:failureCallbackQueue options:options onSuccess:onSuccess onFailure:onFailure];
            } else if ([[operation response] statusCode] == SMErrorServiceUnavailable && options.numberOfRetries > 0) {
                NSString *retryAfter = [[[operation response] allHeaderFields] valueForKey:@"Retry-After"];
                if (retryAfter) {
                    dispatch_queue_t retryQueue = dispatch_queue_create("com.stackmob.retry", NULL);
                    [options setNumberOfRetries:(options.numberOfRetries - 1)];
                    double delayInSeconds = [retryAfter doubleValue];
                    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                    dispatch_after(popTime, retryQueue, ^(void){
                        if (options.retryBlock) {
                            options.retryBlock([operation request], [operation response], error, nil, options, onSuccess, onFailure);
                        } else {
                            [self queueCustomCodeRequest:[self.session signRequest:[operation request]] customCodeRequestInstance:customCodeRequest options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:onSuccess onFailure:onFailure];
                        }
                    });
                } else {
                    if (onFailure) {
                        onFailure([operation request], [operation response], error, nil);
                    }
                }
            } else if ([error domain] == NSURLErrorDomain && [error code] == -1009) {
                if (onFailure) {
                    NSError *networkNotReachableError = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorNetworkNotReachable userInfo:[error userInfo]];
                    onFailure([operation request], [operation response], networkNotReachableError, nil);
                }
            } else {
                if (onFailure) {
                    onFailure([operation request], [operation response], error, nil);
                }
            }
        };
        
        SMHTTPRequestOperation *op = [SMHTTPRequestOperation SMHTTPRequestOperationWithRequest:request success:successBlock failure:retryBlock];
        
        NSSet *whitelistedContentTypes = [NSSet setWithObjects:SM_VENDOR_SPECIFIC_JSON, SM_JSON, SM_TEXT_PLAIN, SM_OCTET_STREAM, nil];
        if (customCodeRequest.responseContentType) {
            [SMHTTPRequestOperation addAcceptableContentTypes:[whitelistedContentTypes setByAddingObject:customCodeRequest.responseContentType]];
        } else {
            [SMHTTPRequestOperation addAcceptableContentTypes:whitelistedContentTypes];
        }
        
        if (successCallbackQueue) {
            [op setSuccessCallbackQueue:successCallbackQueue];
        }
        if (failureCallbackQueue) {
            [op setFailureCallbackQueue:failureCallbackQueue];
        }
        [[self.session oauthClientWithHTTPS:options.isSecure] enqueueHTTPRequestOperation:op];
        
    }
    
}

- (NSString *)URLEncodedStringFromValue:(NSString *)value
{
    static NSString * const kAFCharactersToBeEscaped = @":/.?&=;+!@#$()~[]";
    
return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)value, nil, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
}

// Operational methods

- (AFJSONRequestOperation *)postOperationForObject:(NSDictionary *)object inSchema:(NSString *)schema options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMResultSuccessBlock)successBlock onFailure:(SMCoreDataSaveFailureBlock)failureBlock
{
    
    if (object == nil || schema == nil) {
        if (failureBlock) {
            NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
            failureBlock(nil, error, object, options, nil);
        }
        return nil;
    } 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 SMFullResponseSuccessBlockForResultSuccessBlock:successBlock];
        SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObject:object options:options originalSuccessBlock:successBlock coreDataSaveFailureBlock:failureBlock];
        return [self newOperationForRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
    }
}

- (AFJSONRequestOperation *)putOperationForObjectID:(NSString *)objectId inSchema:(NSString *)schema update:(NSDictionary *)updatedFields options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMResultSuccessBlock)successBlock onFailure:(SMCoreDataSaveFailureBlock)failureBlock
{
    if (objectId == nil || schema == nil) {
        if (failureBlock) {
            NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
            failureBlock(nil, error, updatedFields, options, nil);
        }
        return nil;
    } 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 SMFullResponseSuccessBlockForResultSuccessBlock:successBlock];
        SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObject:updatedFields options:options originalSuccessBlock:successBlock coreDataSaveFailureBlock:failureBlock];
        return [self newOperationForRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
    }
}

- (AFJSONRequestOperation *)deleteOperationForObjectID:(NSString *)objectId inSchema:(NSString *)schema options:(SMRequestOptions *)options successCallbackQueue:(dispatch_queue_t)successCallbackQueue failureCallbackQueue:(dispatch_queue_t)failureCallbackQueue onSuccess:(SMResultSuccessBlock)successBlock onFailure:(SMCoreDataSaveFailureBlock)failureBlock
{
    if (objectId == nil || schema == nil) {
        if (failureBlock) {
            NSError *error = [[NSError alloc] initWithDomain:SMErrorDomain code:SMErrorInvalidArguments userInfo:nil];
            failureBlock(nil, error, nil, options, nil);
        }
        return nil;
    } 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 SMFullResponseSuccessBlockForResultSuccessBlock:successBlock];
        SMFullResponseFailureBlock urlFailureBlock = [self SMFullResponseFailureBlockForObject:nil options:options originalSuccessBlock:successBlock coreDataSaveFailureBlock:failureBlock];
        return [self newOperationForRequest:request options:options successCallbackQueue:successCallbackQueue failureCallbackQueue:failureCallbackQueue onSuccess:urlSuccessBlock onFailure:urlFailureBlock];
    }
}



@end
Something went wrong with that request. Please try again.