responsePageFromResponse function

ResponsePage? responsePageFromResponse(
  1. Response response
)

Parses a Dio Response response and returns a corresponding ResponsePage.

  • response: The Dio response to parse.

Returns a ResponsePage based on the parsed data, or null if no valid response page can be created.

Implementation

ResponsePage? responsePageFromResponse(Response response) {
  String? captchaUrl;
  ResponsePage? responsePage;
  MapEntry<String, List<String>> ddheader = response.headers.map.entries
      .firstWhere((element) =>
          (element.key.toLowerCase() == 'x-dd-b') ||
          (element.key.toLowerCase() == 'x-sf-cc-x-dd-b'));
  int xddbHeader = int.parse(ddheader.value.first);
  var contentType = response.headers.value("content-type");
  if (contentType != null &&
      response.data != null &&
      contentType.contains("text/html")) {
    var body = response.data;
    CaptchaPage.requestUrl = response.realUri;
    responsePage = new HtmlResponsePage(xddbHeader, body, response.realUri);
  } else {
    switch (response.requestOptions.responseType) {
      case ResponseType.json:
        captchaUrl = response.data['url'];
        break;
      case ResponseType.stream:
        DataDomeLogger.warning(
            "The SDK does not support Stream ResponseType. Please change it to json.");
        break;
      case ResponseType.plain:
        try {
          var jsonObj = jsonDecode(response.data);
          captchaUrl = jsonObj['url'];
        } catch (e) {
          DataDomeLogger.error(
              "Error decoding the response body to retrieve URL: $e");
          captchaUrl = null; // Return null URL variable
        }
        break;
      case ResponseType.bytes:
        try {
          final decodedJSON = jsonDecode(utf8.decode(response.data));
          captchaUrl = decodedJSON['url'];
        } catch (e) {
          DataDomeLogger.error(
              "Error decoding the response body to retrieve URL: $e");
          captchaUrl = null; // Return null URL variable
        }
        break;
    }

    if (captchaUrl != null) {
      responsePage = new UrlResponsePage(xddbHeader, captchaUrl);
    }
  }
  return responsePage;
}