Files
RestKit/OTRestResponse.m
Jeremy Ellison cf972fce69 * [OTRestModel allObjectsOrderedBy:]
* buildModelFromXML: guards against nils
* added error property to OTRestResponse, will get set on connectionDidFail:withError:
* also added errorDescription which either uses the error description or the first <error> element in the document
2009-08-11 06:14:59 -04:00

96 lines
2.1 KiB
Objective-C

//
// OTRestResponse.m
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestResponse.h"
@implementation OTRestResponse
@synthesize payload = _payload, request = _request, error = _error;
- (id)init {
if (self = [super init]) {
_payload = [[NSMutableData alloc] init];
_error = nil;
}
return self;
}
- (id)initWithRestRequest:(OTRestRequest*)request {
if (self = [self init]) {
_request = [request retain];
}
return self;
}
- (void)dealloc {
[_httpURLResponse release];
[_payload release];
[_request release];
[_error release];
[super dealloc];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_payload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
_httpURLResponse = [response retain];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
[[_request delegate] performSelector:[_request callback] withObject:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
_error = [error retain];
[[_request delegate] performSelector:[_request callback] withObject:self];
}
- (NSString*)localizedStatusCodeString {
return [NSHTTPURLResponse localizedStringForStatusCode:[self statusCode]];
}
- (NSString*)payloadString {
return [[[NSString alloc] initWithData:_payload encoding:NSUTF8StringEncoding] autorelease];
}
- (DocumentRoot*)payloadXMLDocument {
return [DocumentRoot parseXML:[self payloadString]];
}
- (NSString*)errorDescription {
NSString* desc;
if (_error != nil) {
desc = [_error localizedDescription];
} else {
desc = [[(Element*)[self payloadXMLDocument] selectElement:@"error"] contentsText];
}
return desc;
}
- (NSURL*)URL {
return [_httpURLResponse URL];
}
- (NSString*)MIMEType {
return [_httpURLResponse MIMEType];
}
- (NSInteger)statusCode {
return [_httpURLResponse statusCode];
}
- (NSDictionary*)allHeaderFields {
return [_httpURLResponse allHeaderFields];
}
@end