-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDIOSCSRFAFHTTPClient.m
More file actions
executable file
·69 lines (52 loc) · 2.58 KB
/
DIOSCSRFAFHTTPClient.m
File metadata and controls
executable file
·69 lines (52 loc) · 2.58 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
//
// 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)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];
[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];
[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];
[self enqueueHTTPRequestOperation:operation];
}
@end