put method

Future<RestResponse> put(
  1. String apiEndPoint,
  2. {Map<String, String> headers,
  3. Map<String, 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> put(
  String apiEndPoint, {
  Map<String, String> headers,
  Map<String, dynamic> 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,
  );
  //este operador "??=" é a mesmo que if(x != null)
  encoding ??= Utf8Codec();

  headers ??= headersDefault;

  var resp = await client.put(url, body: jsonEncode(body), encoding: encoding, 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);
}