Repository: square/okhttp
File: okhttp-sse/src/main/kotlin/okhttp3/sse/internal/RealEventSource.kt (Kotlin)

Code before:

import java.io.IOException
import okhttp3.Call
import okhttp3.Callback
import okhttp3.EventListener
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.internal.EMPTY_RESPONSE
import okhttp3.internal.connection.RealCall
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener

class RealEventSource(
  private val request: Request,
  private val listener: EventSourceListener
) : EventSource, ServerSentEventReader.Callback, Callback {
  private var call: RealCall? = null

  fun connect(client: OkHttpClient) {
    val client = client.newBuilder()
        .eventListener(EventListener.NONE)
        .build()
    call = client.newCall(request) as RealCall
    call?.enqueue(this)
  }

  override fun onResponse(call: Call, response: Response) {
    processResponse(response)
  }

  fun processResponse(response: Response) {
    response.use {
      if (!response.isSuccessful) {
        listener.onFailure(this, null, response)
        return
      }

      val body = response.body!!

      if (!body.isEventStream()) {
        listener.onFailure(this,
            IllegalStateException("Invalid content-type: ${body.contentType()}"), response)
        return
      }

      // This is a long-lived response. Cancel full-call timeouts.
      call?.timeoutEarlyExit()

      // Replace the body with an empty one so the callbacks can't see real data.

Review comment (refactor):
Using `?` here is inappropriate as it will never be null. `!!` is better, but annoying. I would prefer just extracting a local we can re-use:
```kotlin
val realCall = client.newCall(request) as RealCall
call = realCall
realCall.enqueue(this)
```

Code after:

import java.io.IOException
import okhttp3.Call
import okhttp3.Callback
import okhttp3.EventListener
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.internal.EMPTY_RESPONSE
import okhttp3.internal.connection.RealCall
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener

class RealEventSource(
  private val request: Request,
  private val listener: EventSourceListener
) : EventSource, ServerSentEventReader.Callback, Callback {
  private var call: RealCall? = null

  fun connect(client: OkHttpClient) {
    val client = client.newBuilder()
        .eventListener(EventListener.NONE)
        .build()
    val realCall = client.newCall(request) as RealCall
    call = realCall
    realCall.enqueue(this)
  }

  override fun onResponse(call: Call, response: Response) {
    processResponse(response)
  }

  fun processResponse(response: Response) {
    response.use {
      if (!response.isSuccessful) {
        listener.onFailure(this, null, response)
        return
      }

      val body = response.body!!

      if (!body.isEventStream()) {
        listener.onFailure(this,
            IllegalStateException("Invalid content-type: ${body.contentType()}"), response)
        return
      }

      // This is a long-lived response. Cancel full-call timeouts.
      call?.timeoutEarlyExit()