featherbed icon indicating copy to clipboard operation
featherbed copied to clipboard

make client val lazy to allow Client to be extended

Open MC-Escherichia opened this issue 7 years ago • 1 comments

I recently had a use case where I needed to access a server which had self signed certificates, so I needed to change the underlying Client code in featherbed to use other finagle options. It turns out if you make the instantiation of the val client in the class client lazy, you can do this by extending the class without modifying the Client.scala source. Without the client being lazy, you get initialization errors.

Here was my use case:

package featherbed 

class InsecureClient(
  baseUrl: URL,
  charset: Charset = StandardCharsets.UTF_8)
  extends Client(baseUrl, charset) {

  override protected lazy val client = clientTransform(InsecureClient.forUrl(baseUrl))
}

object InsecureClient {

  private[featherbed] def forUrl(url: URL) = {
    val client = Http.Client()
    if (url.getProtocol == "https") client.withTlsWithoutValidation else client
  }
}

Let me know if more needs to be done.

MC-Escherichia avatar Nov 03 '17 20:11 MC-Escherichia

Codecov Report

Merging #73 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #73   +/-   ##
=======================================
  Coverage   67.71%   67.71%           
=======================================
  Files           8        8           
  Lines         254      254           
  Branches        3        2    -1     
=======================================
  Hits          172      172           
  Misses         82       82
Impacted Files Coverage Δ
...herbed-core/src/main/scala/featherbed/Client.scala 92% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1cd0911...26e2498. Read the comment docs.

codecov-io avatar Nov 03 '17 20:11 codecov-io