Commit b8be173a by Brent Gulanowski

Add proper initializer to BUYCustomerToken.

parent 426f4dbe
......@@ -115,7 +115,7 @@
- (NSOperation *)renewCustomerTokenCallback:(BUYDataTokenBlock)block
{
if (self.customerToken) {
NSURL *url = [self urlForCustomersTokenRenewalWithID:self.customerToken.customerID];
NSURL *url = [self urlForCustomersTokenRenewal];
return [self putRequestForURL:url object:nil completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSString *accessToken = nil;
......@@ -137,7 +137,7 @@
- (NSOperation *)logoutCustomerCallback:(BUYDataStatusBlock)block
{
NSURL *url = [self urlForLoggedInCustomer];
NSURL *url = [self urlForLoggedInCustomerToken];
return [self deleteRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error);
}];
......@@ -160,7 +160,7 @@
- (NSOperation *)getOrdersForCustomerCallback:(BUYDataOrdersBlock)block
{
NSURL *url = [self urlForLoggedInCustomer];
NSURL *url = [self urlForCustomersOrders];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) {
NSArray *orders = [self.modelManager ordersWithJSONDictionary:json];
......@@ -173,7 +173,7 @@
- (NSOperation *)getOrderWithID:(NSNumber *)orderID callback:(BUYDataOrderBlock)block
{
NSURL *url = [self urlForCustomersOrdersWithID:self.customerToken.customerID orderID:orderID];
NSURL *url = [self urlForCustomersOrdersWithOrderID:orderID];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) {
BUYOrder *order = [self.modelManager orderWithJSONDictionary:json];
......
......@@ -50,13 +50,14 @@
- (NSURL *)urlForCustomersToken;
- (NSURL *)urlForCustomersPasswordRecovery;
- (NSURL *)urlForLoggedInCustomer;
- (NSURL *)urlForLoggedInCustomerToken;
- (NSURL *)urlForCustomersTokenRenewal;
- (NSURL *)urlForCustomersOrders;
- (NSURL *)urlForCustomersOrdersWithOrderID:(NSNumber *)orderID;
- (NSURL *)urlForCustomersOrdersWithID:(NSString *)identifier;
- (NSURL *)urlForCustomersOrdersWithID:(NSString *)identifier orderID:(NSNumber *)orderID;
- (NSURL *)urlForCustomersWithID:(NSString *)identifier;
- (NSURL *)urlForCustomersActivationWithID:(NSString *)identifier parameters:(NSDictionary *)parameters;
- (NSURL *)urlForCustomersTokenWithID:(NSString *)customerID;
- (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID;
- (NSURL *)urlForCustomersPasswordResetWithID:(NSString *)identifier parameters:(NSDictionary *)parameters;
- (NSURL *)urlForCustomersAddressesWithID:(NSString *)customerID;
......
......@@ -191,7 +191,12 @@
- (NSURL *)urlForLoggedInCustomer
{
return [self urlForCustomersWithID:self.customerToken.customerID];
return [self urlForCustomersWithID:[self.customerToken.customerID stringValue]];
}
- (NSURL *)urlForLoggedInCustomerToken
{
return [[[self urlForLoggedInCustomer] appendPath:@"/customer_token"] appendExtension];
}
#pragma mark - Customer With ID -
......@@ -201,14 +206,14 @@
return [[[self urlForCustomers] appendPath:identifier] appendExtension];
}
- (NSURL *)urlForCustomersOrdersWithID:(NSString *)identifier
- (NSURL *)urlForCustomersOrders
{
return [[[self urlForCustomersWithID:identifier] appendPath:@"/orders"] appendExtension];
return [[[self urlForLoggedInCustomer] appendPath:@"/orders"] appendExtension];
}
- (NSURL *)urlForCustomersOrdersWithID:(NSString *)identifier orderID:(NSNumber *)orderID
- (NSURL *)urlForCustomersOrdersWithOrderID:(NSNumber *)orderID
{
return [[[self urlForCustomersOrdersWithID:identifier] appendIdentifier:orderID] appendExtension];
return [[[self urlForCustomersOrders] appendIdentifier:orderID] appendExtension];
}
- (NSURL *)urlForCustomersActivationWithID:(NSString *)identifier parameters:(NSDictionary *)parameters
......@@ -216,14 +221,9 @@
return [[[[self urlForCustomersWithID:identifier] appendPath:@"/activate"] appendParameters:parameters] appendExtension];
}
- (NSURL *)urlForCustomersTokenWithID:(NSString *)customerID
{
return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token"] appendExtension];
}
- (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID
- (NSURL *)urlForCustomersTokenRenewal
{
return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token/renew"] appendExtension];
return [[[self urlForLoggedInCustomer] appendPath:@"/customer_token/renew"] appendExtension];
}
- (NSURL *)urlForCustomersPasswordResetWithID:(NSString *)identifier parameters:(NSDictionary *)parameters
......
......@@ -30,9 +30,11 @@
@property (nonatomic, copy, readonly) NSString *accessToken;
@property (nonatomic, copy, readonly) NSDate *expiry;
@property (nonatomic, copy, readonly) NSString *customerID;
@property (nonatomic, copy, readonly) NSNumber *customerID;
@property (nonatomic, copy, readonly) NSDictionary *JSONDictionary;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithCustomerID:(NSNumber *)customerID accessToken:(NSString *)accessToken expiry:(NSDate *)expiry;
+ (BUYCustomerToken *)customerTokenWithJSON:(NSDictionary *)json;
@end
......@@ -34,23 +34,31 @@ static NSString * const customerIDKey = @"customer_id";
@implementation BUYCustomerToken
- (instancetype)initWithCustomerID:(NSNumber *)customerID accessToken:(NSString *)accessToken expiry:(NSDate *)expiry
{
self = [super init];
if (self) {
_customerID = customerID;
_accessToken = accessToken;
_expiry = expiry;
}
return self;
}
+ (BUYCustomerToken *)customerTokenWithJSON:(NSDictionary *)json
{
return [[[self class] alloc] initWithJSON:json];
return [[self alloc] initWithJSON:json];
}
- (instancetype)initWithJSON:(NSDictionary *)json
{
self = [super init];
if (self) {
NSDateFormatter *formatter = [NSDateFormatter dateFormatterForPublications];
NSDictionary *access = json[customerAccessTokenKey];
_accessToken = access[accessTokenKey];
_expiry = [formatter dateFromString:access[expiresAtKey]];
_customerID = [NSString stringWithFormat:@"%@", access[customerIDKey]];
}
return self;
NSDateFormatter *formatter = [NSDateFormatter dateFormatterForPublications];
NSDictionary *access = json[customerAccessTokenKey];
NSNumber *customerID = access[customerIDKey];
NSString *accessToken = access[accessTokenKey];
NSDate *expiry = [formatter dateFromString:access[expiresAtKey]];
return [self initWithCustomerID:customerID accessToken:accessToken expiry:expiry];
}
- (NSDictionary *)JSONDictionary
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment