getItems method
Implementation
Future<void> getItems(String itemCategory, GetItems getItems) async {
getItems.onLoading();
final url = Uri.parse(
'https://api.plentrasphere.com/v2/client/?class=default&action=getItems&appKey=$appKey&page=1&categoryId=$itemCategory');
try {
final response = await http.get(url);
if (response.statusCode == 200) {
final jsonData = json.decode(response.body);
final code = jsonData['response']['code'];
final status = jsonData['response']['status'];
if (code == 401) {
getItems.onError(status);
getItems.onLoadfinished();
return;
} else if (code == 404) {
getItems.onNotFound();
getItems.onLoadfinished();
return;
} else if (code == 400) {
if (status == "app-expired") {
getItems.onAppNotActive(jsonData['info']['appName']);
getItems.onLoadfinished();
return;
}
getItems.onError(status);
getItems.onLoadfinished();
return;
} else if (code == 406) {
getItems.onError(status);
return;
}
if (status == "under-construction") {
getItems.onUnderConstruction();
getItems.onLoadfinished();
return;
}
final info = jsonData['info'];
final itemsInThisPage = (info['itemsInThisPage']);
final totalItemCount = (info['totalItemCount']);
if (info['appLocation']['status']) {
final location = info['appLocation'];
getItems.onAppLocation(
location['location'],
(location['lat']),
(location['lon']),
);
}
if (jsonData['announcement']['status']) {
getItems.onAnnouncement(jsonData['announcement']['announcementBody']);
}
if (info['homeCover']['status']) {
getItems.onHomeCover(info['homeCover']['image']);
}
if (info['nextPage']['status']) {
getItems.onNextPage(info['nextPage']['page']);
} else {
getItems.onNoNextPage();
}
if (itemsInThisPage != 0) {
final List<dynamic> items = [];
final _items = jsonData['items'];
_items.forEach((element) {
items.add({
'itemName': element['itemName'],
'itemId': element['itemId'],
'extras': element['extras'],
'itemImage': element['itemImage'],
});
});
getItems.onResult(
itemCategory,
info['appName'],
info['appIcon'],
info['categoryName'],
info['categoryThumbnail'],
totalItemCount,
itemsInThisPage,
(info['itemsPerPage']),
items,
);
} else {
getItems.onEmpty(
info['appName'],
info['appIcon'],
info['categoryName'],
info['categoryThumbnail'],
);
}
getItems.onLoadfinished();
} else {
getItems.onError('HTTP ${response.statusCode}');
getItems.onLoadfinished();
}
} catch (e) {
getItems.onError(e.toString());
getItems.onLoadfinished();
}
}