post method

Future<Response?> post(
  1. String apiPath, {
  2. Object? body,
  3. bool jsonEncodeBody = true,
})

Implementation

Future<Response?> post(
  String apiPath, {
  Object? body,
  bool jsonEncodeBody = true,
}) async {
  Logger.log(
    "--------> START OF POST API CALL <--------",
    type: LogType.api,
    tag: "DF-API-CLIENT",
  );
  Object? requestBody = body;
  final client = http.Client();

  if (jsonEncodeBody && body != null) {
    requestBody = jsonEncode(body);
  }
  Uri apiUri = _generateApiUri(apiPath);
  return _processApiCall(
    httpApiConfig.maxRetryAttempts,
    apiCall: () async {
      return await client
          .post(
            apiUri,
            encoding: httpApiConfig.encoding,
            body: requestBody,
            headers: httpApiConfig.headers,
          )
          .timeout(
            Duration(seconds: httpApiConfig.timeout),
            onTimeout: () => throw Exception(
              "API Timeout exception, no response from the server for: $apiPath",
            ),
          );
    },
    onFinish: client.close,
  );
}