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

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

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy