Commit 93c81413 by Dima Bart

Factor route creation / management out of BUYClient.

parent d956de37
......@@ -44,7 +44,7 @@ extern NSString * const BUYFakeCustomerToken;
@property (nonatomic, strong) NSString *giftCardCode2;
@property (nonatomic, strong) NSString *giftCardCode3;
@property (nonatomic, strong) NSString *giftCardCodeExpired;
@property (nonatomic, strong) NSString *giftCardIdExpired;
@property (nonatomic, strong) NSNumber *giftCardIdExpired;
@property (nonatomic, strong) NSString *giftCardCodeInvalid;
@property (nonatomic, strong) NSString *discountCodeValid;
@property (nonatomic, strong) NSString *discountCodeExpired;
......
......@@ -517,7 +517,7 @@
return [OHHTTPStubsResponse responseWithKey:@"testRemovingInvalidGiftCardFromCheckout_2"];
}];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"000" }];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @(000) }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error);
......
......@@ -356,6 +356,10 @@
9A47CF201CE50EBB00A6D5BA /* BUYApplePayTestToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */; };
9A47CF221CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; };
9A47CF231CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; };
9A53317E1CE5F92A0025964B /* BUYRouter.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A53317C1CE5F9260025964B /* BUYRouter.m */; };
9A53317F1CE5F92B0025964B /* BUYRouter.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A53317C1CE5F9260025964B /* BUYRouter.m */; };
9A5331801CE5F9310025964B /* BUYRouter.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A53317B1CE5F9260025964B /* BUYRouter.h */; };
9A5331811CE5F9310025964B /* BUYRouter.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A53317B1CE5F9260025964B /* BUYRouter.h */; };
9A6B03791CDA5D4F0054C26E /* BUYAccountCredentialsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */; };
BE47340F1B66C4EF00AA721A /* BUYError.h in Headers */ = {isa = PBXBuildFile; fileRef = BE47340D1B66C4EF00AA721A /* BUYError.h */; settings = {ATTRIBUTES = (Public, ); }; };
BE4734101B66C4EF00AA721A /* BUYError.m in Sources */ = {isa = PBXBuildFile; fileRef = BE47340E1B66C4EF00AA721A /* BUYError.m */; };
......@@ -623,6 +627,8 @@
9A47CF1E1CE50EBB00A6D5BA /* BUYApplePayTestToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYApplePayTestToken.h; sourceTree = "<group>"; };
9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayTestToken.m; sourceTree = "<group>"; };
9A47CF211CE5112A00A6D5BA /* BUYAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BUYAssert.h; path = ../Additions/BUYAssert.h; sourceTree = "<group>"; };
9A53317B1CE5F9260025964B /* BUYRouter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYRouter.h; sourceTree = "<group>"; };
9A53317C1CE5F9260025964B /* BUYRouter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYRouter.m; sourceTree = "<group>"; };
9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYAccountCredentialsTests.m; sourceTree = "<group>"; };
BE33B4ED1B15FF4D0067982B /* BUYApplePayHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYApplePayHelpers.h; sourceTree = "<group>"; };
BE33B4EE1B15FF4D0067982B /* BUYApplePayHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayHelpers.m; sourceTree = "<group>"; };
......@@ -1098,6 +1104,8 @@
isa = PBXGroup;
children = (
8498DCB01CDD1B4A00BD12A8 /* BUYClient_Internal.h */,
9A53317B1CE5F9260025964B /* BUYRouter.h */,
9A53317C1CE5F9260025964B /* BUYRouter.m */,
F7FDA17019C93F6F00AF4E93 /* BUYClient.h */,
F7FDA17119C93F6F00AF4E93 /* BUYClient.m */,
8498DCB11CDD1B4A00BD12A8 /* BUYClient+Customers.h */,
......@@ -1188,6 +1196,9 @@
9032F2DB1BE9457A00BB9EEF /* BUYCheckoutAttribute.h in Headers */,
9019315E1BC5B9BC00D1134E /* BUYError.h in Headers */,
84980F371CB75C2900CFAB58 /* NSPropertyDescription+BUYAdditions.h in Headers */,
9019315F1BC5B9BC00D1134E /* BUYProductHeaderCell.h in Headers */,
9A5331801CE5F9310025964B /* BUYRouter.h in Headers */,
901931601BC5B9BC00D1134E /* BUYProductViewErrorView.h in Headers */,
901931611BC5B9BC00D1134E /* BUYClient.h in Headers */,
849810971CB7E07900CFAB58 /* BUYFlatCollectionTransformer.h in Headers */,
901931641BC5B9BC00D1134E /* BUYCartLineItem.h in Headers */,
......@@ -1281,6 +1292,9 @@
9032F2DA1BE9457A00BB9EEF /* BUYCheckoutAttribute.h in Headers */,
BE47340F1B66C4EF00AA721A /* BUYError.h in Headers */,
84980F361CB75C2900CFAB58 /* NSPropertyDescription+BUYAdditions.h in Headers */,
BEB74A6D1B5564200005A300 /* BUYProductHeaderCell.h in Headers */,
9A5331811CE5F9310025964B /* BUYRouter.h in Headers */,
903BCC7C1B7D1C2D00C21FEB /* BUYProductViewErrorView.h in Headers */,
BE9A64471B503C8B0033E558 /* BUYClient.h in Headers */,
849810961CB7E07900CFAB58 /* BUYFlatCollectionTransformer.h in Headers */,
9003969B1B601DF400226B73 /* BUYCartLineItem.h in Headers */,
......@@ -1508,6 +1522,7 @@
9A47CEFE1CE39F6100A6D5BA /* BUYCreditCardToken.m in Sources */,
901930F21BC5B9BC00D1134E /* BUYOrder.m in Sources */,
849810991CB7E07900CFAB58 /* BUYFlatCollectionTransformer.m in Sources */,
9A53317F1CE5F92B0025964B /* BUYRouter.m in Sources */,
84DD12A41CC63FE600A2442D /* _BUYLineItem.m in Sources */,
84DD12CC1CC63FEE00A2442D /* _BUYTaxLine.m in Sources */,
84DD12CA1CC63FEE00A2442D /* _BUYShippingRate.m in Sources */,
......@@ -1637,6 +1652,7 @@
9A47CEFD1CE39F6000A6D5BA /* BUYCreditCardToken.m in Sources */,
90E83BC51B9F550E00C95A1B /* BUYOrder.m in Sources */,
849810981CB7E07900CFAB58 /* BUYFlatCollectionTransformer.m in Sources */,
9A53317E1CE5F92A0025964B /* BUYRouter.m in Sources */,
84DD128C1CC63FE600A2442D /* _BUYLineItem.m in Sources */,
84DD12BE1CC63FEE00A2442D /* _BUYTaxLine.m in Sources */,
84DD12BC1CC63FEE00A2442D /* _BUYShippingRate.m in Sources */,
......
......@@ -31,6 +31,7 @@
#import "BUYAccountCredentials.h"
#import "BUYOrder.h"
#import "BUYShopifyErrorCodes.h"
#import "BUYRouter.h"
@interface BUYAuthenticatedResponse : NSObject
+ (BUYAuthenticatedResponse *)responseFromJSON:(NSDictionary *)json;
......@@ -60,8 +61,8 @@
- (NSURLSessionDataTask *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block
{
NSURLComponents *components = [self URLComponentsForCustomerWithID:customerID];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCustomersWithID:customerID];
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYCustomer *customer = nil;
if (json && !error) {
customer = [self.modelManager customerWithJSONDictionary:json];
......@@ -72,8 +73,8 @@
- (NSURLSessionDataTask *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{
NSURLComponents *components = [self URLComponentsForCustomers];
return [self postRequestForURL:components.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCustomers];
return [self postRequestForURL:route.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (json && !error) {
[self createTokenForCustomerWithCredentials:credentials customerJSON:json callback:block];
}
......@@ -85,8 +86,8 @@
- (NSURLSessionDataTask *)createTokenForCustomerWithCredentials:(BUYAccountCredentials *)credentials customerJSON:(NSDictionary *)customerJSON callback:(BUYDataCustomerTokenBlock)block
{
NSURLComponents *components = [self URLComponentsForCustomerLogin];
return [self postRequestForURL:components.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCustomersToken];
return [self postRequestForURL:route.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (json && !error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseFromJSON:json];
self.customerToken = authenticatedResponse.accessToken;
......@@ -114,9 +115,8 @@
- (NSURLSessionDataTask *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataCheckoutStatusBlock)block
{
NSURLComponents *components = [self URLComponentsForPasswordReset];
return [self postRequestForURL:components.URL object:@{@"email": email} completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCustomersPasswordRecovery];
return [self postRequestForURL:route.URL object:@{@"email": email} completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (!error) {
......@@ -130,9 +130,9 @@
- (NSURLSessionDataTask *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block
{
if (self.customerToken) {
NSURLComponents *components = [self URLComponentsForTokenRenewalWithID:customerID];
BUYRoute *route = [self.router routeForCustomersTokenRenewalWithID:customerID];
return [self putRequestForURL:components.URL object:nil completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self putRequestForURL:route.URL object:nil completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSString *accessToken = nil;
if (json && !error) {
......@@ -155,9 +155,12 @@
- (NSURLSessionDataTask *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block
{
NSURLComponents *components = [self URLComponentsForCustomerActivationWithID:customerID customerToken:customerToken];
BUYRoute *route = [self.router routeForCustomersActivationWithID:customerID];
route.queryItems = @{
@"token": customerToken,
};
return [self putRequestForURL:components.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self putRequestForURL:route.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSString *email = json[@"customer"][@"email"];
if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
......@@ -171,9 +174,12 @@
- (NSURLSessionDataTask *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block
{
NSURLComponents *components = [self URLComponentsForCustomerPasswordResetWithCustomerID:customerID customerToken:customerToken];
BUYRoute *route = [self.router routeForCustomersPasswordResetWithID:customerID];
route.queryItems = @{
@"token": customerToken,
};
return [self putRequestForURL:components.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self putRequestForURL:route.URL object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSString *email = json[@"customer"][@"email"];
if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
......@@ -187,8 +193,8 @@
- (NSURLSessionDataTask *)getOrdersForCustomerWithCallback:(BUYDataOrdersBlock)block
{
NSURLComponents *components = [self URLComponentsForCustomerOrders];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCustomersOrders];
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (json && !error) {
NSArray *orders = [self.modelManager ordersWithJSONDictionary:json];
block(orders, error);
......@@ -198,68 +204,4 @@
}];
}
#pragma mark - URL Formatting
- (NSURLComponents *)URLComponentsForCustomers
{
return [self customerURLComponentsAppendingPath:nil];
}
- (NSURLComponents *)URLComponentsForCustomerWithID:(NSString *)customerID
{
return [self customerURLComponentsAppendingPath:customerID];
}
- (NSURLComponents *)URLComponentsForCustomerLogin
{
return [self customerURLComponentsAppendingPath:@"customer_token"];
}
- (NSURLComponents *)URLComponentsForCustomerActivationWithID:(NSString *)customerID customerToken:(NSString *)customerToken
{
NSDictionary *queryItems = @{ @"token": customerToken };
NSString *path = [NSString stringWithFormat:@"%@/activate", customerID];
return [self customerURLComponentsAppendingPath:path queryItems:queryItems];
}
- (NSURLComponents *)URLComponentsForCustomerPasswordResetWithCustomerID:(NSString *)customerID customerToken:(NSString *)customerToken
{
NSDictionary *queryItems = @{ @"token": customerToken };
NSString *path = [NSString stringWithFormat:@"%@/reset", customerID];
return [self customerURLComponentsAppendingPath:path queryItems:queryItems];
}
- (NSURLComponents *)URLComponentsForPasswordReset
{
return [self customerURLComponentsAppendingPath:@"recover" queryItems:nil];
}
- (NSURLComponents *)URLComponentsForTokenRenewalWithID:(NSString *)customerID
{
NSString *path = [NSString stringWithFormat:@"%@/customer_token/renew", customerID];
return [self customerURLComponentsAppendingPath:path queryItems:nil];
}
- (NSURLComponents *)URLComponentsForCustomerOrders
{
return [self customerURLComponentsAppendingPath:@"orders" queryItems:nil];
}
#pragma mark - Convenience methods
- (NSURLComponents *)customerURLComponentsAppendingPath:(NSString *)path
{
return [self customerURLComponentsAppendingPath:path queryItems:nil];
}
- (NSURLComponents *)customerURLComponentsAppendingPath:(NSString *)path queryItems:(NSDictionary *)queryItems
{
return [self URLComponentsForAPIPath:@"customers" appendingPath:path queryItems:queryItems];
}
- (NSString *)accessTokenFromHeaders:(NSDictionary *)headers
{
return [headers valueForKey:BUYClientCustomerAccessToken];
}
@end
......@@ -45,6 +45,7 @@
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "NSURLComponents+BUYAdditions.h"
#import "BUYRouter.h"
#define kGET @"GET"
#define kPOST @"POST"
......@@ -75,6 +76,7 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSString *merchantId;
@property (nonatomic, strong) BUYRouter *router;
@end
......@@ -95,6 +97,7 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
self = [super init];
if (self) {
self.router = [[BUYRouter alloc] initWithShopDomain:shopDomain appID:appId];
self.modelManager = [BUYModelManager modelManager];
self.shopDomain = shopDomain;
self.apiKey = apiKey;
......@@ -132,9 +135,7 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)getShop:(BUYDataShopBlock)block
{
NSURLComponents *shopComponents = [self URLComponentsForShop];
return [self getRequestForURL:shopComponents.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:[self.router routeForShop].URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYShop *shop = nil;
if (json && !error) {
shop = [self.modelManager insertShopWithJSONDictionary:json];
......@@ -145,13 +146,13 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block
{
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathProductPublications
queryItems:@{
@"limit" : @(self.pageSize).stringValue,
@"page" : @(page).stringValue,
}];
BUYRoute *route = [self.router routeForProductListings];
route.queryItems = @{
@"limit" : @(self.pageSize),
@"page" : @(page),
};
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *products = nil;
if (json && !error) {
......@@ -181,12 +182,12 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
{
BUYAssert(productIds, @"Failed to get product by IDs. Product IDs array must not be nil.");
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathProductPublications
queryItems:@{
@"product_ids" : [productIds componentsJoinedByString:@","]
}];
BUYRoute *route = [self.router routeForProductListings];
route.queryItems = @{
@"product_ids" : [productIds componentsJoinedByString:@","],
};
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *products = nil;
if (json && !error) {
......@@ -208,12 +209,13 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathCollectionPublications
queryItems:@{
@"limit" : @(self.pageSize).stringValue,
@"page" : @(page).stringValue,
}];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCollectionListings];
route.queryItems = @{
@"limit" : @(self.pageSize),
@"page" : @(page),
};
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *collections = nil;
if (json && !error) {
......@@ -232,15 +234,15 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
{
BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID.");
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathProductPublications
queryItems:@{
@"collection_id" : collectionId.stringValue,
@"limit" : @(self.pageSize).stringValue,
@"page" : @(page).stringValue,
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
}];
BUYRoute *route = [self.router routeForProductListings];
route.queryItems = @{
@"collection_id" : collectionId,
@"limit" : @(self.pageSize),
@"page" : @(page),
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
};
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *products = nil;
if (json && !error) {
......@@ -250,48 +252,6 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
}];
}
#pragma mark - URL Components
- (NSURLComponents *)URLComponentsForChannelsAppendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems
{
return [self URLComponentsForAPIPath:[NSString stringWithFormat:@"apps/%@", self.appId] appendingPath:appendingPath queryItems:queryItems];
}
- (NSURLComponents *)URLComponentsForCheckoutsAppendingPath:(NSString *)appendingPath checkoutToken:(NSString *)checkoutToken queryItems:(NSDictionary*)queryItems
{
NSString *apiPath = @"checkouts";
if (checkoutToken) {
apiPath = [NSString pathWithComponents:@[apiPath, checkoutToken]];
}
return [self URLComponentsForAPIPath:[apiPath copy] appendingPath:appendingPath queryItems:queryItems];
}
- (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath appendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems
{
NSMutableArray *pathComponents = [NSMutableArray array];
[pathComponents addObject:@"/api"];
[pathComponents addObject:apiPath];
if (appendingPath) {
[pathComponents addObject:appendingPath];
}
return [self URLComponentsForPathComponents:pathComponents queryItems:queryItems];
}
- (NSURLComponents *)URLComponentsForShop
{
return [self URLComponentsForPathComponents:@[@"/meta"] queryItems:nil];
}
- (NSURLComponents *)URLComponentsForPathComponents:(NSArray*)pathComponents queryItems:(NSDictionary*)queryItems
{
NSURLComponents *components = [[NSURLComponents alloc] init];
components.scheme = @"https";
components.host = self.shopDomain;
components.path = [[NSString pathWithComponents:pathComponents] stringByAppendingPathExtension:@"json"];
[components setQueryItemsWithDictionary:queryItems];
return components;
}
#pragma mark - Checkout
- (void)handleCheckoutResponse:(NSDictionary *)json error:(NSError *)error block:(BUYDataCheckoutBlock)block
......@@ -336,9 +296,7 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)postCheckout:(NSDictionary *)checkoutJSON completion:(BUYDataCheckoutBlock)block
{
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:nil checkoutToken:nil queryItems:nil];
return [self postRequestForURL:components.URL object:checkoutJSON completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self postRequestForURL:[self.router routeForCheckouts].URL object:checkoutJSON completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -349,11 +307,9 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
BUYAssert(giftCardCode.length > 0, @"Failed to apply gift card code. Invalid gift card code.");
BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode];
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"gift_cards"
checkoutToken:checkout.token
queryItems:nil];
BUYRoute *route = [self.router routeForCheckoutsUsingGiftCardWithToken:checkout.token];
return [self postRequestForURL:components.URL object:giftCard completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
return [self postRequestForURL:route.URL object:giftCard completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (json && !error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:YES];
}
......@@ -366,10 +322,8 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
BUYAssertCheckout(checkout);
BUYAssert(giftCard.identifier, @"Failed to remove gift card. Gift card must have a valid identifier.");
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:[NSString stringWithFormat:@"gift_cards/%@", giftCard.identifier]
checkoutToken:checkout.token
queryItems:nil];
return [self deleteRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCheckoutsUsingGiftCard:giftCard.identifier token:checkout.token];
return [self deleteRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (!error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO];
}
......@@ -397,10 +351,9 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)getCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:nil
checkoutToken:checkout.token
queryItems:nil];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCheckoutsWithToken:checkout.token];
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -409,10 +362,8 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
{
BUYAssertCheckout(checkout);
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:nil
checkoutToken:checkout.token
queryItems:nil];
return [self patchRequestForURL:components.URL object:checkout completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCheckoutsWithToken:checkout.token];
return [self patchRequestForURL:route.URL object:checkout completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -425,11 +376,8 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
BUYAssert(paymentToken || isFree, @"Failed to complete checkout. Checkout must have a payment token or have a payment value equal to $0.00");
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"complete"
checkoutToken:checkout.token
queryItems:nil];
return [self postRequestForURL:components.URL object:[paymentToken JSONDictionary] completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCheckoutsCompletionWithToken:checkout.token];
return [self postRequestForURL:route.URL object:[paymentToken JSONDictionary] completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
......@@ -460,10 +408,8 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)getCompletionStatusOfCheckoutToken:(NSString *)token completion:(BUYDataCheckoutStatusBlock)block
{
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"processing"
checkoutToken:token
queryItems:nil];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCheckoutsProcessingWithToken:token];
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
block([BUYClient statusForStatusCode:statusCode error:error], error);
}];
......@@ -474,8 +420,13 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
- (NSURLSessionDataTask *)getShippingRatesForCheckout:(BUYCheckout *)checkout completion:(BUYDataShippingRatesBlock)block
{
BUYAssertCheckout(checkout);
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"shipping_rates" checkoutToken:checkout.token queryItems:@{ @"checkout" : @"" }];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYRoute *route = [self.router routeForCheckoutsShippingRatesWithToken:checkout.token];
route.queryItems = @{
@"checkout" : @"",
};
return [self getRequestForURL:route.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *shippingRates = nil;
if (json && !error) {
shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]];
......
......@@ -29,14 +29,16 @@
extern NSString *const kShopifyError;
@class BUYRouter;
@interface BUYClient (Internal)
@property (nonatomic, strong) BUYRouter *router;
- (NSURLSessionDataTask *)postRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)putRequestForURL:(NSURL *)url object:(id<BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)getRequestForURL:(NSURL *)url completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath appendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems;
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response;
@end
//
// BUYRouter.h
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
@interface BUYRoute : NSObject
@property (strong, nonatomic, readonly) NSURL *URL;
@property (strong, nonatomic, readonly) NSDictionary *queryItems;
- (void)setQueryItems:(NSDictionary *)queryItems;
@end
@interface BUYRouter : NSObject
- (instancetype)initWithShopDomain:(NSString *)shopDomain appID:(NSString *)appID;
- (BUYRoute *)routeForAPI;
- (BUYRoute *)routeForApps;
- (BUYRoute *)routeForShop;
- (BUYRoute *)routeForProductListings;
- (BUYRoute *)routeForCollectionListings;
- (BUYRoute *)routeForCheckouts;
- (BUYRoute *)routeForCheckoutsWithToken:(NSString *)token;
- (BUYRoute *)routeForCheckoutsProcessingWithToken:(NSString *)token;
- (BUYRoute *)routeForCheckoutsCompletionWithToken:(NSString *)token;
- (BUYRoute *)routeForCheckoutsShippingRatesWithToken:(NSString *)token;
- (BUYRoute *)routeForCheckoutsUsingGiftCard;
- (BUYRoute *)routeForCheckoutsUsingGiftCardWithToken:(NSString *)token;
- (BUYRoute *)routeForCheckoutsUsingGiftCard:(NSNumber *)giftCardID token:(NSString *)token;
- (BUYRoute *)routeForCustomers;
- (BUYRoute *)routeForCustomersOrders;
- (BUYRoute *)routeForCustomersWithID:(NSString *)identifier;
- (BUYRoute *)routeForCustomersActivationWithID:(NSString *)identifier;
- (BUYRoute *)routeForCustomersToken;
- (BUYRoute *)routeForCustomersTokenRenewalWithID:(NSString *)customerID;
- (BUYRoute *)routeForCustomersPasswordRecovery;
- (BUYRoute *)routeForCustomersPasswordResetWithID:(NSString *)identifier;
@end
//
// BUYRouter.m
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "BUYRouter.h"
#pragma mark - Route -
@interface BUYRoute ()
@property (strong, nonatomic) NSURLComponents *components;
@end
@implementation BUYRoute
+ (instancetype)routeWithFormat:(NSString *)format, ...
{
va_list list;
va_start(list, format);
NSString *URLString = [[NSString alloc] initWithFormat:format arguments:list];
va_end(list);
return [[[self class] alloc] initWithURLString:URLString];
}
- (instancetype)initWithURLString:(NSString *)URLString
{
self = [super init];
if (self) {
NSURL *url = [NSURL URLWithString:URLString];
_components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
}
return self;
}
- (void)setQueryItems:(NSDictionary *)queryItems
{
_queryItems = queryItems;
NSMutableArray *items = [NSMutableArray new];
[queryItems enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
[items addObject:[NSURLQueryItem queryItemWithName:key value:[NSString stringWithFormat:@"%@", value]]];
}];
[_components setQueryItems:items];
}
#pragma mark - Accessors -
- (NSURL *)URL
{
/* ---------------------------------
* All API requests should end with
* a .json suffix.
*/
return [_components.URL URLByAppendingPathExtension:@"json"];
}
#pragma mark - Mutation -
- (BUYRoute *)appendFormat:(NSString *)format, ...
{
va_list list;
va_start(list, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:list];
va_end(list);
if (formattedString.length > 0) {
_components.path = [_components.path stringByAppendingPathComponent:formattedString];
}
return self;
}
- (BUYRoute *)appendPath:(NSString *)path
{
return [self appendFormat:path];
}
- (BUYRoute *)appendIdentifier:(NSNumber *)identifier
{
return [self appendFormat:@"%@", identifier];
}
@end
#pragma mark - Router -
@interface BUYRouter ()
@property (strong, nonatomic) NSString *shopDomain;
@property (strong, nonatomic) NSString *appID;
@end
@implementation BUYRouter
#pragma mark - Init -
- (instancetype)initWithShopDomain:(NSString *)shopDomain appID:(NSString *)appID
{
self = [super init];
if (self) {
_shopDomain = shopDomain;
_appID = appID;
}
return self;
}
#pragma mark - API -
- (BUYRoute *)routeForAPI
{
return [BUYRoute routeWithFormat:@"https://%@/api/", self.shopDomain];
}
- (BUYRoute *)routeForApps
{
return [[self routeForAPI] appendFormat:@"/apps/%@", self.appID];
}
#pragma mark - Storefront -
- (BUYRoute *)routeForShop
{
return [[self routeForAPI] appendPath:@"/meta"];
}
- (BUYRoute *)routeForProductListings
{
return [[self routeForApps] appendPath:@"/product_listings"];
}
- (BUYRoute *)routeForCollectionListings
{
return [[self routeForApps] appendPath:@"/collection_listings"];
}
#pragma mark - Checkout -
- (BUYRoute *)routeForCheckouts
{
return [[self routeForAPI] appendPath:@"/checkouts"];
}
- (BUYRoute *)routeForCheckoutsWithToken:(NSString *)token
{
return [self routeForCheckoutsAction:@"" withToken:token];
}
- (BUYRoute *)routeForCheckoutsProcessingWithToken:(NSString *)token
{
return [self routeForCheckoutsAction:@"/processing" withToken:token];
}
- (BUYRoute *)routeForCheckoutsCompletionWithToken:(NSString *)token
{
return [self routeForCheckoutsAction:@"/complete" withToken:token];
}
- (BUYRoute *)routeForCheckoutsShippingRatesWithToken:(NSString *)token
{
return [self routeForCheckoutsAction:@"/shipping_rates" withToken:token];
}
- (BUYRoute *)routeForCheckoutsUsingGiftCard
{
return [[self routeForCheckouts] appendPath:@"/gift_cards"];
}
- (BUYRoute *)routeForCheckoutsUsingGiftCardWithToken:(NSString *)token
{
return [[[self routeForCheckouts] appendPath:@"/gift_cards"] appendPath:token];
}
- (BUYRoute *)routeForCheckoutsUsingGiftCard:(NSNumber *)giftCardID token:(NSString *)token
{
return [[[self routeForCheckoutsUsingGiftCard] appendIdentifier:giftCardID] appendPath:token];
}
#pragma mark - Customers -
- (BUYRoute *)routeForCustomers
{
return [[self routeForAPI] appendPath:@"/customers"];
}
- (BUYRoute *)routeForCustomersOrders
{
return [[self routeForCustomers] appendPath:@"/orders"];
}
- (BUYRoute *)routeForCustomersWithID:(NSString *)identifier
{
return [[self routeForCustomers] appendPath:identifier];
}
- (BUYRoute *)routeForCustomersActivationWithID:(NSString *)identifier
{
return [[self routeForCustomersWithID:identifier] appendPath:@"/activate"];
}
- (BUYRoute *)routeForCustomersToken
{
return [[self routeForCustomers] appendPath:@"/customer_token"];
}
- (BUYRoute *)routeForCustomersTokenRenewalWithID:(NSString *)customerID
{
return [[self routeForCustomersWithID:customerID] appendPath:@"/customer_token/renew"];
}
- (BUYRoute *)routeForCustomersPasswordRecovery
{
return [[self routeForCustomers] appendPath:@"/recover"];
}
- (BUYRoute *)routeForCustomersPasswordResetWithID:(NSString *)identifier
{
return [[self routeForCustomersWithID:identifier] appendPath:@"/reset"];
}
#pragma mark - Utilities -
- (BUYRoute *)routeForCheckoutsAction:(NSString *)action withToken:(NSString *)token
{
return [[[self routeForCheckouts] appendPath:action] appendPath:token];
}
@end
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