Commit 326451f3 by Rune Madsen

Use pathWithComponents: and stringByAppendingPathExtension: for better…

Use pathWithComponents: and stringByAppendingPathExtension: for better consistency with URL creation (+4 squashed commits)
Squashed commits:
[90e8970] Also use pathWithComponents: for "checkouts" with token and fixing tests by appending a slash to "api" and "meta"
[4eb36a4] No longer appending = for purposely blank values
[4c0f4e2] Improve method names, use of constants and appending .json when assembling the NSURLCompenents path at the end
[935cbb2] forgot to stage /meta

Improve method names, use of constants and appending .json when assembling the NSURLCompenents path at the end

No longer appending = for purposely blank values

Also use pathWithComponents: for "checkouts" with token and fixing tests by appending a slash to "api" and "meta"

Use pathWithComponents: and stringByAppendingPathExtension: for better consistency with URL creation
parent a9b60cbc
...@@ -56,6 +56,9 @@ ...@@ -56,6 +56,9 @@
NSString * const BUYVersionString = @"1.2.3"; NSString * const BUYVersionString = @"1.2.3";
static NSString *const kBUYClientPathProductPublications = @"product_publications";
static NSString *const kBUYClientPathCollectionPublications = @"collection_publications";
@interface BUYClient () <NSURLSessionDelegate> @interface BUYClient () <NSURLSessionDelegate>
@property (nonatomic, strong) NSString *shopDomain; @property (nonatomic, strong) NSString *shopDomain;
...@@ -124,15 +127,15 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -124,15 +127,15 @@ NSString * const BUYVersionString = @"1.2.3";
- (NSURLSessionDataTask *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block - (NSURLSessionDataTask *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block
{ {
NSURLComponents *components = [self URLComponentsForChannelsWithEndPoint:@"product_publications.json" NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathProductPublications
queryItems:@{@"limit" : [NSString stringWithFormat:@"%lu", (unsigned long)self.pageSize], queryItems:@{@"limit" : [NSString stringWithFormat:@"%lu", (unsigned long)self.pageSize],
@"page" : [NSString stringWithFormat:@"%lu", (unsigned long)page]}]; @"page" : [NSString stringWithFormat:@"%lu", (unsigned long)page]}];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *products = nil; NSArray *products = nil;
if (json && error == nil) { if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[@"product_publications"]]; products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]];
} }
block(products, page, [self hasReachedEndOfPage:products] || error, error); block(products, page, [self hasReachedEndOfPage:products] || error, error);
}]; }];
...@@ -154,14 +157,14 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -154,14 +157,14 @@ NSString * const BUYVersionString = @"1.2.3";
- (NSURLSessionDataTask *)getProductsByIds:(NSArray *)productIds completion:(BUYDataProductsBlock)block - (NSURLSessionDataTask *)getProductsByIds:(NSArray *)productIds completion:(BUYDataProductsBlock)block
{ {
NSURLComponents *components = [self URLComponentsForChannelsWithEndPoint:@"product_publications.json" NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathProductPublications
queryItems:@{@"product_ids" : [productIds componentsJoinedByString:@","]}]; queryItems:@{@"product_ids" : [productIds componentsJoinedByString:@","]}];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *products = nil; NSArray *products = nil;
if (json && error == nil) { if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[@"product_publications"]]; products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]];
} }
if (error == nil && [products count] == 0) { if (error == nil && [products count] == 0) {
error = [NSError errorWithDomain:kShopifyError code:BUYShopifyError_InvalidProductID userInfo:@{ NSLocalizedDescriptionKey : @"Product IDs are not valid. Confirm the product IDs on your shop's admin and also ensure that the visibility is on for the Mobile App channel." }]; error = [NSError errorWithDomain:kShopifyError code:BUYShopifyError_InvalidProductID userInfo:@{ NSLocalizedDescriptionKey : @"Product IDs are not valid. Confirm the product IDs on your shop's admin and also ensure that the visibility is on for the Mobile App channel." }];
...@@ -172,13 +175,13 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -172,13 +175,13 @@ NSString * const BUYVersionString = @"1.2.3";
- (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block - (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block
{ {
NSURLComponents *components = [self URLComponentsForChannelsWithEndPoint:@"collection_publications.json" queryItems:nil]; NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathCollectionPublications queryItems:nil];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *collections = nil; NSArray *collections = nil;
if (json && error == nil) { if (json && error == nil) {
collections = [BUYCollection convertJSONArray:json[@"collection_publications"]]; collections = [BUYCollection convertJSONArray:json[kBUYClientPathCollectionPublications]];
} }
block(collections, error); block(collections, error);
}]; }];
...@@ -193,17 +196,17 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -193,17 +196,17 @@ NSString * const BUYVersionString = @"1.2.3";
{ {
NSURLSessionDataTask *task = nil; NSURLSessionDataTask *task = nil;
if (collectionId) { if (collectionId) {
NSURLComponents *components = [self URLComponentsForChannelsWithEndPoint:@"product_publications.json" NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathProductPublications
queryItems:@{@"collection_id" : [NSString stringWithFormat:@"%lu", collectionId.longValue], queryItems:@{@"collection_id" : [NSString stringWithFormat:@"%lu", collectionId.longValue],
@"limit" : [NSString stringWithFormat:@"%lu", (unsigned long)self.pageSize], @"limit" : [NSString stringWithFormat:@"%lu", (unsigned long)self.pageSize],
@"page" : [NSString stringWithFormat:@"%lu", (unsigned long)page], @"page" : [NSString stringWithFormat:@"%lu", (unsigned long)page],
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]}]; @"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]}];
task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *products = nil; NSArray *products = nil;
if (json && error == nil) { if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[@"product_publications"]]; products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]];
} }
block(products, page, [self hasReachedEndOfPage:products] || error, error); block(products, page, [self hasReachedEndOfPage:products] || error, error);
}]; }];
...@@ -229,36 +232,34 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -229,36 +232,34 @@ NSString * const BUYVersionString = @"1.2.3";
#pragma mark - URL Components #pragma mark - URL Components
- (NSURLComponents *)URLComponentsForChannelsWithEndPoint:(NSString *)endPoint queryItems:(NSDictionary*)queryItems - (NSURLComponents *)URLComponentsForChannelsAppendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems
{ {
return [self URLComponentsForAPIPath:[NSString stringWithFormat:@"channels/%@", self.channelId] endPoint:endPoint queryItems:queryItems]; return [self URLComponentsForAPIPath:[NSString stringWithFormat:@"channels/%@", self.channelId] appendingPath:appendingPath queryItems:queryItems];
} }
- (NSURLComponents *)URLComponentsForCheckoutsWithToken:(NSString *)checkoutToken endPoint:(NSString *)endPoint queryItems:(NSDictionary*)queryItems - (NSURLComponents *)URLComponentsForCheckoutsAppendingPath:(NSString *)appendingPath checkoutToken:(NSString *)checkoutToken queryItems:(NSDictionary*)queryItems
{ {
NSMutableString *apiPath = [NSMutableString stringWithString:@"checkouts"]; NSString *apiPath = @"checkouts";
if (checkoutToken) { if (checkoutToken) {
[apiPath appendFormat:@"/%@", checkoutToken]; apiPath = [NSString pathWithComponents:@[apiPath, checkoutToken]];
} else {
[apiPath appendString:@".json"];
} }
return [self URLComponentsForAPIPath:[apiPath copy] endPoint:endPoint queryItems:queryItems]; return [self URLComponentsForAPIPath:[apiPath copy] appendingPath:appendingPath queryItems:queryItems];
} }
- (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath endPoint:(NSString *)endPoint queryItems:(NSDictionary*)queryItems - (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath appendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems
{ {
NSMutableArray *pathComponents = [NSMutableArray array]; NSMutableArray *pathComponents = [NSMutableArray array];
[pathComponents addObject:@"api"]; [pathComponents addObject:@"/api"];
[pathComponents addObject:apiPath]; [pathComponents addObject:apiPath];
if (endPoint) { if (appendingPath) {
[pathComponents addObject:endPoint]; [pathComponents addObject:appendingPath];
} }
return [self URLComponentsForPathComponents:pathComponents queryItems:queryItems]; return [self URLComponentsForPathComponents:pathComponents queryItems:queryItems];
} }
- (NSURLComponents *)URLComponentsForShop - (NSURLComponents *)URLComponentsForShop
{ {
return [self URLComponentsForPathComponents:@[@"meta.json"] queryItems:nil]; return [self URLComponentsForPathComponents:@[@"/meta"] queryItems:nil];
} }
- (NSURLComponents *)URLComponentsForPathComponents:(NSArray*)pathComponents queryItems:(NSDictionary*)queryItems - (NSURLComponents *)URLComponentsForPathComponents:(NSArray*)pathComponents queryItems:(NSDictionary*)queryItems
...@@ -266,7 +267,7 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -266,7 +267,7 @@ NSString * const BUYVersionString = @"1.2.3";
NSURLComponents *components = [[NSURLComponents alloc] init]; NSURLComponents *components = [[NSURLComponents alloc] init];
components.scheme = @"https"; components.scheme = @"https";
components.host = self.shopDomain; components.host = self.shopDomain;
components.path = [NSString stringWithFormat:@"/%@", [pathComponents componentsJoinedByString:@"/"]]; components.path = [[NSString pathWithComponents:pathComponents] stringByAppendingPathExtension:@"json"];
[components setQueryItemsWithDictionary:queryItems]; [components setQueryItemsWithDictionary:queryItems];
return components; return components;
} }
...@@ -319,7 +320,7 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -319,7 +320,7 @@ NSString * const BUYVersionString = @"1.2.3";
NSData *data = [NSJSONSerialization dataWithJSONObject:checkoutJSON options:0 error:&error]; NSData *data = [NSJSONSerialization dataWithJSONObject:checkoutJSON options:0 error:&error];
if (data && error == nil) { if (data && error == nil) {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:nil endPoint:nil queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:nil checkoutToken:nil queryItems:nil];
task = [self postRequestForURL:components.URL body:data completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { task = [self postRequestForURL:components.URL body:data completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
...@@ -337,9 +338,9 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -337,9 +338,9 @@ NSString * const BUYVersionString = @"1.2.3";
} }
else { else {
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"code" : giftCardCode }]; BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"code" : giftCardCode }];
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:checkout.token NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"gift_cards"
endPoint:@"gift_cards.json" checkoutToken:checkout.token
queryItems:nil]; queryItems:nil];
task = [self postRequestForURL:components.URL task = [self postRequestForURL:components.URL
object:giftCard object:giftCard
...@@ -358,7 +359,9 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -358,7 +359,9 @@ NSString * const BUYVersionString = @"1.2.3";
{ {
NSURLSessionDataTask *task = nil; NSURLSessionDataTask *task = nil;
if (giftCard.identifier) { if (giftCard.identifier) {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:checkout.token endPoint:[NSString stringWithFormat:@"gift_cards/%@.json", giftCard.identifier] queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:[NSString stringWithFormat:@"gift_cards/%@", giftCard.identifier]
checkoutToken:checkout.token
queryItems:nil];
task = [self deleteRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { task = [self deleteRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (error == nil) { if (error == nil) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO]; [self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO];
...@@ -393,7 +396,9 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -393,7 +396,9 @@ NSString * const BUYVersionString = @"1.2.3";
- (NSURLSessionDataTask *)getCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block - (NSURLSessionDataTask *)getCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{ {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:checkout.token endPoint:nil queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:nil
checkoutToken:checkout.token
queryItems:nil];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
...@@ -406,7 +411,9 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -406,7 +411,9 @@ NSString * const BUYVersionString = @"1.2.3";
NSURLSessionDataTask *task = nil; NSURLSessionDataTask *task = nil;
if ([checkout hasToken]) { if ([checkout hasToken]) {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:checkout.token endPoint:nil queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:nil
checkoutToken:checkout.token
queryItems:nil];
task = [self patchRequestForURL:components.URL body:data completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { task = [self patchRequestForURL:components.URL body:data completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
...@@ -472,7 +479,9 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -472,7 +479,9 @@ NSString * const BUYVersionString = @"1.2.3";
- (NSURLSessionDataTask *)checkoutCompletionRequestWithCheckout:(BUYCheckout *)checkout body:(NSData *)body completion:(BUYDataCheckoutBlock)block - (NSURLSessionDataTask *)checkoutCompletionRequestWithCheckout:(BUYCheckout *)checkout body:(NSData *)body completion:(BUYDataCheckoutBlock)block
{ {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:checkout.token endPoint:@"complete.json" queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"complete"
checkoutToken:checkout.token
queryItems:nil];
return [self postRequestForURL:components.URL body:body completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self postRequestForURL:components.URL body:body completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
...@@ -513,7 +522,9 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -513,7 +522,9 @@ NSString * const BUYVersionString = @"1.2.3";
- (NSURLSessionDataTask *)getCompletionStatusOfCheckoutToken:(NSString *)token completion:(BUYDataCheckoutStatusBlock)block - (NSURLSessionDataTask *)getCompletionStatusOfCheckoutToken:(NSString *)token completion:(BUYDataCheckoutStatusBlock)block
{ {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:token endPoint:@"processing.json" queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"processing"
checkoutToken:token
queryItems:nil];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
block([BUYClient statusForStatusCode:statusCode error:error], error); block([BUYClient statusForStatusCode:statusCode error:error], error);
...@@ -526,7 +537,7 @@ NSString * const BUYVersionString = @"1.2.3"; ...@@ -526,7 +537,7 @@ NSString * const BUYVersionString = @"1.2.3";
{ {
NSURLSessionDataTask *task = nil; NSURLSessionDataTask *task = nil;
if ([checkout hasToken]) { if ([checkout hasToken]) {
NSURLComponents *components = [self URLComponentsForCheckoutsWithToken:checkout.token endPoint:@"shipping_rates.json?checkout" queryItems:nil]; NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"shipping_rates" checkoutToken:checkout.token queryItems:@{ @"checkout" : @"" }];
task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *shippingRates = nil; NSArray *shippingRates = nil;
if (error == nil && json) { if (error == nil && json) {
......
...@@ -33,7 +33,8 @@ ...@@ -33,7 +33,8 @@
if (dictionary) { if (dictionary) {
NSMutableArray *queryItems = [NSMutableArray array]; NSMutableArray *queryItems = [NSMutableArray array];
for (NSString *key in [dictionary allKeys]) { for (NSString *key in [dictionary allKeys]) {
[queryItems addObject:[[NSURLQueryItem alloc] initWithName:key value:dictionary[key]]]; NSString *value = [dictionary[key] length] ? dictionary[key] : nil;
[queryItems addObject:[[NSURLQueryItem alloc] initWithName:key value:value]];
} }
self.queryItems = [queryItems copy]; self.queryItems = [queryItems copy];
} }
......
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