forked from kylebrowning/waterwheel.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIOSCSRFAFHTTPClient.m
More file actions
85 lines (75 loc) · 4.32 KB
/
DIOSCSRFAFHTTPClient.m
File metadata and controls
85 lines (75 loc) · 4.32 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
//
// DIOSCSRFAFHTTPClient.m
// WalkthroughAcquia
//
// Created by Zoltán Váradi on 7/3/13.
// Copyright (c) 2013 Zoltán Váradi. All rights reserved.
//
#import "DIOSCSRFAFHTTPClient.h"
#import "Settings.h"
@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;
}
-(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 {
[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 {
[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 {
[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