Permalink
Browse files

Changed CSRF token request to asynchronous

  • Loading branch information...
1 parent dd0c331 commit c71234f2535b275aaabe91d64e40f2657354dc73 @markusekblad markusekblad committed Aug 21, 2013
Showing with 51 additions and 34 deletions.
  1. +2 −0 DIOSCSRFAFHTTPClient.h
  2. +49 −34 DIOSCSRFAFHTTPClient.m
View
@@ -11,4 +11,6 @@
@interface DIOSCSRFAFHTTPClient : AFHTTPClient
- (NSString*)getCSRFToken;
+-(void)getCSRFTokenWithSuccess:(void (^)(NSString *csrfToken))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
@end
View
@@ -13,57 +13,72 @@ @implementation DIOSCSRFAFHTTPClient
- (NSString*)getCSRFToken
{
- NSString* csrfToken;
-
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/services/session/token", kDiosBaseUrl]]];
- NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- csrfToken = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-
- return csrfToken;
+ NSString* csrfToken;
+
+ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/services/session/token", kDiosBaseUrl]]];
+ NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ csrfToken = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+ return csrfToken;
+}
+
+-(void)getCSRFTokenWithSuccess:(void (^)(NSString *csrfToken))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
+ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/services/session/token", kDiosBaseUrl]]];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ NSString *csrfToken = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
+ success(csrfToken);
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
+ failure(operation, error);
+ }];
+ [self enqueueHTTPRequestOperation:operation];
}
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
-{
- NSMutableURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
-
- NSString* csrfToken = [self getCSRFToken];
- [request setValue: csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
-
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
+ [self getCSRFTokenWithSuccess:^(NSString *csrfToken) {
+ NSMutableURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
+ [request setValue: csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
+ } failure:^(AFHTTPRequestOperation *csrfOperation, NSError *error) {
+ NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ [self enqueueHTTPRequestOperation:operation];
+ }];
}
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
-{
- NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
-
- NSString* csrfToken = [self getCSRFToken];
- [request setValue: csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
-
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
+ [self getCSRFTokenWithSuccess:^(NSString *csrfToken) {
+ NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
+ [request setValue: csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
+ } failure:^(AFHTTPRequestOperation *csrfOperation, NSError *error) {
+ NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ [self enqueueHTTPRequestOperation:operation];
+ }];
}
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
-{
- NSMutableURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
-
- NSString* csrfToken = [self getCSRFToken];
- [request setValue: csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
-
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
+ [self getCSRFTokenWithSuccess:^(NSString *csrfToken) {
+ NSMutableURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
+ [request setValue: csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ [self enqueueHTTPRequestOperation:operation];
+ } failure:^(AFHTTPRequestOperation *csrfOperation, NSError *error) {
+ NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
+ }];
}
-
-
-@end
+@end

0 comments on commit c71234f

Please sign in to comment.