Commit 751554a5 by Dima Bart

Add missing getProductByHandle API. Minor cleanup and refactoring.

parent 5a94ac08
......@@ -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
{
[OHHTTPStubs stubUsingResponseWithKey:@"testGetProduct_0" useMocks:[self shouldUseMocks]];
......
......@@ -141,6 +141,16 @@ typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable produc
- (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.
*
* @param productId Product ID
......
......@@ -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(productId, @"Failed to get product by ID. Product ID must not be nil.");
BUYAssert(handle, @"Failed to get product by handle. Product handle must not be nil.");
return [self getProductsByIds:@[productId] completion:^(NSArray *products, NSError *error) {
if (products.count > 0) {
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." }];
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;
}
block(nil, error);
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.");
return [self getProductsByIds:@[productId] completion:^(NSArray *products, NSError *error) {
block(products.firstObject, error);
}];
}
......@@ -106,7 +118,7 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
products = [self.modelManager insertProductsWithJSONArray:json[BUYProductsKey]];
}
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);
}];
......@@ -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
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