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

client.Client.scala Maven / Gradle / Ivy

There is a newer version: 0.4.5
Show newest version
/**
 *   __________            .__  __  .__
 *   \____    /____   ____ |__|/  |_|  |__
 *     /     // __ \ /    \|  \   __\  |  \
 *    /     /\  ___/|   |  \  ||  | |   Y  \
 *   /_______ \___  >___|  /__||__| |___|  /
 *           \/   \/     \/              \/
 */
package zenith.client

import zenith._
import scala.util.{Success, Failure}

/**
 * HttpClientConfig
 */
final case class HttpClientConfig (
  timeoutMs: Int = 5000)


/**
 * HttpClient
 */
final case class HttpClient[Z[_]: Async](private val sendFn: HttpRequest => Z[HttpResponse])(config: HttpClientConfig) {
  def send (request: HttpRequest): Z[HttpResponse] = sendFn (request)

  def sendEx (request: HttpRequest): Z[HttpExchange] = {
    val startTime = System.nanoTime ()
    val result: Z[HttpResponse] = send (request)
    def timeMs: Int = (System.nanoTime () - startTime).toInt / 1000000
    Async[Z].transform (result) {
      case Failure (ex) => HttpExchange (request, Left (ex), timeMs)
      case Success (response) => HttpExchange (request, Right (response), timeMs)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy