Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added singleton instance of PKTClient

  • Loading branch information...
commit 1e6cb318bd29ae9f842838e37eb1a70c0722d0f0 1 parent 39e84dd
@sebreh sebreh authored
View
4 PodioKit/Core/PKTClient.h
@@ -17,8 +17,12 @@ typedef void(^PKTRequestCompletionBlock)(PKTResponse *response, NSError *error);
@property (nonatomic, copy, readonly) NSString *apiKey;
@property (nonatomic, copy, readonly) NSString *apiSecret;
++ (instancetype)sharedClient;
+
- (instancetype)initWithAPIKey:(NSString *)key secret:(NSString *)secret;
+- (void)setupWithAPIKey:(NSString *)key secret:(NSString *)secret;
+
- (void)setValue:(NSString *)value forHTTPHeader:(NSString *)header;
- (void)performRequest:(PKTRequest *)request completion:(PKTRequestCompletionBlock)completion;
View
41 PodioKit/Core/PKTClient.m
@@ -27,24 +27,43 @@ @interface PKTClient ()
@implementation PKTClient
-- (instancetype)initWithAPIKey:(NSString *)key secret:(NSString *)secret {
- NSParameterAssert(key);
- NSParameterAssert(secret);
++ (instancetype)sharedClient {
+ static PKTClient *sharedClient;
+ static dispatch_once_t once;
- NSURL *baseURL = [[NSURL alloc] initWithString:kDefaultBaseURLString];
- self = [super initWithBaseURL:baseURL];
- if (!self) return nil;
+ dispatch_once(&once, ^{
+ sharedClient = [[self alloc] init];
+ });
- _apiKey = [key copy];
- _apiSecret = [secret copy];
+ return sharedClient;
+}
- self.requestSerializer = [PKTRequestSerializer serializer];
-
- return self;
+- (instancetype)init {
+ return [self initWithAPIKey:nil secret:nil];
+}
+
+- (instancetype)initWithAPIKey:(NSString *)key secret:(NSString *)secret {
+ @synchronized(self) {
+ NSURL *baseURL = [[NSURL alloc] initWithString:kDefaultBaseURLString];
+ self = [super initWithBaseURL:baseURL];
+ if (!self) return nil;
+
+ _apiKey = [key copy];
+ _apiSecret = [secret copy];
+
+ self.requestSerializer = [PKTRequestSerializer serializer];
+
+ return self;
+ }
}
#pragma mark - Public
+- (void)setupWithAPIKey:(NSString *)key secret:(NSString *)secret {
+ self.apiKey = key;
+ self.apiSecret = secret;
+}
+
- (void)setValue:(NSString *)value forHTTPHeader:(NSString *)header {
[self.requestSerializer setValue:value forHTTPHeaderField:header];
}
View
17 PodioKitTests/PKTClientTests.m
@@ -38,13 +38,26 @@ - (void)tearDown {
#pragma mark - Tests
- (void)testInit {
- PKTClient *client = [[PKTClient alloc] initWithAPIKey:kAPISecret secret:kAPISecret];
+ PKTClient *client = [[PKTClient alloc] initWithAPIKey:kAPIKey secret:kAPISecret];
- expect(client.apiKey).to.equal(kAPISecret);
+ expect(client.apiKey).to.equal(kAPIKey);
expect(client.apiSecret).to.equal(kAPISecret);
expect([client.baseURL absoluteString]).to.equal(@"https://api.podio.com");
}
+- (void)testSetupClient {
+ PKTClient *client = [PKTClient new];
+ [client setupWithAPIKey:kAPIKey secret:kAPISecret];
+
+ expect(client.apiKey).to.equal(kAPIKey);
+ expect(client.apiSecret).to.equal(kAPISecret);
+ expect([client.baseURL absoluteString]).to.equal(@"https://api.podio.com");
+}
+
+- (void)testSharedInstance {
+ expect([PKTClient sharedClient]).to.equal([PKTClient sharedClient]);
+}
+
- (void)testPerformSuccessfulRequest {
PKTRequest *request = [PKTRequest GETRequestWithPath:@"/user/status" parameters:@{@"param1": @"someValue", @"param2": @"someOtherValue"}];
Please sign in to comment.
Something went wrong with that request. Please try again.