processResponse method

Future processResponse(
  1. Response response, [
  2. bool returnPayloadOnly = true
])

Implementation

Future<dynamic> processResponse(http.Response response, [bool returnPayloadOnly = true]) {
  debug("processResponse http status ${response.statusCode}");
  //debug("processResponse http headers ${response.headers}");
  //debug("processResponse http reasonPhrase ${response.reasonPhrase}");
  //debug("processResponse http res ${response.body}");
  // check response herader for the content_type and process accordingly.
  String? contentType = response.headers["content-type"];
  debug("processResponse contentType $contentType");
  if (contentType != null && contentType == "application/octet-stream") {
    return Future<dynamic>.value(response.bodyBytes);
  } else {
    Map<String, dynamic> resBody = jsonDecode(response.body);
    if (response.statusCode < 400 && resBody.containsKey("access_token") ) {
      return Future<dynamic>.value(resBody);
    } else if (response.statusCode < 400 && resBody.containsKey("payload") && resBody.containsKey("status") && (resBody["status"] < 400) && returnPayloadOnly) {
      debug("Returning success payload");
      return Future<dynamic>.value(resBody["payload"]);
    } else if (response.statusCode < 400 && resBody.containsKey("status") && resBody["status"] < 400 ) {
      debug("Returning success entire response");
      return Future<dynamic>.value(resBody);
    } else  {
      debug("processResponse error: ${response.body}");
      return throw( VasatError.fromMap(resBody) );
    }
  }

}