Commit 4f9f8577 by David Muzi

product tags API implementation

parent 1b5065bd
......@@ -184,7 +184,7 @@ NSString * const BUYFakeCustomerToken = @"dsfasdgafdg";
- (void)testProductsInCollection
{
BUYRequestOperation *task = (BUYRequestOperation *)[self.client getProductsPage:1 inCollection:@1 sortOrder:BUYCollectionSortCollectionDefault completion:^(NSArray<BUYProduct *> *products, NSUInteger page, BOOL reachedEnd, NSError *error) {}];
BUYRequestOperation *task = (BUYRequestOperation *)[self.client getProductsPage:1 withTags:nil inCollection:@1 sortOrder:BUYCollectionSortCollectionDefault completion:^(NSArray<BUYProduct *> *products, NSUInteger page, BOOL reachedEnd, NSError *error) {}];
XCTAssertEqualObjects(task.originalRequest.HTTPMethod, @"GET");
XCTAssertEqualObjects(task.originalRequest.URL.scheme, @"https");
XCTAssertEqualObjects(task.originalRequest.URL.host, @"test_shop");
......
......@@ -34,6 +34,7 @@
- (NSURL *)urlForShop;
- (NSURL *)urlForProductListingsWithParameters:(NSDictionary *)parameters;
- (NSURL *)urlForCollectionListingsWithParameters:(NSDictionary *)parameters;
- (NSURL *)urlForProductTagsWithParameters:(NSDictionary *)parameters;
- (NSURL *)urlForCheckouts;
- (NSURL *)urlForCheckoutsWithToken:(NSString *)token;
......
......@@ -124,6 +124,11 @@
return [[[[self urlForApps] appendPath:@"/collection_listings"] appendExtension] appendParameters:parameters];
}
- (NSURL *)urlForProductTagsWithParameters:(NSDictionary *)parameters
{
return [[[[self urlForApps] appendPath:@"/product_listings/tags"] appendExtension] appendParameters:parameters];;
}
#pragma mark - Checkout -
- (NSURL *)urlForCheckouts
......
......@@ -191,29 +191,6 @@ typedef void (^BUYDataTagsListBlock)(NSArray <NSString *> * _Nullable tags, NSUI
- (NSOperation *)getProductTagsPage:(NSUInteger)page completion:(BUYDataTagsListBlock)block;
/**
* Get the set of products that contain the tags provided
*
* @param page Page to request. Pages start at 1.
* @param tags an array of tags which each product must contain
* @param block (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable products, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error)
*
* @return The associated NSOperation
*/
- (NSOperation *)getProductsPage:(NSUInteger)page withTags:(NSArray <NSString *> *)tags completion:(BUYDataProductListBlock)block;
/**
* Get the set of products that contain the tags provided within the given collection
*
* @param page Page to request. Pages start at 1.
* @param tags an array of tags which each product must contain
* @param collectionId The `collectionId` found in the BUYCollection object to fetch the products from
* @param block (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable products, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error)
*
* @return The associated NSOperation
*/
- (NSOperation *)getProductsPage:(NSUInteger)page withTags:(NSArray <NSString *> *)tags inCollection:(nullable NSNumber *)collectionId completion:(BUYDataProductListBlock)block;
/**
* Fetches collections based off page
*
* @param page Index of the page requested
......@@ -239,13 +216,14 @@ typedef void (^BUYDataTagsListBlock)(NSArray <NSString *> * _Nullable tags, NSUI
* Fetches the products in the given collection with a given sort order
*
* @param page Index of the page requested
* @param tags an array of tags which each product must contain
* @param collectionId The `collectionId` found in the BUYCollection object to fetch the products from
* @param sortOrder The sort order that overrides the default collection sort order
* @param block (NSArray *products, NSUInteger page, BOOL reachedEnd, NSError *error)
*
* @return the associated BUYRequestOperation
*/
- (NSOperation *)getProductsPage:(NSUInteger)page inCollection:(NSNumber *)collectionId sortOrder:(BUYCollectionSort)sortOrder completion:(BUYDataProductListBlock)block;
- (NSOperation *)getProductsPage:(NSUInteger)page withTags:(nullable NSArray <NSString *> *)tags inCollection:(nullable NSNumber *)collectionId sortOrder:(BUYCollectionSort)sortOrder completion:(BUYDataProductListBlock)block;
@end
......
......@@ -124,6 +124,23 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
}];
}
- (NSOperation *)getProductTagsPage:(NSUInteger)page completion:(BUYDataTagsListBlock)block
{
NSURL *url = [self urlForProductTagsWithParameters:@{
@"limit" : @(self.pageSize),
@"page" : @(page),
}];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *tags = nil;
if (json && !error) {
tags = [json[@"tags"] valueForKey:@"title"];
}
block(tags, [self hasReachedEndOfPage:tags], page, error);
}];
}
- (NSOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{
NSURL *url = [self urlForCollectionListingsWithParameters:@{
......@@ -143,19 +160,26 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (NSOperation *)getProductsPage:(NSUInteger)page inCollection:(NSNumber *)collectionId completion:(BUYDataProductListBlock)block
{
return [self getProductsPage:page inCollection:collectionId sortOrder:BUYCollectionSortCollectionDefault completion:block];
return [self getProductsPage:page withTags:nil inCollection:collectionId sortOrder:BUYCollectionSortCollectionDefault completion:block];
}
- (NSOperation *)getProductsPage:(NSUInteger)page inCollection:(NSNumber *)collectionId sortOrder:(BUYCollectionSort)sortOrder completion:(BUYDataProductListBlock)block
- (NSOperation *)getProductsPage:(NSUInteger)page withTags:(NSArray <NSString *> *)tags inCollection:(NSNumber *)collectionId sortOrder:(BUYCollectionSort)sortOrder completion:(BUYDataProductListBlock)block
{
BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID.");
NSMutableDictionary *params = @{
@"limit" : @(self.pageSize),
@"page" : @(page),
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
}.mutableCopy;
NSURL *url = [self urlForProductListingsWithParameters:@{
@"collection_id" : collectionId,
@"limit" : @(self.pageSize),
@"page" : @(page),
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
}];
if (tags) {
params[@"tags"] = [tags componentsJoinedByString:@","];
}
if (collectionId) {
params[@"collection_id"] = collectionId;
}
NSURL *url = [self urlForProductListingsWithParameters:params];
return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
......
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