getItem method
Implementation
Future<void> getItem(String itemId, GetItem getItem) async {
getItem.onLoading();
final url = Uri.parse(
'https://api.plentrasphere.com/?action=getItem&appKey=${this.appKey}&itemId=$itemId');
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'];
final info = jsonData['info'];
if (code == 401) {
getItem.onError(status);
getItem.onLoadfinished();
return;
} else if (code == 404) {
getItem.onNotFound();
getItem.onLoadfinished();
return;
} else if (code == 400) {
if (status == "app-expired") {
getItem.onAppNotActive(info['appName']);
getItem.onLoadfinished();
return;
}
getItem.onError(status);
getItem.onLoadfinished();
return;
} else if (code == 406) {
getItem.onError(status);
getItem.onLoadfinished();
return;
}
if (status == "under-construction") {
getItem.onUnderConstruction();
getItem.onLoadfinished();
return;
}
if (jsonData['announcement']['status']) {
getItem.onAnnouncement(jsonData['announcement']['announcementBody']);
}
if (info['appLocation']['status']) {
final location = info['appLocation'];
getItem.onAppLocation(
location['location'],
(location['lat']),
(location['lon']),
);
}
if (info['homeCover']['status']) {
getItem.onHomeCover(info['homeCover']['image']);
}
if (jsonData['video']['status']) {
getItem.onVideo(jsonData['video']['video']);
}
getItem.onResult(
info['appName'],
info['appIcon'],
jsonData['itemName'],
jsonData['extras'],
utf8.decode(base64.decode(jsonData['body'])),
jsonData['itemCategoryName'],
jsonData['itemCategory'],
jsonData['itemImages'],
jsonData['itemImage'],
);
if (jsonData['variants']['status']) {
final firstVariant = jsonData['variants']['firstVariant'];
getItem.onFirstVariant(
firstVariant['variantTag'],
firstVariant['variantId'],
firstVariant['variantExtras'],
firstVariant['variantImage'],
firstVariant['variantImages'],
);
getItem.onVariants(jsonData['variants']['variants']);
}
getItem.onLoadfinished();
} else {
getItem.onError('HTTP ${response.statusCode}');
getItem.onLoadfinished();
}
} catch (e) {
getItem.onError(e.toString());
getItem.onLoadfinished();
}
}