All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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> execute(HttpRequest request) {
    return serviceK.execute(request).fix(IOOf::toIO);
  }

  public HttpIOService combine(HttpIOService other) {
    return new HttpIOService(serviceK.combine(other.serviceK));
  }

  public HttpServiceK> build() {
    return serviceK;
  }

  public HttpIOService addMapping(Matcher1 matcher, RequestHandlerK> handler) {
    return new HttpIOService(serviceK.addMapping(matcher, handler));
  }

  private HttpIOService addPreFilter(Matcher1 matcher, RequestHandlerK> handler) {
    return preFilter(filter(serviceK.monad(), matcher, handler));
  }

  @Override
  public String toString() {
    return "HttpIOService(" + serviceK.name() + ")";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy