post method

Future<RestResponse> post(
  1. String apiEndPoint,
  2. {Map<String, String> headers,
  3. dynamic body,
  4. Map<String, String> queryParameters,
  5. Encoding encoding,
  6. String basePath,
  7. String protocol,
  8. String hosting,
  9. int hostPort}
)

Implementation

Future<RestResponse> post(
  String apiEndPoint, {
  Map<String, String> headers,
  body,
  Map<String, String> queryParameters,
  Encoding encoding,
  String basePath,
  String protocol,
  String hosting,
  int hostPort,
}) async {
  var url = uri(
    apiEndPoint,
    queryParameters: queryParameters,
    host: hosting,
    basePath: basePath,
    protocol: protocol,
    port: hostPort,
  );

  headers ??= headersDefault;

  var resp = await client.post(url, body: jsonEncode(body), encoding: Utf8Codec(), headers: headers);

  if (resp.statusCode == 200) {
    return RestResponse(
        message: 'Sucesso', status: RestStatus.SUCCESS, data: jsonDecode(resp.body), statusCode: resp.statusCode);
  }

  var jsonDecoded = jsonDecode(resp.body);
  var message = '${resp.body}';
  var exception = '${resp.body}';
  //exibe mensagem se der erro não autorizado
  if (resp.statusCode == 401) {
    if (jsonDecoded is Map) {
      if (jsonDecoded.containsKey('message')) {
        message = jsonDecoded['message'];
      }
      if (jsonDecoded.containsKey('exception')) {
        exception = jsonDecoded['exception'];
      }
    }

    return RestResponse(message: message, status: RestStatus.UNAUTHORIZED, statusCode: resp.statusCode);
  }

  if (jsonDecoded is Map) {
    if (jsonDecoded.containsKey('message')) {
      message = jsonDecoded['message'];
    }
    if (jsonDecoded.containsKey('exception')) {
      exception = jsonDecoded['exception'];
    }
  }

  return RestResponse(message: message, exception: exception, status: RestStatus.DANGER, statusCode: resp.statusCode);
}