com.github.tonivade.zeromock.api.AsyncHttpService Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2024, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.zeromock.api;
import static com.github.tonivade.zeromock.api.PreFilterK.filter;
import static java.util.Objects.requireNonNull;
import com.github.tonivade.purefun.concurrent.Future;
import com.github.tonivade.purefun.concurrent.FutureOf;
import com.github.tonivade.purefun.concurrent.Promise;
import com.github.tonivade.purefun.core.Matcher1;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.typeclasses.Instance;
import com.github.tonivade.purefun.typeclasses.Instances;
import java.util.concurrent.Executor;
public final class AsyncHttpService implements HttpRouteBuilderK, AsyncHttpService> {
private final HttpServiceK> serviceK;
public AsyncHttpService(String name) {
this(name, Future.DEFAULT_EXECUTOR);
}
public AsyncHttpService(String name, Executor executor) {
this(new HttpServiceK<>(name, new Instance>() {}.monad(executor)));
}
private AsyncHttpService(HttpServiceK> serviceK) {
this.serviceK = requireNonNull(serviceK);
}
public String name() {
return serviceK.name();
}
public HttpServiceK> build() {
return serviceK;
}
public AsyncHttpService mount(String path, AsyncHttpService other) {
return new AsyncHttpService(serviceK.mount(path, other.serviceK));
}
public AsyncHttpService exec(RequestHandlerK> handler) {
return new AsyncHttpService(serviceK.exec(handler));
}
public ThenStepK, AsyncHttpService> preFilter(Matcher1 matcher) {
return new ThenStepK<>(Instances.>monad(), handler -> addPreFilter(matcher, handler));
}
public AsyncHttpService preFilter(PreFilter filter) {
return preFilter(filter.lift(serviceK.monad()));
}
public AsyncHttpService preFilter(PreFilterK> filter) {
return new AsyncHttpService(serviceK.preFilter(filter));
}
public AsyncHttpService postFilter(PostFilter filter) {
return postFilter(filter.lift(serviceK.monad()));
}
public AsyncHttpService postFilter(PostFilterK> filter) {
return new AsyncHttpService(serviceK.postFilter(filter));
}
@Override
public ThenStepK, AsyncHttpService> when(Matcher1 matcher) {
return new ThenStepK<>(Instances.>monad(), handler -> addMapping(matcher, handler));
}
public Promise