Commit 751554a5 by Dima Bart

Add missing getProductByHandle API. Minor cleanup and refactoring.

parent 5a94ac08
...@@ -97,6 +97,25 @@ ...@@ -97,6 +97,25 @@
}]; }];
} }
- (void)testGetProductByHandle
{
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProduct_0" useMocks:[self shouldUseMocks]];
NSString *handle = @"actinian-fur-hat";
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductByHandle:handle completion:^(BUYProduct *product, NSError *error) {
XCTAssertNil(error);
XCTAssertNotNil(product);
XCTAssertEqualObjects(product.handle, handle);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)testGetProductById - (void)testGetProductById
{ {
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProduct_0" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testGetProduct_0" useMocks:[self shouldUseMocks]];
......
...@@ -141,6 +141,16 @@ typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable produc ...@@ -141,6 +141,16 @@ typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable produc
- (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block; - (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block;
/** /**
* Fetches a single product by the handle of the product.
*
* @param handle Product handle
* @param block (^BUYDataProductBlock)(BUYProduct *product, NSError *error);
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)getProductByHandle:(NSString *)handle completion:(BUYDataProductBlock)block;
/**
* Fetches a single product by the ID of the product. * Fetches a single product by the ID of the product.
* *
* @param productId Product ID * @param productId Product ID
......
...@@ -61,9 +61,9 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -61,9 +61,9 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block - (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block
{ {
NSURL *url = [self urlForProductListingsWithParameters:@{ NSURL *url = [self urlForProductListingsWithParameters:@{
@"limit" : @(self.pageSize), @"limit" : @(self.pageSize),
@"page" : @(page), @"page" : @(page),
}]; }];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -75,19 +75,31 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -75,19 +75,31 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
}]; }];
} }
- (BUYRequestOperation *)getProductById:(NSString *)productId completion:(BUYDataProductBlock)block; - (BUYRequestOperation *)getProductByHandle:(NSString *)handle completion:(BUYDataProductBlock)block
{
BUYAssert(handle, @"Failed to get product by handle. Product handle must not be nil.");
NSURL *url = [self urlForProductListingsWithParameters:@{ @"handle" : handle }];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYProduct *product = nil;
if (json && !error) {
product = [self.modelManager insertProductsWithJSONArray:json[BUYProductsKey]].firstObject;
}
if (!product && !error) {
error = [self productsError];
}
block(product, error);
}];
}
- (BUYRequestOperation *)getProductById:(NSString *)productId completion:(BUYDataProductBlock)block
{ {
BUYAssert(productId, @"Failed to get product by ID. Product ID must not be nil."); BUYAssert(productId, @"Failed to get product by ID. Product ID must not be nil.");
return [self getProductsByIds:@[productId] completion:^(NSArray *products, NSError *error) { return [self getProductsByIds:@[productId] completion:^(NSArray *products, NSError *error) {
if (products.count > 0) { block(products.firstObject, error);
block(products[0], error);
} else {
if (!error) {
error = [NSError errorWithDomain:BUYShopifyErrorDomain code:BUYShopifyError_InvalidProductID userInfo:@{ NSLocalizedDescriptionKey : @"Product ID is not valid. Confirm the product ID on your shop's admin and also ensure that the visibility is on for the Mobile App channel." }];
}
block(nil, error);
}
}]; }];
} }
...@@ -96,8 +108,8 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -96,8 +108,8 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
BUYAssert(productIds, @"Failed to get product by IDs. Product IDs array must not be nil."); BUYAssert(productIds, @"Failed to get product by IDs. Product IDs array must not be nil.");
NSURL *url = [self urlForProductListingsWithParameters:@{ NSURL *url = [self urlForProductListingsWithParameters:@{
@"product_ids" : [productIds componentsJoinedByString:@","], @"product_ids" : [productIds componentsJoinedByString:@","],
}]; }];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -106,7 +118,7 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -106,7 +118,7 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
products = [self.modelManager insertProductsWithJSONArray:json[BUYProductsKey]]; products = [self.modelManager insertProductsWithJSONArray:json[BUYProductsKey]];
} }
if (!error && products.count == 0) { if (!error && products.count == 0) {
error = [NSError errorWithDomain:BUYShopifyErrorDomain 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 = [self productsError];
} }
block(products, error); block(products, error);
}]; }];
...@@ -115,9 +127,9 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -115,9 +127,9 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block - (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{ {
NSURL *url = [self urlForCollectionListingsWithParameters:@{ NSURL *url = [self urlForCollectionListingsWithParameters:@{
@"limit" : @(self.pageSize), @"limit" : @(self.pageSize),
@"page" : @(page), @"page" : @(page),
}]; }];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -139,11 +151,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -139,11 +151,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID."); BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID.");
NSURL *url = [self urlForProductListingsWithParameters:@{ NSURL *url = [self urlForProductListingsWithParameters:@{
@"collection_id" : collectionId, @"collection_id" : collectionId,
@"limit" : @(self.pageSize), @"limit" : @(self.pageSize),
@"page" : @(page), @"page" : @(page),
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder] @"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
}]; }];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -155,4 +167,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -155,4 +167,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
}]; }];
} }
#pragma mark - Helpers -
- (NSError *)productsError
{
return [NSError errorWithDomain:BUYShopifyErrorDomain code:BUYShopifyError_InvalidProductID userInfo:@{ NSLocalizedDescriptionKey : @"Product identifier(s) / handle is not valid. Confirm the product identifier(s) / handle in your shop's admin and ensure that the visibility is ON for the Mobile App channel." }];
}
@end @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