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

com.github.tonivade.zeromock.api.HttpURIOService Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2020, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.zeromock.api;

import static com.github.tonivade.purefun.Function1.cons;
import static com.github.tonivade.purefun.effect.URIOOf.toURIO;
import static com.github.tonivade.zeromock.api.PreFilterK.filter;
import static java.util.Objects.requireNonNull;

import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Matcher1;
import com.github.tonivade.purefun.effect.URIO;
import com.github.tonivade.purefun.effect.URIO_;
import com.github.tonivade.purefun.instances.URIOInstances;
import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Option;

public final class HttpURIOService {

  private final HttpServiceK> serviceK;

  public HttpURIOService(String name) {
    this(new HttpServiceK<>(name, URIOInstances.monad()));
  }

  private HttpURIOService(HttpServiceK> serviceK) {
    this.serviceK = requireNonNull(serviceK);
  }

  public String name() {
    return serviceK.name();
  }

  public HttpURIOService mount(String path, HttpURIOService other) {
    return new HttpURIOService<>(this.serviceK.mount(path, other.serviceK));
  }

  public HttpURIOService exec(URIO method) {
    return new HttpURIOService<>(serviceK.exec(cons(method)::apply));
  }

  public ThenStep> preFilter(Matcher1 matcher) {
    return handler -> addPreFilter(matcher, handler);
  }

  public HttpURIOService preFilter(PreFilter filter) {
    return preFilter(filter.andThen(URIO::>pure)::apply);
  }

  public HttpURIOService preFilter(URIOPreFilter filter) {
    return new HttpURIOService<>(serviceK.preFilter(filter));
  }

  public HttpURIOService postFilter(PostFilter filter) {
    return postFilter(filter.andThen(URIO::pure)::apply);
  }

  public HttpURIOService postFilter(URIOPostFilter filter) {
    return new HttpURIOService<>(serviceK.postFilter(filter));
  }

  public ThenStep> when(Matcher1 matcher) {
    return handler -> addMapping(matcher, handler);
  }

  public URIO> execute(HttpRequest request) {
    return serviceK.execute(request).fix(toURIO());
  }

  public HttpURIOService combine(HttpURIOService other) {
    return new HttpURIOService<>(this.serviceK.combine(other.serviceK));
  }

  public HttpServiceK> build() {
    return serviceK;
  }

  protected HttpURIOService addMapping(Matcher1 matcher, URIORequestHandler handler) {
    return new HttpURIOService<>(serviceK.addMapping(matcher, handler));
  }

  protected HttpURIOService addPreFilter(Matcher1 matcher, URIORequestHandler handler) {
    return preFilter(filter(URIOInstances.monad(), matcher, handler)::apply);
  }

  @Override
  public String toString() {
    return "HttpURIOService(" + serviceK.name() + ")";
  }
  
  @FunctionalInterface
  public interface ThenStep {
    T then(URIORequestHandler handler);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy