Commit bcd3a4c0 by Dima Bart

Merge pull request #185 from Shopify/feature/customer-logout

Add customer logout and update API.
No related merge requests found
......@@ -130,6 +130,10 @@
@"https://_DOMAIN_/api/customers/customer_token.json"
);
XCTAssertEqualObjects(
[self.client urlForCustomersTokenWithID:identifier].absoluteString,
@"https://_DOMAIN_/api/customers/_ID_/customer_token.json"
);
XCTAssertEqualObjects(
[self.client urlForCustomersTokenRenewalWithID:identifier].absoluteString,
@"https://_DOMAIN_/api/customers/_ID_/customer_token/renew.json"
);
......
......@@ -426,8 +426,8 @@ NSString * const BUYFakeCustomerToken = @"dsfasdgafdg";
];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:items];
NSString *customerID = @"12345";
NSString *customerToken = @"12345";
BUYRequestOperation *task = [self.client activateCustomerWithCredentials:credentials customerID:customerID customerToken:customerToken callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
NSString *token = @"12345";
BUYRequestOperation *task = [self.client activateCustomerWithCredentials:credentials customerID:customerID token:token callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
}];
......
......@@ -40,28 +40,22 @@
@implementation BUYClientTest_Customer
#pragma mark - Tear Down -
- (void)tearDown
{
[super tearDown];
[OHHTTPStubs removeAllStubs];
}
#pragma mark - Creation -
- (void)testCustomerDuplicateEmail
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerDuplicateEmail"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerDuplicateEmail" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *passwordItem = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
BUYAccountCredentialItem *passwordConfItem = [BUYAccountCredentialItem itemWithPasswordConfirmation:self.customerPassword];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[emailItem, passwordItem, passwordConfItem]];
[self.client createCustomerWithCredentials:credentials callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
[self.client createCustomerWithCredentials:[self credentialsForCreation] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(customer);
XCTAssertNotNil(error);
......@@ -85,20 +79,10 @@
- (void)testCustomerInvalidEmailPassword
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerInvalidEmailPassword"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerInvalidEmailPassword" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:@"a"];
BUYAccountCredentialItem *passwordItem = [BUYAccountCredentialItem itemWithPassword:@"b"];
BUYAccountCredentialItem *passwordConfItem = [BUYAccountCredentialItem itemWithPasswordConfirmation:@"c"];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[emailItem, passwordItem, passwordConfItem]];
[self.client createCustomerWithCredentials:credentials callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
[self.client createCustomerWithCredentials:[self credentialsForFailure] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(customer);
XCTAssertNotNil(error);
......@@ -125,21 +109,14 @@
}];
}
#pragma mark - Auth -
- (void)testCustomerLogin
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerLogin"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *passwordItem = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[emailItem, passwordItem]];
[self.client loginCustomerWithCredentials:credentials callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
[self.client loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(error);
XCTAssertNotNil(customer);
......@@ -153,4 +130,78 @@
}];
}
- (void)testCustomerLogout
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogout" useMocks:[self shouldUseMocks]];
[self.client logoutCustomerID:customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertEqual(status, 204);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
#pragma mark - Update -
- (void)testCustomerUpdate
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[email]];
[self.client loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer * _Nullable customer, NSString * _Nullable token, NSError * _Nullable error) {
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
[self.client updateCustomerWithCredentials:credentials customerID:customer.identifier.stringValue callback:^(BUYCustomer *customer, NSError *error) {
XCTAssertNil(error);
XCTAssertNotNil(customer);
XCTAssertEqualObjects(customer.email, self.customerEmail);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
#pragma mark - Credentials -
- (BUYAccountCredentials *)credentialsForLogin
{
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *password = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
return [BUYAccountCredentials credentialsWithItems:@[email, password]];
}
- (BUYAccountCredentials *)credentialsForCreation
{
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *password = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
BUYAccountCredentialItem *password2 = [BUYAccountCredentialItem itemWithPasswordConfirmation:self.customerPassword];
return [BUYAccountCredentials credentialsWithItems:@[email, password, password2]];
}
- (BUYAccountCredentials *)credentialsForFailure
{
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:@"a"];
BUYAccountCredentialItem *password = [BUYAccountCredentialItem itemWithPassword:@"b"];
BUYAccountCredentialItem *password2 = [BUYAccountCredentialItem itemWithPasswordConfirmation:@"c"];
return [BUYAccountCredentials credentialsWithItems:@[email, password, password2]];
}
@end
......@@ -65,11 +65,7 @@
- (void)testGetProductList
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProducts_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProducts_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductsPage:0 completion:^(NSArray *products, NSUInteger page, BOOL reachedEnd, NSError *error) {
......@@ -86,11 +82,7 @@
- (void)testGetShop
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetShop_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetShop_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getShop:^(BUYShop *shop, NSError *error) {
......@@ -107,11 +99,7 @@
- (void)testGetProductById
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProduct_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProduct_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductById:self.productIds[0] completion:^(BUYProduct *product, NSError *error) {
......@@ -136,11 +124,7 @@
- (void)testGetMultipleProductByIds
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProducts_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProducts_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......@@ -171,11 +155,7 @@
- (void)testProductRequestError
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetNonexistentProduct_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetNonexistentProduct_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductById:@"asdfdsasdfdsasdfdsasdfjkllkj" completion:^(BUYProduct *product, NSError *error) {
......@@ -191,11 +171,7 @@
- (void)testCollections
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCollection_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetCollection_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollections:^(NSArray *collections, NSError *error) {
......@@ -217,11 +193,7 @@
- (void)testCollectionsFromFirstPage
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCollection_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetCollection_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
......@@ -244,11 +216,7 @@
- (void)testCollectionsFromEmptyPage
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetOutOfIndexCollectionPage_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetOutOfIndexCollectionPage_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:999 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
......@@ -274,11 +242,7 @@
XCTAssertNotNil(self.collection);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProductsInCollection_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProductsInCollection_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......@@ -298,11 +262,7 @@
- (void)testValidTags
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetValidTag_0"];
}];
[OHHTTPStubs stubUsingResponseWithKey:@"testGetValidTag_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......
......@@ -54,7 +54,7 @@
- (void)testInit
{
BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
// We don't start the session
}];
......@@ -357,7 +357,7 @@
- (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation completion:(dispatch_block_t)completion
{
return [self operationFulfillingExpectation:expectation responseCompletion:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self operationFulfillingExpectation:expectation responseCompletion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (completion) {
completion();
}
......@@ -366,7 +366,7 @@
- (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation responseCompletion:(void(^)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error))completion
{
BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self asyncMain:^{
if (completion) {
completion(json, (id)response, error);
......
......@@ -26,8 +26,15 @@
#import <OHHTTPStubs/OHHTTPStubs.h>
@interface OHHTTPStubsResponse (Helpers)
@interface OHHTTPStubsResponse (Buy)
+ (instancetype)responseWithKey:(NSString *)key;
@end
@interface OHHTTPStubs (Buy)
+ (void)stubUsingResponseWithKey:(NSString *)key;
+ (void)stubUsingResponseWithKey:(NSString *)key useMocks:(BOOL)useMocks;
@end
......@@ -26,28 +26,49 @@
#import "OHHTTPStubsResponse+Helpers.h"
static NSDictionary *JSONMock;
@implementation OHHTTPStubsResponse (Buy)
@implementation OHHTTPStubsResponse (Helpers)
+ (instancetype)responseWithKey:(NSString *)key
+ (NSDictionary *)mockResponses
{
static NSDictionary *dictionary = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *jsonPath = [bundle pathForResource:@"mocked_responses" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
JSONMock = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
});
NSDictionary *json = JSONMock[key];
NSData *data = [json[@"body"] dataUsingEncoding:NSUTF8StringEncoding];
int code = [json[@"code"] intValue];
return dictionary;
}
+ (instancetype)responseWithKey:(NSString *)key
{
NSDictionary *mocks = [self mockResponses];
NSDictionary *json = mocks[key];
NSData *data = [json[@"body"] dataUsingEncoding:NSUTF8StringEncoding];
int code = [json[@"code"] intValue];
return [OHHTTPStubsResponse responseWithData:data statusCode:code headers:nil];
}
@end
@implementation OHHTTPStubs (Buy)
+ (void)stubUsingResponseWithKey:(NSString *)key
{
[self stubUsingResponseWithKey:key useMocks:YES];
}
+ (void)stubUsingResponseWithKey:(NSString *)key useMocks:(BOOL)useMocks
{
if (useMocks) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:key];
}];
}
}
@end
......@@ -123,5 +123,6 @@
"testOutOfStockVariant":{"body":"{\"errors\":{\"checkout\":{\"line_items\":[null,{\"quantity\":[{\"code\":\"not_enough_in_stock\",\"message\":\"Not enough items available. Only 0 left.\",\"options\":{\"remaining\":0}}]},{\"quantity\":[{\"code\":\"not_enough_in_stock\",\"message\":\"Not enough items available. Only 0 left.\",\"options\":{\"remaining\":0}}]}],\"source_name\":[],\"reservation_time\":[]}}}"},
"testCustomerDuplicateEmail":{"body":"{\"errors\":{\"customer\":{\"email\":[{\"code\":\"taken\",\"message\":\"has already been taken\",\"options\":{\"rescue_from_duplicate\":true,\"value\":\"asd@asd.com\"}}]}}}"},
"testCustomerInvalidEmailPassword":{"body":"{\"errors\":{\"customer\":{\"password\":[{\"code\":\"too_short\",\"message\":\"is too short (minimum is 5 characters)\",\"options\":{\"count\":5}}],\"password_confirmation\":[{\"code\":\"confirmation\",\"message\":\"doesn't match Password\",\"options\":{\"attribute\":\"Password\"}}],\"email\":[{\"code\":\"invalid\",\"message\":\"is invalid\",\"options\":{}}]}}}"},
"testCustomerLogout":{"body":"{}","code":204,"message":"OK"},
"testCustomerLogin":{"body":"{\"customer\":{\"id\":2529265094,\"email\":\"asd@asd.com\",\"default_address\":{\"id\":2839567814,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"company\":\"Adidas\",\"address1\":\"Sass\",\"address2\":\"12\",\"city\":\"Qsdasd\",\"province\":null,\"country\":\"Bouvet Island\",\"zip\":\"24124124\",\"phone\":\"123124124\",\"name\":\"Fast Add\",\"province_code\":null,\"country_code\":\"BV\",\"country_name\":\"Bouvet Island\",\"default\":true},\"verified_email\":true,\"accepts_marketing\":false,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"orders_count\":9,\"total_spent\":\"375.00\",\"created_at\":\"2016-02-16T14:42:24-05:00\",\"updated_at\":\"2016-03-10T16:34:04-05:00\",\"state\":\"enabled\",\"last_order_id\":2566266118,\"last_order_name\":\"#1137\",\"addresses\":[{\"id\":2785988486,\"first_name\":\"AA\",\"last_name\":\"A\",\"phone\":\"\",\"company\":\"\",\"address1\":\"Assiniboine Road\",\"address2\":\"\",\"city\":\"Toronto\",\"province\":\"Ontario\",\"province_code\":\"ON\",\"country\":\"Canada\",\"country_code\":\"CA\",\"zip\":\"M3J1E1\"},{\"id\":2839567814,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"phone\":\"123124124\",\"company\":\"Adidas\",\"address1\":\"Sass\",\"address2\":\"12\",\"city\":\"Qsdasd\",\"province\":null,\"province_code\":null,\"country\":\"Bouvet Island\",\"country_code\":\"BV\",\"zip\":\"24124124\"}],\"multipass_identifier\":null,\"tax_exempt\":false}}","code":200,"message":"OK"}
}
......@@ -48,7 +48,7 @@
- (void)handleCheckoutResponse:(NSDictionary *)json error:(NSError *)error block:(BUYDataCheckoutBlock)block
{
BUYCheckout *checkout = nil;
if (!error) {
if (json && !error) {
checkout = [self.modelManager insertCheckoutWithJSONDictionary:json[@"checkout"]];
}
block(checkout, error);
......@@ -87,7 +87,7 @@
- (BUYRequestOperation *)postCheckout:(NSDictionary *)checkoutJSON completion:(BUYDataCheckoutBlock)block
{
return [self postRequestForURL:[self urlForCheckouts] object:checkoutJSON completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self postRequestForURL:[self urlForCheckouts] object:checkoutJSON completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -98,9 +98,9 @@
BUYAssert(giftCardCode.length > 0, @"Failed to apply gift card code. Invalid gift card code.");
BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode];
NSURL *route = [self urlForCheckoutsUsingGiftCardWithToken:checkout.token];
NSURL *url = [self urlForCheckoutsUsingGiftCardWithToken:checkout.token];
return [self postRequestForURL:route object:giftCard completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self postRequestForURL:url object:giftCard completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:YES];
}
......@@ -113,8 +113,8 @@
BUYAssertCheckout(checkout);
BUYAssert(giftCard.identifier, @"Failed to remove gift card. Gift card must have a valid identifier.");
NSURL *route = [self urlForCheckoutsUsingGiftCard:giftCard.identifier token:checkout.token];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSURL *url = [self urlForCheckoutsUsingGiftCard:giftCard.identifier token:checkout.token];
return [self deleteRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (!error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO];
}
......@@ -148,8 +148,8 @@
{
BUYAssertCheckout(checkout);
NSURL *route = [self urlForCheckoutsWithToken:checkout.token];
return [self getRequestForURL:route start:start completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSURL *url = [self urlForCheckoutsWithToken:checkout.token];
return [self getRequestForURL:url start:start completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -158,8 +158,8 @@
{
BUYAssertCheckout(checkout);
NSURL *route = [self urlForCheckoutsWithToken:checkout.token];
return [self patchRequestForURL:route object:checkout completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSURL *url = [self urlForCheckoutsWithToken:checkout.token];
return [self patchRequestForURL:url object:checkout completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -178,8 +178,8 @@
BUYAssert(paymentToken || isFree, @"Failed to complete checkout. Checkout must have a payment token or have a payment value equal to $0.00");
NSURL *route = [self urlForCheckoutsCompletionWithToken:checkout.token];
return [self postRequestForURL:route object:[paymentToken JSONDictionary] start:NO completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSURL *url = [self urlForCheckoutsCompletionWithToken:checkout.token];
return [self postRequestForURL:url object:[paymentToken JSONDictionary] start:NO completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -210,10 +210,9 @@
- (BUYRequestOperation *)getCompletionStatusOfCheckoutToken:(NSString *)token start:(BOOL)start completion:(BUYDataStatusBlock)block
{
NSURL *route = [self urlForCheckoutsProcessingWithToken:token];
return [self getRequestForURL:route start:start completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
block([self statusForStatusCode:statusCode error:error], error);
NSURL *url = [self urlForCheckoutsProcessingWithToken:token];
return [self getRequestForURL:url start:start completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block([self statusForStatusCode:response.statusCode error:error], error);
}];
}
......@@ -223,18 +222,17 @@
{
BUYAssertCheckout(checkout);
NSURL *route = [self urlForCheckoutsShippingRatesWithToken:checkout.token parameters:@{
NSURL *url = [self urlForCheckoutsShippingRatesWithToken:checkout.token parameters:@{
@"checkout" : @"",
}];
BUYRequestOperation *operation = [self getRequestForURL:route start:NO completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRequestOperation *operation = [self getRequestForURL:url start:NO completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *shippingRates = nil;
if (json && !error) {
shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]];
}
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
block(shippingRates, [self statusForStatusCode:statusCode error:error], error);
block(shippingRates, [self statusForStatusCode:response.statusCode error:error], error);
}];
operation.pollingHandler = ^BOOL(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
......@@ -259,7 +257,7 @@
json[@"billing_address"] = [checkout.billingAddress jsonDictionaryForCheckout];
}
return [self postRequestForURL:checkout.paymentURL object:@{ @"checkout" : json } completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self postRequestForURL:checkout.paymentURL object:@{ @"checkout" : json } completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
id<BUYPaymentToken> token = nil;
if (!error) {
token = [[BUYCreditCardToken alloc] initWithPaymentSessionID:json[@"id"]];
......
......@@ -67,6 +67,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
@interface BUYClient (Customers)
#pragma mark - Getting -
/**
* GET /api/customers/:customer_id
* Gets an existing customer
......@@ -78,6 +80,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
#pragma mark - Customer -
/**
* POST /api/customers
* Creates a new customer
......@@ -93,27 +97,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/**
* POST /api/customers/customer_token
* Logs in an existing customer
* Expects email and password
* PUT /api/customers/:customer_id/activate
* Activates an unactivated customer
*
* @param credentials Credentials object containing items for required fields
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param token Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
- (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block;
/**
* POST /api/customers/recover
* Sends email for password recovery to an existing customer
* PUT /api/customers/:customer_id
* Update customer credentials represented by BUYAccountCredentials object
*
* @param email Email to send the password reset to
* @param block (BUYStatus status, NSError *error)
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param block (BUYCustomer *customer, NSError *error)
*
* @return the associated BUYRequestOperation
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block;
- (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
/**
* PUT /api/customers/:customer_id/reset
* Resets an existing customer's password
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer resetting password
* @param token Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block;
#pragma mark - Token -
/**
* PUT /api/customers/:customer_id/customer_token/renew
......@@ -126,31 +147,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/
- (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block;
#pragma mark - Login -
/**
* PUT /api/customers/:customer_id/activate
* Activates an unactivated customer
* POST /api/customers/customer_token
* Logs in an existing customer
* Expects email and password
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param customerToken Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param credentials Credentials object containing items for required fields
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block;
- (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/**
* PUT /api/customers/:customer_id/reset
* Resets an existing customer's password
* DELETE /api/customers/:customer_id/customer_token
* Logs out an existing customer
* Expects a customerID string
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer resetting password
* @param customerToken Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param customer A customerID represented by a string
* @param block (BUYStatus status, NSError *error)
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block;
- (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block;
/**
* POST /api/customers/recover
* Sends email for password recovery to an existing customer
*
* @param email Email to send the password reset to
* @param block (BUYStatus status, NSError *error)
*
* @return the associated BUYRequestOperation
*/
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block;
#pragma mark - Orders -
/**
* GET /api/customers/:customer_id/orders
......
......@@ -28,11 +28,11 @@
#import "BUYClient+Checkout.h"
#import "BUYSerializable.h"
static NSString * const BUYShopifyErrorDomain = @"shopify";
static NSString * const BUYShopifyErrorDomain = @"BUYShopifyErrorDomain";
static NSString * const BUYClientVersionString = @"1.3";
static NSString * const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token";
typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSURLResponse *response, NSError *error);
typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error);
@interface BUYClient (Internal)
......@@ -51,7 +51,6 @@ typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSURLResponse
- (BUYRequestOperation *)patchRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object start:(BOOL)start completionHandler:(BUYClientRequestJSONCompletion)completionHandler;
- (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error;
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response;
- (void)startOperation:(BUYOperation *)operation;
......
......@@ -50,6 +50,7 @@
- (NSURL *)urlForCustomersWithID:(NSString *)identifier;
- (NSURL *)urlForCustomersActivationWithID:(NSString *)identifier parameters:(NSDictionary *)parameters;
- (NSURL *)urlForCustomersToken;
- (NSURL *)urlForCustomersTokenWithID:(NSString *)customerID;
- (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID;
- (NSURL *)urlForCustomersPasswordRecovery;
- (NSURL *)urlForCustomersPasswordResetWithID:(NSString *)identifier parameters:(NSDictionary *)parameters;
......
......@@ -193,6 +193,11 @@
return [[[self urlForCustomers] appendPath:@"/customer_token"] appendExtension];
}
- (NSURL *)urlForCustomersTokenWithID:(NSString *)customerID
{
return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token"] appendExtension];
}
- (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID
{
return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token/renew"] appendExtension];
......
......@@ -49,7 +49,7 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getShop:(BUYDataShopBlock)block
{
return [self getRequestForURL:[self urlForShop] completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:[self urlForShop] completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYShop *shop = nil;
if (json && !error) {
shop = [self.modelManager insertShopWithJSONDictionary:json];
......@@ -60,12 +60,12 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block
{
NSURL *route = [self urlForProductListingsWithParameters:@{
NSURL *url = [self urlForProductListingsWithParameters:@{
@"limit" : @(self.pageSize),
@"page" : @(page),
}];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *products = nil;
if (json && !error) {
......@@ -95,11 +95,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
{
BUYAssert(productIds, @"Failed to get product by IDs. Product IDs array must not be nil.");
NSURL *route = [self urlForProductListingsWithParameters:@{
NSURL *url = [self urlForProductListingsWithParameters:@{
@"product_ids" : [productIds componentsJoinedByString:@","],
}];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *products = nil;
if (json && !error) {
......@@ -121,12 +121,12 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{
NSURL *route = [self urlForCollectionListingsWithParameters:@{
NSURL *url = [self urlForCollectionListingsWithParameters:@{
@"limit" : @(self.pageSize),
@"page" : @(page),
}];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *collections = nil;
if (json && !error) {
......@@ -145,14 +145,14 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
{
BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID.");
NSURL *route = [self urlForProductListingsWithParameters:@{
NSURL *url = [self urlForProductListingsWithParameters:@{
@"collection_id" : collectionId,
@"limit" : @(self.pageSize),
@"page" : @(page),
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
}];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *products = nil;
if (json && !error) {
......
......@@ -123,15 +123,6 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
}
}
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response
{
NSInteger statusCode = [((NSHTTPURLResponse *) response) statusCode];
if ((int)(statusCode / 100.0) != 2) { // If not a 2xx response code
return [[NSError alloc] initWithDomain:BUYShopifyErrorDomain code:statusCode userInfo:json];
}
return nil;
}
#pragma mark - Auto Starting Convenience Requests
- (BUYRequestOperation *)getRequestForURL:(NSURL *)url completionHandler:(BUYClientRequestJSONCompletion)completionHandler
......@@ -221,7 +212,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
request.HTTPMethod = method;
BUYRequestOperation *operation = [[BUYRequestOperation alloc] initWithSession:self.session request:request payload:object completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRequestOperation *operation = [[BUYRequestOperation alloc] initWithSession:self.session request:request payload:object completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self.callbackQueue addOperationWithBlock:^{
completionHandler(json, response, error);
}];
......
......@@ -26,8 +26,7 @@
#import "BUYRequestOperation.h"
#import "BUYSerializable.h"
NSString * const kShopifyError = @"shopify";
#import "BUYClient+Internal.h"
typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error);
......@@ -169,9 +168,9 @@ typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}
NSHTTPURLResponse *httpResponse = (id)response;
if (!httpResponse.successful && !error) {
error = [[NSError alloc] initWithDomain:kShopifyError code:httpResponse.statusCode userInfo:json];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (!error && !httpResponse.successful) {
error = [[NSError alloc] initWithDomain:BUYShopifyErrorDomain code:httpResponse.statusCode userInfo:json];
}
completion(json, httpResponse, error);
......
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