com.github.tonivade.zeromock.api.HttpIOService 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.core.Matcher1;
import com.github.tonivade.purefun.monad.IO;
import com.github.tonivade.purefun.monad.IOOf;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.typeclasses.Instances;
public final class HttpIOService implements HttpRouteBuilderK, HttpIOService> {
private final HttpServiceK> serviceK;
public HttpIOService(String name) {
this(new HttpServiceK<>(name, Instances.>monad()));
}
private HttpIOService(HttpServiceK> serviceK) {
this.serviceK = requireNonNull(serviceK);
}
public String name() {
return serviceK.name();
}
public HttpIOService mount(String path, HttpIOService other) {
return new HttpIOService(serviceK.mount(path, other.serviceK));
}
public HttpIOService exec(RequestHandlerK> handler) {
return new HttpIOService(serviceK.exec(handler));
}
public ThenStepK, HttpIOService> preFilter(Matcher1 matcher) {
return new ThenStepK<>(serviceK.monad(), handler -> addPreFilter(matcher, handler));
}
public HttpIOService preFilter(PreFilter filter) {
return preFilter(filter.lift(serviceK.monad()));
}
public HttpIOService preFilter(PreFilterK> filter) {
return new HttpIOService(serviceK.preFilter(filter));
}
public HttpIOService postFilter(PostFilter filter) {
return postFilter(filter.lift(serviceK.monad()));
}
public HttpIOService postFilter(PostFilterK> filter) {
return new HttpIOService(serviceK.postFilter(filter));
}
@Override
public ThenStepK, HttpIOService> when(Matcher1 matcher) {
return new ThenStepK<>(serviceK.monad(), handler -> addMapping(matcher, handler));
}
public IO