1
resposta

Refatoração do método dispararRequisicao

Olá, tenho uma dúvida. Aqui optei por fazer 1 método só dispararRequisicao e passei como parametro algumas coisas que eram necessárias para utilizar o método ficou mais ou menos assim:

private static HttpResponse<String> dispararRequisicao(String param, String method, String endPoint, JsonObject json) throws IOException, InterruptedException {
        if (!param.isBlank() && method.equals("GET")) {
            HttpClient client = HttpClient.newHttpClient();
            String uri = API_URL + param + endPoint;
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(uri))
                    .method(method, HttpRequest.BodyPublishers.noBody())
                    .build();
            return client.send(request, HttpResponse.BodyHandlers.ofString());
        } else if (!param.isBlank() && method.equals("POST")) {
            HttpClient client = HttpClient.newHttpClient();
            String uri = "http://localhost:8080/abrigos/" + param + endPoint;
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(uri))
                    .header("Content-Type", "application/json")
                    .method(method, HttpRequest.BodyPublishers.ofString(json.toString()))
                    .build();

            return client.send(request, HttpResponse.BodyHandlers.ofString());
        } else if (method.equals("POST")){
            HttpClient client = HttpClient.newHttpClient();
            String uri = "http://localhost:8080/abrigos";
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(uri))
                    .header("Content-Type", "application/json")
                    .method("POST", HttpRequest.BodyPublishers.ofString(json.toString()))
                    .build();

            return client.send(request, HttpResponse.BodyHandlers.ofString());
        } else {
            HttpClient client = HttpClient.newHttpClient();
            String uri = "http://localhost:8080/abrigos";
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(uri))
                    .method("GET", HttpRequest.BodyPublishers.noBody())
                    .build();

            return client.send(request, HttpResponse.BodyHandlers.ofString());
        }
    }

Eu testei e funcionou, não sei se é certo deixar um método para fazer tudo (get,post,patch...etc)

1 resposta

Metodo muito grande, muitos codigos repetitivos, é bom refatorar seu codigo, pra deixar o mais limpo possivel, menos codigo repetitivo.

aqui está um exemplo.

private static final String API_URL = "http://localhost:8080/abrigos/";

private static HttpRequest buildHttpRequest(String uri, String method, JsonObject json) {
    HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
            .uri(URI.create(uri));

    if ("POST".equals(method)) {
        requestBuilder.header("Content-Type", "application/json")
                      .method("POST", HttpRequest.BodyPublishers.ofString(json.toString()));
    } else {
        requestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
    }

    return requestBuilder.build();
}

private static HttpResponse<String> dispararRequisicao(String param, String method, String endPoint, JsonObject json)
        throws IOException, InterruptedException {
    HttpClient client = HttpClient.newHttpClient();
    String uri;

    if (!param.isBlank() && "GET".equals(method)) {
        uri = API_URL + param + endPoint;
    } else {
        uri = API_URL + endPoint;
    }

    HttpRequest request = buildHttpRequest(uri, method, json);

    return client.send(request, HttpResponse.BodyHandlers.ofString());
}