-
Notifications
You must be signed in to change notification settings - Fork 2
/
Response.m
79 lines (65 loc) · 1.54 KB
/
Response.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
//
// Response.m
//
//
// Created by Ryan Daigle on 7/30/08.
// Copyright 2008 yFactorial, LLC. All rights reserved.
//
#import "Response.h"
#import "NSHTTPURLResponse+Error.h"
@implementation Response
@synthesize body, headers, statusCode, error;
+ (id)responseFrom:(NSHTTPURLResponse *)response withBody:(NSData *)data andError:(NSError *)aError {
return [[[self alloc] initFrom:response withBody:data andError:aError] autorelease];
}
- (void)normalizeError:(NSError *)aError {
switch ([aError code]) {
case NSURLErrorUserCancelledAuthentication:
self.statusCode = 401;
self.error = [NSHTTPURLResponse buildResponseError:401];
break;
default:
self.error = aError;
break;
}
}
- (id)initFrom:(NSHTTPURLResponse *)response withBody:(NSData *)data andError:(NSError *)aError {
[self init];
self.body = data;
if(response) {
self.statusCode = [response statusCode];
self.headers = [response allHeaderFields];
self.error = [response error];
}
else {
[self normalizeError:aError];
}
return self;
}
- (BOOL)isSuccess {
return statusCode >= 200 && statusCode < 400;
}
- (BOOL)isError {
return ![self isSuccess];
}
- (BOOL)isARError{
return statusCode == 422;
}
- (void)log {
NSString *log = [[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] autorelease];
//log = @"YUP, GOT DATA. CHANGE Response#log to see it";
if ([self isSuccess]) {
debugLog(@"<= %@", log);
}
else {
NSLog(@"<= %@", log);
}
}
#pragma mark cleanup
- (void) dealloc
{
[body release];
[headers release];
[super dealloc];
}
@end