Skip to content

Commit

Permalink
Added new pre-flight delegate callbacks for customization of RKReques…
Browse files Browse the repository at this point in the history
…t and RKObjectLoader instances before dispatch
  • Loading branch information
blakewatters committed Feb 15, 2012
1 parent 789b0b9 commit 948cc85
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Code/Network/RKRequest.h
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion Code/Network/RKRequest.m
Expand Up @@ -365,12 +365,17 @@ - (void)addHeadersToRequest {
// 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;
}
Expand Down
10 changes: 10 additions & 0 deletions Code/ObjectMapping/RKObjectLoader.h
Expand Up @@ -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<RKRequestSerializable> *)serialization;

/**
Sent when an object loader encounters a response status code or MIME Type that RestKit does not know how to handle.
Expand Down
4 changes: 4 additions & 0 deletions Code/ObjectMapping/RKObjectLoader.m
Expand Up @@ -351,6 +351,10 @@ - (BOOL)prepareURLRequest {
[self didFailLoadWithError:error];
return NO;
}

if ([self.delegate respondsToSelector:@selector(objectLoader:didSerializeSourceObject:toSerialization:)]) {
[self.delegate objectLoader:self didSerializeSourceObject:self.sourceObject toSerialization:&params];
}

self.params = params;
}
Expand Down
2 changes: 1 addition & 1 deletion Code/ObjectMapping/RKObjectSerializer.m
Expand Up @@ -90,7 +90,7 @@ - (id)serializedObjectForMIMEType:(NSString*)MIMEType error:(NSError**)error {
return nil;
}

- (id<RKRequestSerializable>)serializationForMIMEType:(NSString*)MIMEType error:(NSError**)error {
- (id<RKRequestSerializable>)serializationForMIMEType:(NSString *)MIMEType error:(NSError **)error {
if ([MIMEType isEqualToString:RKMIMETypeFormURLEncoded]) {
// Dictionaries are natively RKRequestSerializable as Form Encoded
return [self serializedObject:error];
Expand Down

0 comments on commit 948cc85

Please sign in to comment.