post method

Future<ApiServiceResponse> post({
  1. required String endpoint,
  2. required Object data,
  3. Map<String, dynamic>? headers,
  4. bool? followRedirects,
  5. String? contentType,
  6. CancelToken? cancelToken,
})

post

Implementation

Future<ApiServiceResponse> post({
  required String endpoint,
  required Object data,
  Map<String, dynamic>? headers,
  bool? followRedirects,
  String? contentType,
  CancelToken? cancelToken,
}) async {
  LoggerService.logInfo('Endpoint: $endpoint');
  LoggerService.logInfo('Input: $data');
  LoggerService.logInfo('Headers: $headers');

  try {
    final response = await _dio.post<dynamic>(
      endpoint,
      data: data,
      options: Options(
        headers: headers,
        followRedirects: followRedirects ?? true,
        contentType: contentType,
      ),
      cancelToken: cancelToken,
    );
    LoggerService.logInfo('Status code $endpoint: ${response.statusCode}');
    if (response.statusCode == 200) {
      LoggerService.logInfo(
        'Response $endpoint: ${response.data}',
      );
      LoggerService.logInfo(
        'Extra $endpoint: ${response.extra}',
      );
      return SuccessResponse(
        data: response.data,
        header: response.headers,
        isRedirect: response.isRedirect,
      );
    }
    LoggerService.logError(
      'Status not 200 $endpoint: ${response.data}',
    );

    return FailureResponse(
      data: response.data,
      header: response.headers,
      isRedirect: response.isRedirect,
    );
  } on DioException catch (err) {
    LoggerService.logError(
      'Error $endpoint: $err',
    );
    return FailureResponse(data: 'DioException err: $err');
  } on FormatException {
    LoggerService.logError('Invalid Format: FormatException');
    return FailureResponse(data: 'Invalid Format');
  } catch (e) {
    LoggerService.logError(
      'Error $endpoint: $e',
    );
    return FailureResponse(data: 'Invalid Error');
  }
}