Skip to content

Commit

Permalink
Remove RKObjectManager specific value for the 'Accept' header and c…
Browse files Browse the repository at this point in the history
…onfigure the header directly on the HTTP client instead. Register the `AFJSONRequestOperation` class in `managerWithBaseURL:`. Update documentation.
  • Loading branch information
blakewatters committed Dec 12, 2012
1 parent f51a711 commit b9457ce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 43 deletions.
10 changes: 6 additions & 4 deletions Code/Network/RKObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ RKMappingResult, RKRequestDescriptor, RKResponseDescriptor;
/**
Creates and returns a new `RKObjectManager` object initialized with a new `AFHTTPClient` object that was in turn initialized with the given base URL. The RestKit defaults are applied to the object manager.
When initialized with a base URL, the returned object manager will have a `requestSerializationMIMEType` with the value of `RKMIMETypeFormURLEncoded` and a default value for the 'Accept' header set to `RKMIMETypeJSON`.
When initialized with a base URL, the returned object manager will have a `requestSerializationMIMEType` with the value of `RKMIMETypeFormURLEncoded` and the underlying `HTTPClient` will have a default value for the 'Accept' header set to `RKMIMETypeJSON`, and the `AFJSONRequestOperation` class will be registered.
@param baseURL The base URL with which to initialize the `AFHTTPClient` object
@return A new `RKObjectManager` initialized with an `AFHTTPClient` that was initialized with the given baseURL.
Expand Down Expand Up @@ -312,9 +312,11 @@ RKMappingResult, RKRequestDescriptor, RKResponseDescriptor;
@property (nonatomic, strong) NSString *requestSerializationMIMEType;

/**
The value for the HTTP "Accept" header to specify the preferred serialization format for retrieved data.
If the receiver was initialized with an `AFHTTPClient`, then the value of the 'Accept' header is deferred to the client. If initialized directly with a baseURL, the default value is `RKMIMETypeJSON`, which is equal to the string `@"application/json"`. A value of `nil` will prevent the object manager from explicitly setting a value for the "Accept" header.
Sets a default header on the HTTP cleitn for the HTTP "Accept" header to specify the preferred serialization format for retrieved data.

This comment has been minimized.

Copy link
@allaire

allaire Dec 12, 2012

Small typo here, "cleitn".

This comment has been minimized.

Copy link
@blakewatters

blakewatters Dec 12, 2012

Author Member

Thanks, fixed in my last push

This method is a convenience method whose implementation is equivalent to the following example code:
[manager.HTTPClient setDefaultHeader:@"Accept" value:MIMEType];
@param MIMEType The MIME Type to set as the value for the HTTP "Accept" header.
*/
Expand Down
15 changes: 4 additions & 11 deletions Code/Network/RKObjectManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ @interface RKObjectManager ()
@property (nonatomic, strong) NSMutableArray *mutableRequestDescriptors;
@property (nonatomic, strong) NSMutableArray *mutableResponseDescriptors;
@property (nonatomic, strong) NSMutableArray *mutableFetchRequestBlocks;
@property (nonatomic, strong) NSString *acceptHeaderValue;
@property (nonatomic) Class HTTPOperationClass;
@end

Expand Down Expand Up @@ -237,16 +236,15 @@ + (void)setSharedManager:(RKObjectManager *)manager
+ (RKObjectManager *)managerWithBaseURL:(NSURL *)baseURL
{
RKObjectManager *manager = [[self alloc] initWithHTTPClient:[AFHTTPClient clientWithBaseURL:baseURL]];
manager.acceptHeaderValue = RKMIMETypeJSON;
[manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
manager.requestSerializationMIMEType = RKMIMETypeFormURLEncoded;
return manager;
}

// NOTE: This implementation could just use the default headers on AFHTTPClient, but this
// feels less intrusive.
- (void)setAcceptHeaderWithMIMEType:(NSString *)MIMEType;
{
self.acceptHeaderValue = MIMEType;
[self.HTTPClient setDefaultHeader:@"Accept" value:MIMEType];
}

- (NSURL *)baseURL
Expand All @@ -256,9 +254,7 @@ - (NSURL *)baseURL

- (NSDictionary *)defaultHeaders
{
NSMutableDictionary *defaultHeaders = [self.HTTPClient.defaultHeaders mutableCopy];
if (self.acceptHeaderValue) [defaultHeaders setValue:self.acceptHeaderValue forKey:@"Accept"];
return defaultHeaders;
return self.HTTPClient.defaultHeaders;
}

#pragma mark - Building Requests
Expand All @@ -282,7 +278,6 @@ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
} else {
request = [self.HTTPClient requestWithMethod:method path:path parameters:parameters];
}
if (self.acceptHeaderValue) [request setValue:self.acceptHeaderValue forHTTPHeaderField:@"Accept"];

return request;
}
Expand Down Expand Up @@ -353,8 +348,6 @@ - (NSMutableURLRequest *)multipartFormRequestWithObject:(id)object
requestParameters = parameters;
}
NSMutableURLRequest *multipartRequest = [self.HTTPClient multipartFormRequestWithMethod:stringMethod path:requestPath parameters:requestParameters constructingBodyWithBlock:block];
if (self.acceptHeaderValue) [multipartRequest setValue:self.acceptHeaderValue forHTTPHeaderField:@"Accept"];

return multipartRequest;
}

Expand Down
28 changes: 0 additions & 28 deletions Tests/Logic/ObjectMapping/RKObjectManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -401,34 +401,6 @@ - (void)testAFHTTPClientCanModifyRequestsBuiltByObjectManager
expect([request allHTTPHeaderFields][@"Accept"]).to.equal(@"text/html");
}

- (void)testDefaultAcceptHeaderOfObjectManagerOverridesValueOfHTTPClient
{
RKTestAFHTTPClient *testClient = [[RKTestAFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://test.com"]];
RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:testClient];
[manager setAcceptHeaderWithMIMEType:@"application/json"];
RKHuman *temporaryHuman = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
NSURLRequest *request = [manager requestWithObject:temporaryHuman method:RKRequestMethodPATCH path:@"/the/path" parameters:@{@"key": @"value"}];

expect([request.URL absoluteString]).to.equal(@"http://test.com/the/path");
expect(request.HTTPMethod).to.equal(@"PATCH");
expect([request allHTTPHeaderFields][@"test"]).to.equal(@"value");
expect([request allHTTPHeaderFields][@"Accept"]).to.equal(@"application/json");
}

- (void)testDefaultAcceptHeaderOfObjectManagerOverridesValueOfHTTPClientOnMultipartRequests
{
RKTestAFHTTPClient *testClient = [[RKTestAFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://test.com"]];
RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:testClient];
[manager setAcceptHeaderWithMIMEType:@"application/json"];
RKHuman *temporaryHuman = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
NSURLRequest *request = [manager multipartFormRequestWithObject:temporaryHuman method:RKRequestMethodPATCH path:@"/the/path" parameters:@{@"key": @"value"} constructingBodyWithBlock:nil];

expect([request.URL absoluteString]).to.equal(@"http://test.com/the/path");
expect(request.HTTPMethod).to.equal(@"PATCH");
expect([request allHTTPHeaderFields][@"test"]).to.equal(@"value");
expect([request allHTTPHeaderFields][@"Accept"]).to.equal(@"application/json");
}

- (void)testRegistrationOfHTTPRequestOperationClass
{
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://restkit.org"]];
Expand Down

0 comments on commit b9457ce

Please sign in to comment.