<commit_before>package org.http4s.client.okhttp

import java.io.IOException

import cats.data._
import cats.effect._
import cats.effect.implicits._
import okhttp3.{
  Call,
  Callback,
  OkHttpClient,
  Protocol,
  RequestBody,
  Headers => OKHeaders,
  MediaType => OKMediaType,
  Request => OKRequest,
  Response => OKResponse
}
import okio.BufferedSink
import org.http4s.{Header, Headers, HttpVersion, Method, Request, Response, Status}
import org.http4s.client.{Client, DisposableResponse}
import fs2.Stream._
import fs2._
import fs2.io._

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext

object OkHttp {

  def defaultconfig[F[_]]()(implicit F: Effect[F]): F[OkHttpClient.Builder] = F.delay {
    new OkHttpClient.Builder()
      .protocols(
        List(
          Protocol.HTTP_2,
          Protocol.HTTP_1_1
        ).asJava)
  }


  def apply[F[_]](config: F[OkHttpClient.Builder] = null)(
      implicit F: Effect[F],
      ec: ExecutionContext): F[Client[F]] = F.map(Option(config).getOrElse(defaultconfig[F]())) { c =>
    val client = c.build()
    Client(
      Kleisli { req =>
        F.async[DisposableResponse[F]] { cb =>
          client.newCall(toOkHttpRequest(req)).enqueue(handler(cb))
          ()
        }
      },
      F.delay({
        client.dispatcher.executorService().shutdown()
        client.connectionPool().evictAll()
        if (client.cache() != null) {
          client.cache().close()
        }
      })
    )
  }

  def stream[F[_]](config: F[OkHttpClient.Builder] = null)(<commit_msg>Split OkHttp apply into default and config flavours<commit_after>package org.http4s.client.okhttp

import java.io.IOException

import cats.data._
import cats.effect._
import cats.effect.implicits._
import okhttp3.{
  Call,
  Callback,
  OkHttpClient,
  Protocol,
  RequestBody,
  Headers => OKHeaders,
  MediaType => OKMediaType,
  Request => OKRequest,
  Response => OKResponse
}
import okio.BufferedSink
import org.http4s.{Header, Headers, HttpVersion, Method, Request, Response, Status}
import org.http4s.client.{Client, DisposableResponse}
import fs2.Stream._
import fs2._
import fs2.io._

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext

object OkHttp {

  def defaultconfig[F[_]]()(implicit F: Effect[F]): F[OkHttpClient.Builder] = F.delay {
    new OkHttpClient.Builder()
      .protocols(
        List(
          Protocol.HTTP_2,
          Protocol.HTTP_1_1
        ).asJava)
  }

  /** Create an okhttp client with the default config */
  def apply[F[_]]()(implicit F: Effect[F], ec: ExecutionContext): F[Client[F]] =
    apply(defaultconfig[F]())

  /** Create an okhttp client with a supplied config */
  def apply[F[_]](config: F[OkHttpClient.Builder])(
      implicit F: Effect[F],
      ec: ExecutionContext): F[Client[F]] = F.map(config) { c =>
    val client = c.build()
    Client(
      Kleisli { req =>
        F.async[DisposableResponse[F]] { cb =>
          client.newCall(toOkHttpRequest(req)).enqueue(handler(cb))
          ()
        }
      },
      F.delay({
        client.dispatcher.executorService().shutdown()
        client.connectionPool().evictAll()
        if (client.cache() != null) {
          client.cache().close()
        }
      })
    )
  }

  def stream[F[_]](config: F[OkHttpClient.Builder] = null)(
{'fraction_non_alphanumeric': 0.13335331135750675, 'fraction_numerical': 0.005993407252022775, 'mean_word_length': 3.1007371007371005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4670078', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1074, 'n_words': 219}