Skip to content

Commit 185c408

Browse files
authored
Revert "refactor(SSE): move static functions to extensions (#9010)" (#9098)
* Revert "refactor(SSE): move static functions to extensions (#9010)" This reverts commit 09fb9a4. * Remove unnecessary control flow from a test (#9099) Co-authored-by: Jesse Wilson <jwilson@squareup.com> --------- Co-authored-by: Jesse Wilson <jwilson@squareup.com>
1 parent 7721acd commit 185c408

9 files changed

Lines changed: 48 additions & 194 deletions

File tree

okhttp-sse/api/okhttp-sse.api

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
public abstract interface class okhttp3/sse/EventSource {
2-
public static final field Companion Lokhttp3/sse/EventSource$Companion;
32
public abstract fun cancel ()V
4-
public static fun enqueue (Lokhttp3/Call;Lokhttp3/sse/EventSourceListener;)Lokhttp3/sse/EventSource;
5-
public static fun process (Lokhttp3/Response;Lokhttp3/sse/EventSourceListener;)V
63
public abstract fun request ()Lokhttp3/Request;
74
}
85

9-
public final class okhttp3/sse/EventSource$Companion {
10-
public final fun enqueue (Lokhttp3/Call;Lokhttp3/sse/EventSourceListener;)Lokhttp3/sse/EventSource;
11-
public final fun process (Lokhttp3/Response;Lokhttp3/sse/EventSourceListener;)V
12-
}
13-
146
public abstract interface class okhttp3/sse/EventSource$Factory {
15-
public static final field Companion Lokhttp3/sse/EventSource$Factory$Companion;
16-
public static fun create (Lokhttp3/Call$Factory;)Lokhttp3/sse/EventSource$Factory;
177
public abstract fun newEventSource (Lokhttp3/Request;Lokhttp3/sse/EventSourceListener;)Lokhttp3/sse/EventSource;
188
}
199

20-
public final class okhttp3/sse/EventSource$Factory$Companion {
21-
public final fun create (Lokhttp3/Call$Factory;)Lokhttp3/sse/EventSource$Factory;
22-
}
23-
2410
public abstract class okhttp3/sse/EventSourceListener {
2511
public fun <init> ()V
2612
public fun onClosed (Lokhttp3/sse/EventSource;)V

okhttp-sse/src/main/kotlin/okhttp3/sse/EventSource.kt

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
*/
1616
package okhttp3.sse
1717

18-
import okhttp3.Call
1918
import okhttp3.Request
20-
import okhttp3.Response
21-
import okhttp3.sse.internal.RealEventSource
2219

2320
interface EventSource {
2421
/** Returns the original request that initiated this event source. */
@@ -40,40 +37,5 @@ interface EventSource {
4037
request: Request,
4138
listener: EventSourceListener,
4239
): EventSource
43-
44-
companion object {
45-
/**
46-
* Wraps a [Call.Factory] into [EventSource.Factory].
47-
*/
48-
@JvmStatic
49-
@JvmName("create")
50-
fun Call.Factory.asEventSourceFactory(): Factory =
51-
Factory { request, listener ->
52-
val actualRequest =
53-
if (request.header("Accept") == null) {
54-
request.newBuilder().addHeader("Accept", "text/event-stream").build()
55-
} else {
56-
request
57-
}
58-
59-
this.newCall(actualRequest).enqueueEventSource(listener)
60-
}
61-
}
62-
}
63-
64-
companion object {
65-
/**
66-
* Enqueues a [Call] and process it as [EventSource] with [listener].
67-
*/
68-
@JvmStatic
69-
@JvmName("enqueue")
70-
fun Call.enqueueEventSource(listener: EventSourceListener): EventSource = RealEventSource(this, listener).also(this::enqueue)
71-
72-
/**
73-
* Processes the existing response with [listener].
74-
*/
75-
@JvmStatic
76-
@JvmName("process")
77-
fun Response.processEventSource(listener: EventSourceListener) = RealEventSource(this, listener).processResponse(this)
7840
}
7941
}

okhttp-sse/src/main/kotlin/okhttp3/sse/EventSourceListener.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ abstract class EventSourceListener {
2828
) {
2929
}
3030

31-
/**
32-
* Invoked when a new event has been sent to the client.
33-
*
34-
* @param id The `id` line of the event, might be null.
35-
* @param type The `event` line of the event, might be null.
36-
* @param data The `data` line of the event.
37-
*/
3831
open fun onEvent(
3932
eventSource: EventSource,
4033
id: String?,
@@ -44,8 +37,6 @@ abstract class EventSourceListener {
4437
}
4538

4639
/**
47-
* Invoked when the HTTP connection has been closed normally.
48-
*
4940
* No further calls to this listener will be made.
5041
*/
5142
open fun onClosed(eventSource: EventSource) {

okhttp-sse/src/main/kotlin/okhttp3/sse/EventSources.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,37 @@ package okhttp3.sse
1818
import okhttp3.Call
1919
import okhttp3.OkHttpClient
2020
import okhttp3.Response
21-
import okhttp3.sse.EventSource.Companion.processEventSource
22-
import okhttp3.sse.EventSource.Factory.Companion.asEventSourceFactory
21+
import okhttp3.sse.internal.RealEventSource
2322

2423
object EventSources {
2524
@Deprecated(
2625
message = "required for binary-compatibility!",
2726
level = DeprecationLevel.HIDDEN,
2827
)
2928
@JvmStatic
30-
fun createFactory(client: OkHttpClient) = client.asEventSourceFactory()
29+
fun createFactory(client: OkHttpClient) = createFactory(client as Call.Factory)
3130

32-
@Deprecated(
33-
message = "Moved to extension function.",
34-
replaceWith =
35-
ReplaceWith(
36-
expression = "callFactory.asEventSourceFactory()",
37-
imports = ["okhttp3.sse.EventSource.Factory.Companion.asEventSourceFactory"],
38-
),
39-
level = DeprecationLevel.WARNING,
40-
)
4131
@JvmStatic
42-
fun createFactory(callFactory: Call.Factory): EventSource.Factory = callFactory.asEventSourceFactory()
32+
fun createFactory(callFactory: Call.Factory): EventSource.Factory =
33+
EventSource.Factory { request, listener ->
34+
val actualRequest =
35+
if (request.header("Accept") == null) {
36+
request.newBuilder().addHeader("Accept", "text/event-stream").build()
37+
} else {
38+
request
39+
}
40+
41+
RealEventSource(actualRequest, listener).apply {
42+
connect(callFactory)
43+
}
44+
}
4345

44-
@Deprecated(
45-
message = "Moved to extension function.",
46-
replaceWith =
47-
ReplaceWith(
48-
expression = "response.processEventSource(listener)",
49-
imports = ["okhttp3.sse.EventSource.Companion.processEventSource"],
50-
),
51-
level = DeprecationLevel.WARNING,
52-
)
5346
@JvmStatic
5447
fun processResponse(
5548
response: Response,
5649
listener: EventSourceListener,
57-
): Unit = response.processEventSource(listener)
50+
) {
51+
val eventSource = RealEventSource(response.request, listener)
52+
eventSource.processResponse(response)
53+
}
5854
}

okhttp-sse/src/main/kotlin/okhttp3/sse/internal/RealEventSource.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,31 @@ import okhttp3.internal.stripBody
2525
import okhttp3.sse.EventSource
2626
import okhttp3.sse.EventSourceListener
2727

28-
internal class RealEventSource private constructor(
29-
private val call: Call?,
28+
internal class RealEventSource(
3029
private val request: Request,
3130
private val listener: EventSourceListener,
3231
) : EventSource,
3332
ServerSentEventReader.Callback,
3433
Callback {
35-
constructor(call: Call, listener: EventSourceListener) : this(call, call.request(), listener)
36-
37-
constructor(response: Response, listener: EventSourceListener) : this(null, response.request, listener)
34+
private var call: Call? = null
3835

3936
@Volatile private var canceled = false
4037

38+
fun connect(callFactory: Call.Factory) {
39+
call =
40+
callFactory.newCall(request).apply {
41+
enqueue(this@RealEventSource)
42+
}
43+
}
44+
4145
override fun onResponse(
4246
call: Call,
4347
response: Response,
4448
) {
4549
processResponse(response)
4650
}
4751

48-
internal fun processResponse(response: Response) {
52+
fun processResponse(response: Response) {
4953
response.use {
5054
if (!response.isSuccessful) {
5155
listener.onFailure(this, null, response)

okhttp-sse/src/main/kotlin/okhttp3/sse/internal/ServerSentEventReader.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import okio.BufferedSource
2222
import okio.ByteString.Companion.encodeUtf8
2323
import okio.Options
2424

25-
internal class ServerSentEventReader(
25+
class ServerSentEventReader(
2626
private val source: BufferedSource,
2727
private val callback: Callback,
2828
) {
@@ -119,7 +119,7 @@ internal class ServerSentEventReader(
119119
}
120120

121121
companion object {
122-
private val options =
122+
val options =
123123
Options.of(
124124
// 0
125125
"\r\n".encodeUtf8(),

okhttp-sse/src/test/java/okhttp3/sse/internal/EventSourceFactoryTest.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

okhttp-sse/src/test/java/okhttp3/sse/internal/EventSourceHttpTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import okhttp3.OkHttpClientTestRule
2727
import okhttp3.RecordingEventListener
2828
import okhttp3.Request
2929
import okhttp3.sse.EventSource
30-
import okhttp3.sse.EventSource.Factory.Companion.asEventSourceFactory
30+
import okhttp3.sse.EventSources.createFactory
3131
import okhttp3.testing.PlatformRule
3232
import org.junit.jupiter.api.AfterEach
3333
import org.junit.jupiter.api.Tag
@@ -319,7 +319,7 @@ class EventSourceHttpTest {
319319
builder.header("Accept", accept)
320320
}
321321
val request = builder.build()
322-
val factory = client.asEventSourceFactory()
322+
val factory = createFactory(client)
323323
return factory.newEventSource(request, listener)
324324
}
325325
}

okhttp-sse/src/test/java/okhttp3/sse/internal/EventSourcesHttpTest.kt

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import mockwebserver3.junit5.StartStop
2323
import okhttp3.Headers
2424
import okhttp3.OkHttpClientTestRule
2525
import okhttp3.Request
26-
import okhttp3.sse.EventSource.Companion.processEventSource
26+
import okhttp3.sse.EventSources.processResponse
2727
import okhttp3.testing.PlatformRule
2828
import org.junit.jupiter.api.AfterEach
2929
import org.junit.jupiter.api.Tag
@@ -65,7 +65,7 @@ class EventSourcesHttpTest {
6565
)
6666
val request = Request.Builder().url(server.url("/")).build()
6767
val response = client.newCall(request).execute()
68-
response.processEventSource(listener)
68+
processResponse(response, listener)
6969
listener.assertOpen()
7070
listener.assertEvent(null, null, "hey")
7171
listener.assertClose()
@@ -88,7 +88,7 @@ class EventSourcesHttpTest {
8888
listener.enqueueCancel() // Will cancel in onOpen().
8989
val request = Request.Builder().url(server.url("/")).build()
9090
val response = client.newCall(request).execute()
91-
response.processEventSource(listener)
91+
processResponse(response, listener)
9292
listener.assertOpen()
9393
listener.assertFailure("canceled")
9494
}
@@ -113,19 +113,18 @@ class EventSourcesHttpTest {
113113
headers = Headers.headersOf("content-type", "text/event-stream"),
114114
),
115115
)
116-
var request = Request.Builder().url(server.url("/")).build()
117-
repeat(2) {
118-
val response = client.newCall(request).execute()
119116

120-
if (response.code == 401) {
121-
assertThat(response.body.string()).isEqualTo("{\"error\":{\"message\":\"No auth credentials found\",\"code\":401}}")
122-
request = request.newBuilder().header("Authorization", "XYZ").build()
123-
} else {
124-
response.processEventSource(listener)
125-
listener.assertOpen()
126-
listener.assertEvent(null, null, "hey")
127-
listener.assertClose()
128-
}
129-
}
117+
val request1 = Request.Builder().url(server.url("/")).build()
118+
val response1 = client.newCall(request1).execute()
119+
assertThat(response1.code).isEqualTo(401)
120+
assertThat(response1.body.string())
121+
.isEqualTo("{\"error\":{\"message\":\"No auth credentials found\",\"code\":401}}")
122+
123+
val request2 = request1.newBuilder().header("Authorization", "XYZ").build()
124+
val response2 = client.newCall(request2).execute()
125+
processResponse(response2, listener)
126+
listener.assertOpen()
127+
listener.assertEvent(null, null, "hey")
128+
listener.assertClose()
130129
}
131130
}

0 commit comments

Comments
 (0)