diff --git a/ChimpKit3/ChimpKit.m b/ChimpKit3/ChimpKit.m index ba98198..a4e8673 100644 --- a/ChimpKit3/ChimpKit.m +++ b/ChimpKit3/ChimpKit.m @@ -39,11 +39,11 @@ @implementation ChimpKit + (ChimpKit *)sharedKit { static dispatch_once_t pred = 0; __strong static ChimpKit *_sharedKit = nil; - + dispatch_once(&pred, ^{ _sharedKit = [[self alloc] init]; }); - + return _sharedKit; } @@ -52,7 +52,7 @@ - (id)init { self.timeoutInterval = kDefaultTimeoutInterval; self.requests = [[NSMutableDictionary alloc] init]; } - + return self; } @@ -65,13 +65,13 @@ - (NSURLSession *)urlSession { delegate:self delegateQueue:nil]; } - + return _urlSession; } - (void)setApiKey:(NSString *)apiKey { _apiKey = apiKey; - + if (_apiKey) { // Parse out the datacenter and template it into the URL. NSArray *apiKeyParts = [_apiKey componentsSeparatedByString:@"-"]; @@ -94,7 +94,7 @@ - (NSUInteger)callApiMethod:(NSString *)aMethod withApiKey:(NSString *)anApiKey if (aHandler == nil) { return 0; } - + return [self callApiMethod:aMethod withApiKey:anApiKey params:someParams andCompletionHandler:aHandler orDelegate:nil]; } @@ -106,28 +106,28 @@ - (NSUInteger)callApiMethod:(NSString *)aMethod withApiKey:(NSString *)anApiKey if (aDelegate == nil) { return 0; } - + return [self callApiMethod:aMethod withApiKey:anApiKey params:someParams andCompletionHandler:nil orDelegate:aDelegate]; } - (NSUInteger)callApiMethod:(NSString *)aMethod withApiKey:(NSString *)anApiKey params:(NSDictionary *)someParams andCompletionHandler:(ChimpKitRequestCompletionBlock)aHandler orDelegate:(id)aDelegate { if ((anApiKey == nil) && (self.apiKey == nil)) { NSError *error = [NSError errorWithDomain:kErrorDomain code:kChimpKitErrorInvalidAPIKey userInfo:nil]; - + if (aDelegate && [aDelegate respondsToSelector:@selector(ckRequestFailedWithIdentifier:andError:)]) { [aDelegate ckRequestFailedWithIdentifier:0 andError:error]; } - + if (aHandler) { aHandler(nil, nil, error); } - + return 0; } - + NSString *urlString = nil; NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:someParams]; - + if (anApiKey) { NSArray *apiKeyParts = [anApiKey componentsSeparatedByString:@"-"]; if ([apiKeyParts count] > 1) { @@ -139,53 +139,58 @@ - (NSUInteger)callApiMethod:(NSString *)aMethod withApiKey:(NSString *)anApiKey if (aDelegate && [aDelegate respondsToSelector:@selector(ckRequestFailedWithIdentifier:andError:)]) { [aDelegate ckRequestFailedWithIdentifier:0 andError:error]; } - + if (aHandler) { aHandler(nil, nil, error); } - + return 0; } - + [params setValue:anApiKey forKey:@"apikey"]; } else if (self.apiKey) { urlString = [NSString stringWithFormat:@"%@%@", self.apiURL, aMethod]; [params setValue:self.apiKey forKey:@"apikey"]; } - + if (kCKDebug) NSLog(@"URL: %@", urlString); - + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:self.timeoutInterval]; - + [request setHTTPMethod:@"POST"]; [request setHTTPBody:[self encodeRequestParams:params]]; - + + if ([params valueForKey:@"language"]) { + [request addValue:[params valueForKey:@"language"] + forHTTPHeaderField:@"Accept-Language"]; + } + NSURLSessionDataTask *dataTask = [self.urlSession dataTaskWithRequest:request]; - + ChimpKitRequestWrapper *requestWrapper = [[ChimpKitRequestWrapper alloc] init]; - + requestWrapper.dataTask = dataTask; requestWrapper.delegate = aDelegate; requestWrapper.completionHandler = aHandler; - + [dataTask resume]; - + dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; }); - + [self.requests setObject:requestWrapper forKey:[NSNumber numberWithUnsignedInteger:[dataTask taskIdentifier]]]; - + return [dataTask taskIdentifier]; } - (void)cancelRequestWithIdentifier:(NSUInteger)identifier { ChimpKitRequestWrapper *requestWrapper = [self.requests objectForKey:[NSNumber numberWithUnsignedInteger:identifier]]; - + [requestWrapper.dataTask cancel]; - + [self.requests removeObjectForKey:[NSNumber numberWithUnsignedInteger:identifier]]; } @@ -194,7 +199,7 @@ - (void)cancelRequestWithIdentifier:(NSUInteger)identifier { - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend { ChimpKitRequestWrapper *requestWrapper = [self.requests objectForKey:[NSNumber numberWithUnsignedInteger:[task taskIdentifier]]]; - + if (requestWrapper.delegate && [requestWrapper.delegate respondsToSelector:@selector(ckRequestIdentifier:didUploadBytes:outOfBytes:)]) { [requestWrapper.delegate ckRequestIdentifier:[task taskIdentifier] didUploadBytes:totalBytesSent @@ -211,9 +216,9 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; }); - + ChimpKitRequestWrapper *requestWrapper = [self.requests objectForKey:[NSNumber numberWithUnsignedInteger:[task taskIdentifier]]]; - + if (requestWrapper.completionHandler) { requestWrapper.completionHandler(task.response, requestWrapper.receivedData, error); } else { @@ -230,7 +235,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp } } } - + [self.requests removeObjectForKey:[NSNumber numberWithUnsignedInteger:[task taskIdentifier]]]; } @@ -240,9 +245,9 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp - (NSMutableData *)encodeRequestParams:(NSDictionary *)params { NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - + NSMutableData *postData = [NSMutableData dataWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]; - + return postData; } @@ -256,7 +261,7 @@ - (id)init { if (self = [super init]) { self.receivedData = [[NSMutableData alloc] init]; } - + return self; }