From 948cc858736b4e4787256ec34933ecb76c5e08cd Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Wed, 15 Feb 2012 12:06:24 -0500 Subject: [PATCH] Added new pre-flight delegate callbacks for customization of RKRequest and RKObjectLoader instances before dispatch --- Code/Network/RKRequest.h | 8 ++++++++ Code/Network/RKRequest.m | 7 ++++++- Code/ObjectMapping/RKObjectLoader.h | 10 ++++++++++ Code/ObjectMapping/RKObjectLoader.m | 4 ++++ Code/ObjectMapping/RKObjectSerializer.m | 2 +- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Code/Network/RKRequest.h b/Code/Network/RKRequest.h index 2fc7f502..a6515626 100644 --- a/Code/Network/RKRequest.h +++ b/Code/Network/RKRequest.h @@ -784,6 +784,14 @@ typedef void(^RKRequestDidFailLoadWithErrorBlock)(NSError *error); ///----------------------------------------------------------------------------- /// @name Observing Request Progress ///----------------------------------------------------------------------------- + +/** + Tells the delegate the request is about to be prepared for sending to the remote host. + + @param request The RKRequest object that is about to be sent. + */ +- (void)requestWillPrepareForSend:(RKRequest *)request; + /** Sent when a request has started loading diff --git a/Code/Network/RKRequest.m b/Code/Network/RKRequest.m index be527329..653e6bab 100644 --- a/Code/Network/RKRequest.m +++ b/Code/Network/RKRequest.m @@ -365,12 +365,17 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) { // Setup the NSURLRequest. The request must be prepared right before dispatching - (BOOL)prepareURLRequest { [_URLRequest setHTTPMethod:[self HTTPMethod]]; + + if ([self.delegate respondsToSelector:@selector(requestWillPrepareForSend:)]) { + [self.delegate requestWillPrepareForSend:self]; + } + [self setRequestBody]; [self addHeadersToRequest]; NSString* body = [[NSString alloc] initWithData:[_URLRequest HTTPBody] encoding:NSUTF8StringEncoding]; RKLogTrace(@"Prepared %@ URLRequest '%@'. HTTP Headers: %@. HTTP Body: %@.", [self HTTPMethod], _URLRequest, [_URLRequest allHTTPHeaderFields], body); - [body release]; + [body release]; return YES; } diff --git a/Code/ObjectMapping/RKObjectLoader.h b/Code/ObjectMapping/RKObjectLoader.h index 79cbf9a1..36ceb02a 100644 --- a/Code/ObjectMapping/RKObjectLoader.h +++ b/Code/ObjectMapping/RKObjectLoader.h @@ -80,6 +80,16 @@ typedef void(^RKObjectLoaderDidLoadObjectsDictionaryBlock)(NSDictionary *diction */ - (void)objectLoaderDidFinishLoading:(RKObjectLoader *)objectLoader; +/** + Informs the delegate that the object loader has serialized the source object into a serializable representation + for sending to the remote system. The serialization can be modified to allow customization of the request payload independent of mapping. + + @param objectLoader The object loader performing the serialization. + @param sourceObject The object that was serialized. + @param serialization The serialization of sourceObject to be sent to the remote backend for processing. + */ +- (void)objectLoader:(RKObjectLoader *)objectLoader didSerializeSourceObject:(id)sourceObject toSerialization:(inout id *)serialization; + /** Sent when an object loader encounters a response status code or MIME Type that RestKit does not know how to handle. diff --git a/Code/ObjectMapping/RKObjectLoader.m b/Code/ObjectMapping/RKObjectLoader.m index 67fc97c8..fb46c67f 100644 --- a/Code/ObjectMapping/RKObjectLoader.m +++ b/Code/ObjectMapping/RKObjectLoader.m @@ -351,6 +351,10 @@ [self didFailLoadWithError:error]; return NO; } + + if ([self.delegate respondsToSelector:@selector(objectLoader:didSerializeSourceObject:toSerialization:)]) { + [self.delegate objectLoader:self didSerializeSourceObject:self.sourceObject toSerialization:¶ms]; + } self.params = params; } diff --git a/Code/ObjectMapping/RKObjectSerializer.m b/Code/ObjectMapping/RKObjectSerializer.m index 1def68c9..42f9e523 100644 --- a/Code/ObjectMapping/RKObjectSerializer.m +++ b/Code/ObjectMapping/RKObjectSerializer.m @@ -90,7 +90,7 @@ return nil; } -- (id)serializationForMIMEType:(NSString*)MIMEType error:(NSError**)error { +- (id)serializationForMIMEType:(NSString *)MIMEType error:(NSError **)error { if ([MIMEType isEqualToString:RKMIMETypeFormURLEncoded]) { // Dictionaries are natively RKRequestSerializable as Form Encoded return [self serializedObject:error];