Commit 3f817423 by Dima Bart

Factor route creation / management out of BUYClient.

parent d390ebc6
......@@ -373,6 +373,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 */; };
9ABBCCA11CF5C9D10075B0C5 /* BUYFakeSafariController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */; };
B2653EC31CEF55CC0012D57D /* BUYModelManager+ApplePay.h in Headers */ = {isa = PBXBuildFile; fileRef = B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */; };
......@@ -655,6 +659,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>"; };
9ABBCC9F1CF5C9D10075B0C5 /* BUYFakeSafariController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYFakeSafariController.h; sourceTree = "<group>"; };
9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYFakeSafariController.m; sourceTree = "<group>"; };
......@@ -1141,6 +1147,9 @@
isa = PBXGroup;
children = (
8498DCB01CDD1B4A00BD12A8 /* BUYClient+Internal.h */,
8498DCB01CDD1B4A00BD12A8 /* BUYClient_Internal.h */,
9A53317B1CE5F9260025964B /* BUYRouter.h */,
9A53317C1CE5F9260025964B /* BUYRouter.m */,
F7FDA17019C93F6F00AF4E93 /* BUYClient.h */,
F7FDA17119C93F6F00AF4E93 /* BUYClient.m */,
9A0B0C641CEA703E0037D68F /* BUYClient+Routing.h */,
......@@ -1242,6 +1251,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 */,
9A0B0C791CEB5BBD0037D68F /* BUYAuthenticatedResponse.h in Headers */,
849810971CB7E07900CFAB58 /* BUYFlatCollectionTransformer.h in Headers */,
......@@ -1341,6 +1353,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 */,
9A0B0C781CEB5BBD0037D68F /* BUYAuthenticatedResponse.h in Headers */,
849810961CB7E07900CFAB58 /* BUYFlatCollectionTransformer.h in Headers */,
......@@ -1572,6 +1587,7 @@
9A0B0C7B1CEB5BBD0037D68F /* BUYAuthenticatedResponse.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 */,
......@@ -1709,6 +1725,7 @@
9A0B0C7A1CEB5BBD0037D68F /* BUYAuthenticatedResponse.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 */,
......
......@@ -33,6 +33,7 @@
#import "BUYAuthenticatedResponse.h"
#import "BUYOrder.h"
#import "BUYShopifyErrorCodes.h"
#import "BUYRouter.h"
@implementation BUYClient (Customers)
......@@ -96,7 +97,6 @@
{
NSURL *route = [self urlForCustomersPasswordRecovery];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (!error) {
error = [self errorFromJSON:json response:response];
......@@ -112,7 +112,6 @@
NSURL *route = [self urlForCustomersTokenRenewalWithID:customerID];
return [self putRequestForURL:route object:nil completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSString *accessToken = nil;
if (json && !error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json];
......
......@@ -38,6 +38,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSString *merchantId;
@property (nonatomic, strong) BUYRouter *router;
@end
......@@ -58,6 +59,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
self = [super init];
if (self) {
self.router = [[BUYRouter alloc] initWithShopDomain:shopDomain appID:appId];
self.modelManager = [BUYModelManager modelManager];
self.shopDomain = shopDomain;
self.apiKey = apiKey;
......
//
// 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