resolveBody static method

FutureOr<Object?> resolveBody(
  1. Object? payload,
  2. APIResponse apiResponse, {
  3. APIResponseCompression? compression,
  4. String? acceptEncoding,
})

Resolves a payload to an HTTP body (accepts payload as a Future). See resolveBodySync.

A JSON body is compressed with the best compression encoding accepted by acceptEncoding (defaults to the Accept-Encoding header of the APIResponse.apiRequest). If compression is null, a JSON body is always compressed with GZip (if large enough), for backward compatibility.

Implementation

static FutureOr<Object?> resolveBody(
  Object? payload,
  APIResponse apiResponse, {
  APIResponseCompression? compression,
  String? acceptEncoding,
}) {
  if (payload == null) return null;

  if (payload is Future) {
    // Listened in its `Zone`: an error of a `Future` created inside an
    // `APIRoot.call` (an error `Zone`) is not delivered outside it.
    // The result (a value, also on error) is delivered anywhere.
    return apiResponse.runInPayloadZone(
      () => payload.then(
        (value) {
          if (value == null) return null;
          return _resolveBodyImpl(
            value,
            apiResponse,
            compression,
            acceptEncoding,
          );
        },
        onError: (e, s) {
          return apiResponse.asError(error: 'ERROR: $e\n$s');
        },
      ),
    );
  }

  return _resolveBodyImpl(payload, apiResponse, compression, acceptEncoding);
}