Skip to content

Commit 880b91d

Browse files
authored
feat(data): Complete events class (#3016)
1 parent 3ca0692 commit 880b91d

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

appsync/aws-appsync-events/src/main/java/com/amplifyframework/aws/appsync/events/Events.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Events @VisibleForTesting internal constructor(
5656
}
5757
private val endpoints = EventsEndpoints(endpoint)
5858
private val httpClient = RestClient(endpoints.restEndpoint, okHttpClient, json)
59+
private val eventsWebSocket = EventsWebSocket(endpoints, connectAuthorizer, okHttpClient, json)
5960

6061
/**
6162
* Publish a single event to a channel.
@@ -98,13 +99,10 @@ class Events @VisibleForTesting internal constructor(
9899
* @param authorizers for the channel to use for subscriptions and publishes.
99100
* @return a channel to manage subscriptions and publishes.
100101
*/
101-
@Throws(EventsException::class)
102102
fun channel(
103103
channelName: String,
104104
authorizers: ChannelAuthorizers = this.defaultChannelAuthorizers,
105-
): EventsChannel {
106-
TODO("Need to implement")
107-
}
105+
) = EventsChannel(channelName, authorizers, endpoints, eventsWebSocket)
108106

109107
/**
110108
* Method to disconnect from all channels.
@@ -114,8 +112,7 @@ class Events @VisibleForTesting internal constructor(
114112
* @param authorizers for the channel to use for subscriptions and publishes.
115113
* @return a channel to manage subscriptions and publishes.
116114
*/
117-
@Throws(EventsException::class)
118115
suspend fun disconnect(flushEvents: Boolean = true) {
119-
TODO("Need to implement")
116+
eventsWebSocket.disconnect(flushEvents)
120117
}
121118
}

appsync/aws-appsync-events/src/main/java/com/amplifyframework/aws/appsync/events/EventsChannel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import kotlinx.serialization.json.JsonElement
3030
*/
3131
class EventsChannel internal constructor(
3232
val name: String,
33-
val authorizers: ChannelAuthorizers
33+
val authorizers: ChannelAuthorizers,
34+
private val endpoints: EventsEndpoints,
35+
private val eventsWebSocket: EventsWebSocket
3436
) {
3537

3638
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.aws.appsync.events
17+
18+
import com.amplifyframework.aws.appsync.core.AppSyncAuthorizer
19+
import kotlinx.serialization.json.Json
20+
import okhttp3.OkHttpClient
21+
import okhttp3.WebSocketListener
22+
23+
internal class EventsWebSocket(
24+
private val eventsEndpoints: EventsEndpoints,
25+
private val authorizer: AppSyncAuthorizer,
26+
private val okHttpClient: OkHttpClient,
27+
private val json: Json
28+
) : WebSocketListener() {
29+
30+
suspend fun disconnect(flushEvents: Boolean = true) {
31+
TODO("Not yet implemented")
32+
}
33+
}

appsync/aws-appsync-events/src/test/java/com/amplifyframework/aws/appsync/events/EventsTest.kt

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ class EventsTest {
4747
HeaderKeys.CONTENT_TYPE to HeaderValues.CONTENT_TYPE_APPLICATION_JSON,
4848
HeaderKeys.ACCEPT to HeaderValues.ACCEPT_APPLICATION_JSON,
4949
)
50+
private val expectedChannelAuthorizers = ChannelAuthorizers(
51+
subscribeAuthorizer = ApiKeyAuthorizer("123"),
52+
publishAuthorizer = TestAuthorizer()
53+
)
5054
private val interceptor = ConvertToMockRequestInterceptor(mockWebServer.url("/event"))
5155
private val events = Events(
5256
endpoint = expectedEndpoint,
5357
connectAuthorizer = ApiKeyAuthorizer("abc"),
54-
defaultChannelAuthorizers = ChannelAuthorizers(
55-
subscribeAuthorizer = ApiKeyAuthorizer("123"),
56-
publishAuthorizer = TestAuthorizer()
57-
),
58+
defaultChannelAuthorizers = expectedChannelAuthorizers,
5859
okHttpClient = OkHttpClient.Builder()
5960
.addInterceptor(interceptor)
6061
.build()
@@ -304,6 +305,46 @@ class EventsTest {
304305
successfulEvents.size shouldBe 1
305306
}
306307
}
308+
309+
@Test
310+
fun `test channel creation with default authorizers`() = runTest {
311+
// GIVEN
312+
val expectedChannel = "default/testChannel"
313+
314+
// WHEN
315+
val channel = events.channel(expectedChannel)
316+
317+
// THEN
318+
channel.name shouldBe expectedChannel
319+
channel.authorizers shouldBe expectedChannelAuthorizers
320+
}
321+
322+
@Test
323+
fun `test channel creation with override authorizers`() = runTest {
324+
// GIVEN
325+
val expectedChannel = "default/testChannel"
326+
val overrideChannelAuthorizers = ChannelAuthorizers(
327+
subscribeAuthorizer = ApiKeyAuthorizer("override"),
328+
publishAuthorizer = TestAuthorizer("override")
329+
)
330+
331+
// WHEN
332+
val channel = events.channel(expectedChannel, overrideChannelAuthorizers)
333+
334+
// THEN
335+
channel.name shouldBe expectedChannel
336+
channel.authorizers shouldBe overrideChannelAuthorizers
337+
}
338+
339+
@Test
340+
fun `test disconnect with flushEvents`() {
341+
// TODO: Write test once disconnect implemented within websocket
342+
}
343+
344+
@Test
345+
fun `test disconnect without flushEvents`() {
346+
// TODO: Write test once disconnect implemented within websocket
347+
}
307348
}
308349

309350
private class ConvertToMockRequestInterceptor(private val mockUrl: HttpUrl) : Interceptor {

0 commit comments

Comments
 (0)