nst / STHTTPRequest

Obj-C / Cocoa HTTP requests for humans
BSD 3-Clause "New" or "Revised" License
825 stars 82 forks source link

setRequestHeaders could accept NSDictionary instead of NSMutableDictionary #23

Closed jasperblues closed 10 years ago

jasperblues commented 10 years ago

This time, I would like to set my request headers like this:

 [request setRequestHeaders:@{
     @"q"           : city,
     @"format"      : @"xml",
     @"num_of_days" : [NSString stringWithFormat:@"%i", _daysToRetrieve],
     @"key"         : _apiKey
}];

. . rather than use the convenience request setHeaderWithName:value] methods.

However request headers requires a NSMutableDictionary. I can create a mutable copy, but perhaps it would be even better if this was done internally. Eg:

- (void)setRequestHeaders:(NSDictionary *)headers {
    _headers = [headers mutableCopy];
}
nst commented 10 years ago

We could make requestHeaders a NSDictionary instance, and change the following two methods accordingly:

- (void)setHeaderWithName:(NSString *)name value:(NSString *)value {
    if(name == nil || value == nil) return;

    NSMutableDictionary *md = [_requestHeaders mutableCopy];
    [md setObject:value forKey:name];
    self.requestHeaders = [NSDictionary dictionaryWithDictionary:md];
}

- (void)removeHeaderWithName:(NSString *)name {
    if(name == nil) return;

    NSMutableDictionary *md = [_requestHeaders mutableCopy];
    [md removeObjectForKey:name];
    self.requestHeaders = [NSDictionary dictionaryWithDictionary:md];
}

However, it would break existing code modifying the mutable dictionary.

Also, it wouldn't be consistent to have an immutable dictionary and methods to add and remove headers.

That's why I think it's best if you just convert your dictionary into a mutable one.