n1mda / sonos-objc

An Objective-C API for controlling Sonos Devices
MIT License
20 stars 24 forks source link

Feature request:Remove AFNetworking dependecy #5

Closed loretoparisi closed 9 years ago

loretoparisi commented 9 years ago

Since AFNetworking is used to execute a simple HTTP GET, I suggest to remove any dependencies of AFNetworking and replacing it with NSURLConnection:

(SonosController.m)

- (void)upnp:(NSString *)url soap_service:(NSString *)soap_service soap_action:(NSString *)soap_action soap_arguments:(NSString *)soap_arguments completion:(void (^)(NSDictionary *, NSError *))block {

like

// Set Body
    [request setHTTPBody:[post_xml dataUsingEncoding:NSUTF8StringEncoding]];

    [self getRequest:request completionBlock:^(NSData *responseObject, NSError *error) {
        if(responseObject) {
            NSDictionary *response = [XMLReader dictionaryForXMLData:responseObject error:nil];
            block(response, nil);
        } else {
            if(block) block(nil, error);
        }
    }];   

where

- (void)getRequest:(NSURLRequest *)request completionBlock:(void (^)(NSData *data, NSError *error))completionBlock
{
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               completionBlock(data,error);
                           }];
}
endoze commented 9 years ago

@loretoparisi Not to disagree, but you just suggested replacing a working piece of third party code with a deprecated api. I don't think that is a solid path forward.

If you wanted to eliminate the third party dependency with a built in set of Classes, I might suggest NSURLSession. Apple has directed developers to use this new set of apis for networking calls in place of the old NSURLConnection.

Here is a a very rough implementation of what it could look like:

#import <Foundation/Foundation.h>

@interface SonosTask : NSObject

- (NSURLSessionDataTask*)makeApiCallWithData:(NSData *)data andCompletion:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completion;

@end

@implementation SonosTask

- (NSURLSessionDataTask *)makeApiCallWithData:(NSData *)data andCompletion:(void (^)(NSData *data, NSURLResponse *respone, NSError *error))completion {
  NSURLSession *session = [NSURLSession sharedSession];

  NSMutableRequest *request = [NSMutableRequest new];
  request.HTTPMethod = @"POST" // or whatever HTTP method is necessary for the api call
  request.HTTPBody = data;

  return [session dataTaskWithRequest:request completionHandler:completion];
}

@end

And the use of this object would look something like this:

SonosTask *sonosTask = [SonosTask new];

[[sonosTask makeApiCallWithData:@{} andCompletion:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (!error) {
    // do something with data returned here
  } else {
    // handle error case here
  }
}] resume];
loretoparisi commented 9 years ago

@endoze thanks that works better, the basic idea was this :)