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
......
...@@ -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(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) { NSURL *url = [self urlForProductListingsWithParameters:@{ @"handle" : handle }];
if (products.count > 0) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(products[0], error);
} else { BUYProduct *product = nil;
if (!error) { if (json && !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." }]; 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"; ...@@ -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);
}]; }];
...@@ -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