getProductsByFilters method

Future<ApiCallback<ProductPaginationResult>> getProductsByFilters(
  1. {int page = 1,
  2. int perPage = 20,
  3. String continuationToken,
  4. ProductOrder sort,
  5. bool perSeller = false,
  6. Map<String, dynamic> filterParams}
)

Implementation

Future<ApiCallback<ProductPaginationResult>> getProductsByFilters({
  int page = 1,
  int perPage = 20,
  String continuationToken,
  ProductOrder sort,
  bool perSeller = false,
  Map<String, dynamic> filterParams,
}) async {
  Map<String, dynamic> queryParameters = {'page': page, 'perPage': perPage};

  queryParameters.addAll(_queryParametersForPagedProductRequest(continuationToken: continuationToken, sort: sort));
  queryParameters.addAll(filterParams);
  if (perSeller) {
    var cart = await CartDao().get();
    if (cart.seller != null) {
      queryParameters["sellerId"] = cart.seller.id;
    }
  }

  try {
    Stopwatch stopwatch = new Stopwatch()..start();
    var response = await _requester.get(await _catalogEndpoint(API_PRODUCTS_BY_FILTERS, includeSeller: false),
        queryParameters: queryParameters, options: Options(responseType: ResponseType.json));
    var paginationResult = ProductPaginationResult.fromJson(response.data);
    print("Page ${paginationResult.page}/${paginationResult.maxPage} loaded in ${stopwatch.elapsedMilliseconds}ms");
    print(response.request.uri.toString());
    return ApiCallback<ProductPaginationResult>(isSuccessful: true, resultData: paginationResult);
  } on DioError catch (e) {
    print(e.request.uri.toString());
    return ApiCallback<ProductPaginationResult>().defaultErrorCallback(e);
  }
}