Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

subscriber language #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 39 additions & 34 deletions ChimpKit3/ChimpKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -52,7 +52,7 @@ - (id)init {
self.timeoutInterval = kDefaultTimeoutInterval;
self.requests = [[NSMutableDictionary alloc] init];
}

return self;
}

Expand All @@ -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:@"-"];
Expand All @@ -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];
}

Expand All @@ -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<ChimpKitRequestDelegate>)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) {
Expand All @@ -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]];
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -230,7 +235,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
}
}
}

[self.requests removeObjectForKey:[NSNumber numberWithUnsignedInteger:[task taskIdentifier]]];
}

Expand All @@ -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;
}

Expand All @@ -256,7 +261,7 @@ - (id)init {
if (self = [super init]) {
self.receivedData = [[NSMutableData alloc] init];
}

return self;
}

Expand Down