getWishlist method

void getWishlist(
  1. int page,
  2. GetWishlist getWishlist
)

Implementation

void getWishlist(int page, GetWishlist getWishlist) async {
  getWishlist.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'wishlist',
    "appKey": appKey,
    "action": "getWishlist",
    "page": page.toString(),
  };

  try {
    final response = await http.post(
      Uri.parse(url),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer $token'
      },
      body: body,
    );

    final data = json.decode(response.body);
    int code = data['response']['code'];
    String status = data['response']['status'];

    if (code == 400) {
      if (status == "session-expired" || status == "please-login") {
        getWishlist.onNotLoggedIn();
        getWishlist.onLoadfinished();
        return;
      }

      if (status == "app-expired") {
        getWishlist.onAppNotActive(data['info']['appName']);
        getWishlist.onLoadfinished();
        return;
      }

      getWishlist.onError(status);
      getWishlist.onLoadfinished();
      return;
    }

    final info = data['info'];

    if (info['nextPage']['status']) {
      getWishlist.onNextPage(info['nextPage']['page']);
    } else {
      getWishlist.onNoNextPage();
    }

    if (info['itemsInThisPage'] == 0) {
      getWishlist.onEmpty();
      getWishlist.onLoadfinished();
      return;
    }

    getWishlist.onResult(
      info['appName'],
      info['totalItemCount'],
      info['itemsInThisPage'],
      info['itemsPerPage'],
      data['items'],
    );
    getWishlist.onLoadfinished();
  } catch (e) {
    getWishlist.onError(e.toString());
    getWishlist.onLoadfinished();
  }
}