/*
 * Skybot, a multipurpose discord bot
 *      Copyright (C) 2017  Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32"
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package ml.duncte123.skybot.objects;

import com.github.natanbc.reliqua.request.PendingRequest;
import com.github.natanbc.reliqua.request.RequestException;
import me.duncte123.botcommons.web.WebUtils;
import okhttp3.Request;
import okhttp3.Response;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * Creates a fake {@link PendingRequest} because these things should not return null <br>
 * NOTE: the error consumer from async is never called meaning that this "request" will never throw
 *
 * @param <T>
 *     what to return from this pending request
 */
public class FakePendingRequest<T> extends PendingRequest<T> {
    private static final Request DUMMY_REQUEST = WebUtils.defaultRequest().url("http://localhost/").build();
    private final T resp;

    public FakePendingRequest(@Nonnull T resp) {
        // Oh the cheats, this null is safe
        //noinspection ConstantConditions
        super(WebUtils.ins, null, DUMMY_REQUEST);

        this.resp = resp;
    }

    @Nullable
    @Override
    @SuppressWarnings("NullableProblems")
    protected T onSuccess(@Nonnull Response response) {
        return this.resp;
    }

    @Override
    public void async(@Nullable Consumer<T> onSuccess, @Nullable Consumer<RequestException> onError) {
        final Consumer<T> finalOnSuccess = Objects.requireNonNullElseGet(onSuccess, () -> (ignored) -> {});

        // this should work fine
        //noinspection ConstantConditions
        finalOnSuccess.accept(this.onSuccess(null));
    }
}

@@ -975,53 +975,8 @@
 on;%0A
-import me.duncte123.botcommons.web.WebUtils;%0A
 impo
@@ -991,24 +991,24 @@
 p3.Request;%0A
+
 import okhtt
@@ -1481,117 +1481,8 @@
 %3E %7B%0A
-    private static final Request DUMMY_REQUEST = WebUtils.defaultRequest().url(%22http://localhost/%22).build();%0A
     
@@ -1657,41 +1657,34 @@
 per(
-WebUtils.ins, null, DUMMY_REQUEST
+null, null, (Request) null
 );%0A%0A
@@ -1748,50 +1748,8 @@
 ide%0A
-    @SuppressWarnings(%22NullableProblems%22)%0A
     
@@ -1815,25 +1815,20 @@
  return 
-this.resp
+null
 ;%0A    %7D%0A
@@ -2091,50 +2091,8 @@
 ine%0A
-        //noinspection ConstantConditions%0A
     
@@ -2126,23 +2126,12 @@
 his.
-onSuccess(null)
+resp
 );%0A 

Clean up fake pending request