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

io.hireproof.structure.Endpoint.scala Maven / Gradle / Ivy

The newest version!
package io.hireproof.structure

import io.hireproof.structure.dsl._

final case class Endpoint[I, O](input: Input[I], output: Output[O]) {
  def modifyInput[T](f: Input[I] => Input[T]): Endpoint[T, O] = copy(input = f(input))
  def modifyOutput[T](f: Output[O] => Output[T]): Endpoint[I, T] = copy(output = f(output))
  def implementedBy[F[_]](f: I => F[O]): Endpoint.Implementation[F, I, O] = Endpoint.Implementation(this, f)
}

object Endpoint {
  final case class Implementation[F[_], I, O](endpoint: Endpoint[I, O], implementation: I => F[O]) {
    def toRoutes: Routes[F] = Routes.one(this)

    def ~(endpoint: Endpoint.Implementation[F, _, _]): Routes[F] = toRoutes ~ endpoint
  }

  type Authorized[T, I, O] = Endpoint[Authorization[T, I], Authorization.Response[O]]

  object Authorized {
    type Implementation[F[_], T, I, O] = Endpoint.Implementation[F, Authorization[T, I], Authorization.Response[O]]

    def apply[T, I, O](
        authorization: Input[I] => Input[Authorization[T, I]]
    )(endpoint: Endpoint[I, O]): Endpoint.Authorized[T, I, O] = endpoint
      .modifyInput(authorization)
      .modifyOutput(_.modifyResults { results =>
        (
          results.imap(Authorization.Response.Authorized[O])(_.value) |+|
            result(code.unauthorized, const(Authorization.Response.Unauthorized))
        ).ximap[Authorization.Response[O]]
      })
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy