forked from kylebrowning/waterwheel.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIOSSession.m
102 lines (90 loc) · 3.83 KB
/
DIOSSession.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
//
// DIOSSession.m
// dios
//
// Created by Kyle Browning on 9/4/14.
// Copyright (c) 2014 Kyle Browning. All rights reserved.
//
#import "DIOSSession.h"
@implementation DIOSSession
@synthesize baseURL;
#pragma mark Singleton Methods
+ (DIOSSession *) sharedSession
{
static DIOSSession *sharedSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSession = [[self alloc] init];
sharedSession.requestSerializer = [AFJSONRequestSerializer serializer];
});
return sharedSession;
}
- (id)init
{
if (self = [super init]) {
}
return self;
}
#pragma mark -
#pragma mark Request Methods
- (void) sendRequestWithPath:(NSString*)path
method:(NSString*)method
params:(NSDictionary*)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure
{
NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:params];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
[DIOSSession logResponseSucccessToConsole:operation withResponse:responseObject];
if (success != nil) {
success(operation, responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[DIOSSession logRequestFailuretoConsole:operation withError:error];
if (failure != nil) {
failure(operation, error);
}
}];
if (_signRequests) {
NSURLCredential *credential = [NSURLCredential credentialWithUser:_basicAuthUsername password:_basicAuthPassword persistence:NSURLCredentialPersistenceNone];
[operation setCredential:credential];
}
[self.operationQueue addOperation:operation];
}
- (NSMutableURLRequest *) requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
NSString *urlString = [NSString stringWithFormat:@"%@/%@", [[self baseURL] absoluteString], path];
[self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[self.requestSerializer setValue:@"application/hal+json" forHTTPHeaderField:@"Content-Type"];
if (self.signRequests) {
//doesntwork
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:_basicAuthUsername password:_basicAuthPassword];
}
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:urlString parameters:parameters error:nil];
return request;
}
#pragma mark -
#pragma mark Logging methods
+ (void) logResponseSucccessToConsole:(AFHTTPRequestOperation *)operation withResponse:(id)responseObject
{
#ifdef DEBUG
NSLog(@"\n----- DIOS Success -----\nStatus code: %ld\nURL: %@\n----- Response ----- \n%@\n", (long)operation.response.statusCode, [operation.response.URL absoluteString],operation.responseString);
#endif
}
+ (void) logRequestFailuretoConsole:(AFHTTPRequestOperation *)operation withError:(NSError *)error
{
#ifdef DEBUG
NSString *body = [[NSString alloc] initWithData:operation.request.HTTPBody encoding:4];
NSLog(@"\n----- DIOS Failure -----\nStatus code: %ld\nRequest : %@\nURL: %@\n----- Response ----- \n%@\n----- Error ----- \n%@", (long)operation.response.statusCode, body, [operation.response.URL absoluteString], operation.responseString, [error localizedDescription]);
#endif
}
#pragma mark -
#pragma mark Helpful methods
- (void) setBasicAuthCredsWithUsername:(NSString *)username andPassword:(NSString*)password {
[self setBasicAuthUsername:username];
[self setBasicAuthPassword:password];
[self setSignRequests:YES];
}
@end