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

implemented API v3 Endpoint #16

Open
wants to merge 2 commits 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
31 changes: 17 additions & 14 deletions ChimpKit3/ChimpKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "ChimpKit.h"


#define kAPI20Endpoint @"https://%@.api.mailchimp.com/2.0/"
#define kAPI30Endpoint @"https://%@.api.mailchimp.com/3.0/"
#define kErrorDomain @"com.MailChimp.ChimpKit.ErrorDomain"


Expand Down Expand Up @@ -61,7 +61,11 @@ - (id)init {

- (NSURLSession *)urlSession {
if (_urlSession == nil) {
_urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
configuration.allowsCellularAccess = YES;
configuration.timeoutIntervalForRequest = 60.0;
_urlSession = [NSURLSession sessionWithConfiguration:configuration
delegate:self
delegateQueue:nil];
}
Expand All @@ -76,7 +80,7 @@ - (void)setApiKey:(NSString *)apiKey {
// Parse out the datacenter and template it into the URL.
NSArray *apiKeyParts = [_apiKey componentsSeparatedByString:@"-"];
if ([apiKeyParts count] > 1) {
self.apiURL = [NSString stringWithFormat:kAPI20Endpoint, [apiKeyParts objectAtIndex:1]];
self.apiURL = [NSString stringWithFormat:kAPI30Endpoint, [apiKeyParts objectAtIndex:1]];
} else {
NSAssert(FALSE, @"Please provide a valid API Key");
}
Expand Down Expand Up @@ -127,30 +131,27 @@ - (NSUInteger)callApiMethod:(NSString *)aMethod withApiKey:(NSString *)anApiKey

NSString *urlString = nil;
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:someParams];

NSString *userPassword = nil;

if (anApiKey) {
NSArray *apiKeyParts = [anApiKey componentsSeparatedByString:@"-"];
if ([apiKeyParts count] > 1) {
NSString *apiURL = [NSString stringWithFormat:kAPI20Endpoint, [apiKeyParts objectAtIndex:1]];
NSString *apiURL = [NSString stringWithFormat:kAPI30Endpoint, [apiKeyParts objectAtIndex:1]];
urlString = [NSString stringWithFormat:@"%@%@", apiURL, aMethod];
} else {
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;
}

[params setValue:anApiKey forKey:@"apikey"];
userPassword = anApiKey;
} else if (self.apiKey) {
urlString = [NSString stringWithFormat:@"%@%@", self.apiURL, aMethod];
[params setValue:self.apiKey forKey:@"apikey"];
userPassword = self.apiKey;
}

if (kCKDebug) NSLog(@"URL: %@", urlString);
Expand All @@ -160,6 +161,11 @@ - (NSUInteger)callApiMethod:(NSString *)aMethod withApiKey:(NSString *)anApiKey
timeoutInterval:self.timeoutInterval];

[request setHTTPMethod:@"POST"];

NSData *basicAuthCredentials = [[NSString stringWithFormat:@"%@:%@", @"anystring", userPassword] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64AuthCredentials = [basicAuthCredentials base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
[request setValue:[NSString stringWithFormat:@"Basic %@", base64AuthCredentials] forHTTPHeaderField:@"Authorization"];

[request setHTTPBody:[self encodeRequestParams:params]];

NSURLSessionDataTask *dataTask = [self.urlSession dataTaskWithRequest:request];
Expand Down Expand Up @@ -240,13 +246,10 @@ - (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;
}


@end


Expand Down