package org.dominokit.domino.api.client.request;

import org.dominokit.domino.api.shared.request.FailedResponseBean;
import org.dominokit.domino.api.shared.request.RequestBean;
import org.dominokit.domino.api.shared.request.ResponseBean;
import org.dominokit.domino.api.shared.request.VoidResponse;
import org.dominokit.jacksonapt.AbstractObjectMapper;
import org.dominokit.rest.client.FailedResponse;
import org.dominokit.rest.shared.Response;
import org.dominokit.rest.shared.RestfulRequest;

import java.util.Arrays;

import static java.util.Objects.isNull;

public abstract class AbstractRequestSender<R extends RequestBean, S extends ResponseBean> implements RequestRestSender<R, S> {

    @Override
    public void send(ServerRequest<R, S> request, ServerRequestCallBack callBack) {
        new RequestPathHandler<>(request, getPath(), getCustomRoot()).process(serverRequest -> serverRequest.setUrl(replaceRequestParameters(serverRequest.getUrl(), serverRequest.requestBean())));
        RestfulRequest restfulRequest = RestfulRequest.get(request.getUrl())
                .putHeaders(request.headers())
                .putParameters(request.parameters())
                .onSuccess(response -> {
                            if (Arrays.stream(getSuccessCodes()).anyMatch(code -> code.equals(response.getStatusCode()))) {
                                if (isVoidResponse()) {
                                    callBack.onSuccess(new VoidResponse());
                                } else {
                                    readResponse(callBack, response);
                                }
                            } else {
                                callBack.onFailure(new FailedResponseBean(response.getStatusCode(), response.getStatusText(), response.getBodyAsString(), response.getHeaders()));
                            }
                        }
                ).onError(throwable -> callBack.onFailure(new FailedResponseBean(throwable)));
        if ("get".equalsIgnoreCase(getMethod())) {
            restfulRequest.send();
        } else {
            restfulRequest.send(getRequestMapper().write(request.requestBean()));
        }
    }

    protected abstract void readResponse(ServerRequestCallBack callBack, Response response);

    protected abstract String getPath();

    protected abstract Integer[] getSuccessCodes();

    protected abstract String replaceRequestParameters(String path, R request);

    protected abstract AbstractObjectMapper<R> getRequestMapper();

    protected abstract String getMethod();

    protected abstract String getCustomRoot();

    protected abstract boolean isVoidResponse();
    protected abstract boolean isVoidRequest();

}


@@ -350,57 +350,8 @@
 er;%0A
-import org.dominokit.rest.client.FailedResponse;%0A
 impo
@@ -381,32 +381,32 @@
 hared.Response;%0A
+
 import org.domin
@@ -468,23 +468,15 @@
 ys;%0A
-%0A
 import 
-static 
 java
@@ -485,22 +485,12 @@
 til.
-Objects.isNull
+List
 ;%0A%0Ap
@@ -617,16 +617,105 @@
 , S%3E %7B%0A%0A
+    private final List%3CString%3E SEND_BODY_METHODS = Arrays.asList(%22POST%22,%22PUT%22,%22PATCH%22);%0A%0A
     @Ove
@@ -1056,18 +1056,22 @@
 Request.
-ge
+reques
 t(reques
@@ -1080,16 +1080,43 @@
 getUrl()
+, getMethod().toUpperCase()
 )%0A      
@@ -2034,79 +2034,144 @@
 if (
-%22get%22.equalsIgnoreCase(getMethod())) %7B%0A            restfulRequest.send(
+SEND_BODY_METHODS.contains(getMethod().toUpperCase())) %7B%0A            restfulRequest.send(getRequestMapper().write(request.requestBean())
 );%0A 
@@ -2222,55 +2222,8 @@
 end(
-getRequestMapper().write(request.requestBean())
 );%0A 

fix the request is always sent as get