getPage method

Future<void> getPage(
  1. String pageTitle,
  2. GetPage getPage
)

Implementation

Future<void> getPage(String pageTitle, GetPage getPage) async {
  getPage.onLoading();

  final url = Uri.parse(
      'https://api.plentrasphere.com/v2/client/?class=default&action=getPage&appKey=$appKey&pageTitle=$pageTitle');

  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) {
        getPage.onError(status);
        getPage.onLoadfinished();
        return;
      } else if (code == 404) {
        getPage.onNotFound();
        getPage.onLoadfinished();
        return;
      } else if (code == 400) {
        if (status == "app-expired") {
          getPage.onAppNotActive(jsonData['info']['appName']);
          getPage.onLoadfinished();
          return;
        }

        getPage.onError(status);
        getPage.onLoadfinished();
        return;
      } else if (code == 406) {
        getPage.onError(status);
        return;
      }

      if (status == "under-construction") {
        getPage.onUnderConstruction();
        getPage.onLoadfinished();
        return;
      }

      final info = jsonData['info'];

      if (info['appLocation']['status']) {
        final location = info['appLocation'];
        getPage.onAppLocation(
          location['location'],
          (location['lat']),
          (location['lon']),
        );
      }

      if (jsonData['announcement']['status']) {
        final announcement = jsonData['announcement'];
        getPage.onAnnouncement(announcement['announcementBody']);
      }

      if (info['homeCover']['status']) {
        getPage.onHomeCover(info['homeCover']['image']);
      }

      getPage.onResult(
        info['appName'],
        info['appIcon'],
        jsonData['pageTitle'],
        utf8.decode(base64.decode(jsonData['pageBody'])),
      );
      getPage.onLoadfinished();
    } else {
      getPage.onError('HTTP ${response.statusCode}');
      getPage.onLoadfinished();
    }
  } catch (e) {
    getPage.onError(e.toString());
    getPage.onLoadfinished();
  }
}