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

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

The newest version!
package io.hireproof.structure

import cats.MonadThrow
import cats.data.Validated
import cats.syntax.all._

abstract class UnauthorizedClient[F[_]] {
  def trySubmit[I, O](endpoint: Endpoint[I, O])(input: I): F[Validated[Errors, O]]

  final def submit[I, O](endpoint: Endpoint[I, O])(input: I)(implicit F: MonadThrow[F]): F[O] =
    trySubmit(endpoint)(input).flatMap(_.liftTo[F])
}

abstract class AuthorizedClient[F[_]] {
  def trySubmit[T, I, O](endpoint: Endpoint.Authorized[T, I, O])(token: T, input: I): F[Validated[Errors, O]]

  final def submit[T, I, O](endpoint: Endpoint.Authorized[T, I, O])(token: T, input: I)(implicit
      F: MonadThrow[F]
  ): F[O] =
    trySubmit(endpoint)(token, input).flatMap(_.liftTo[F])
}

abstract class Client[F[_]] {
  def unauthorized: UnauthorizedClient[F]

  def authorized: AuthorizedClient[F]
}

object Client {
  def apply[F[_]](
      unauthorizedClient: UnauthorizedClient[F],
      authorizedClient: AuthorizedClient[F]
  ): Client[F] = new Client[F] {
    override def unauthorized: UnauthorizedClient[F] = unauthorizedClient
    override def authorized: AuthorizedClient[F] = authorizedClient
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy