Skip to content

Commit 210dafd

Browse files
committed
Implement Stream video token service
1 parent 19a9a9c commit 210dafd

File tree

5 files changed

+147
-38
lines changed

5 files changed

+147
-38
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2023 Stream.IO, Inc. 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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.getstream.whatsappclone.model
18+
19+
import kotlinx.serialization.Serializable
20+
21+
@Serializable
22+
data class TokenResponse(val userId: String, val token: String)

core/network/src/main/kotlin/io/getstream/whatsappclone/network/di/NetworkModule.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import dagger.Module
2424
import dagger.Provides
2525
import dagger.hilt.InstallIn
2626
import dagger.hilt.components.SingletonComponent
27+
import io.getstream.whatsappclone.network.service.StreamVideoTokenService
2728
import io.getstream.whatsappclone.network.service.WhatsAppUserService
2829
import javax.inject.Singleton
2930
import kotlinx.serialization.ExperimentalSerializationApi
@@ -44,20 +45,39 @@ internal object NetworkModule {
4445

4546
@Provides
4647
@Singleton
47-
fun provideRetrofit(networkJson: Json): Retrofit {
48+
@RetrofitService(BaseService.WhatsApp)
49+
fun provideWhatsAppRetrofit(networkJson: Json): Retrofit {
4850
return Retrofit.Builder()
49-
.baseUrl(
50-
"https://gist.githubusercontent.com/skydoves/44140b10c3b1057b8ac00e2a59eaaa86/raw/" +
51-
"0ca2cdbb34c7eaf365130c75969a29d4e33bd2fc/"
52-
)
51+
.baseUrl(BaseService.WhatsApp.baseUrl)
5352
.addConverterFactory(networkJson.asConverterFactory("application/json".toMediaType()))
5453
.addCallAdapterFactory(ResultCallAdapterFactory.create())
5554
.build()
5655
}
5756

5857
@Provides
5958
@Singleton
60-
fun provideWhatsAppUerService(retrofit: Retrofit): WhatsAppUserService {
59+
fun provideWhatsAppUerService(
60+
@RetrofitService(BaseService.WhatsApp) retrofit: Retrofit
61+
): WhatsAppUserService {
62+
return retrofit.create()
63+
}
64+
65+
@Provides
66+
@Singleton
67+
@RetrofitService(BaseService.StreamVideo)
68+
fun provideStreamVideoRetrofit(networkJson: Json): Retrofit {
69+
return Retrofit.Builder()
70+
.baseUrl(BaseService.StreamVideo.baseUrl)
71+
.addConverterFactory(networkJson.asConverterFactory("application/json".toMediaType()))
72+
.addCallAdapterFactory(ResultCallAdapterFactory.create())
73+
.build()
74+
}
75+
76+
@Provides
77+
@Singleton
78+
fun provideStreamVideoTokenService(
79+
@RetrofitService(BaseService.StreamVideo) retrofit: Retrofit
80+
): StreamVideoTokenService {
6181
return retrofit.create()
6282
}
6383
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023 Stream.IO, Inc. 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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.getstream.whatsappclone.network.di
18+
19+
import javax.inject.Qualifier
20+
21+
@Qualifier
22+
@Retention(AnnotationRetention.RUNTIME)
23+
annotation class RetrofitService(val baseService: BaseService)
24+
25+
enum class BaseService(val baseUrl: String) {
26+
WhatsApp(
27+
baseUrl = "https://gist.githubusercontent.com/skydoves/44140b10c3b1057b8ac00e2a59eaaa86/raw/" +
28+
"0ca2cdbb34c7eaf365130c75969a29d4e33bd2fc/"
29+
),
30+
StreamVideo(
31+
baseUrl = "https://stream-calls-dogfood.vercel.app/"
32+
)
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2023 Stream.IO, Inc. 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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.getstream.whatsappclone.network.service
18+
19+
import io.getstream.whatsappclone.model.TokenResponse
20+
import retrofit2.http.GET
21+
import retrofit2.http.Query
22+
23+
interface StreamVideoTokenService {
24+
25+
@GET("api/auth/create-token")
26+
suspend fun fetchToken(
27+
@Query("user_id") userId: String?,
28+
@Query("api_key") apiKey: String
29+
): Result<TokenResponse>
30+
}

features/video/src/main/kotlin/io/getstream/whatsappclone/video/WhatsAppVideoCall.kt

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,54 @@
1616

1717
package io.getstream.whatsappclone.video
1818

19+
import android.widget.Toast
1920
import androidx.compose.runtime.Composable
21+
import androidx.compose.runtime.LaunchedEffect
2022
import androidx.compose.runtime.getValue
2123
import androidx.compose.runtime.mutableStateOf
2224
import androidx.compose.runtime.remember
2325
import androidx.compose.runtime.setValue
2426
import androidx.compose.ui.platform.LocalContext
2527
import io.getstream.video.android.core.Call
28+
import io.getstream.video.android.core.GEO
29+
import io.getstream.video.android.core.StreamVideoBuilder
30+
import io.getstream.video.android.model.User
2631

2732
@Composable
2833
fun WhatsAppVideoCall(id: String) {
2934
val context = LocalContext.current
3035

3136
var call: Call? by remember { mutableStateOf(null) }
3237

33-
// LaunchedEffect(key1 = Unit) {
34-
// val userToken =
35-
// "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiQmVuX1NreXdhbGtlciIsImlzcyI6InByb250byIsInN1YiI6InVzZXIvQmVuX1NreXdhbGtlciIsImlhdCI6MTY5Njk4NDE3MywiZXhwIjoxNjk3NTg4OTc4fQ.Cdq_sw1ZA_PiGNXmOIZdxZjmlBKK8DuW8Oy_YjKloZw"
36-
// val userId = "Ben_Skywalker"
37-
// val callId = "dE8AsD5Qxqrt"
38-
//
39-
// // step1 - create a user.
40-
// val user = User(
41-
// id = userId, // any string
42-
// name = "Tutorial", // name and image are used in the UI
43-
// role = "admin",
44-
// )
45-
//
46-
// // step2 - initialize StreamVideo. For a production app we recommend adding the client to your Application class or di module.
47-
// val client = StreamVideoBuilder(
48-
// context = context,
49-
// apiKey = stringResource(id = R.string.),
50-
// geo = GEO.GlobalEdgeNetwork,
51-
// user = user,
52-
// token = userToken,
53-
// ensureSingleInstance = false,
54-
// ).build()
55-
//
56-
// // step3 - join a call, which type is `default` and id is `123`.
57-
// call = client.call("livestream", callId)
58-
//
59-
// // join the cal
60-
// val result = call?.join()
61-
// result?.onError {
62-
// Toast.makeText(context, "uh oh $it", Toast.LENGTH_SHORT).show()
63-
// }
64-
// }
38+
LaunchedEffect(key1 = Unit) {
39+
val userToken = ""
40+
val userId = "Ben_Skywalker"
41+
val callId = "dE8AsD5Qxqrt"
42+
43+
// step1 - create a user.
44+
val user = User(
45+
id = userId, // any string
46+
name = "Tutorial", // name and image are used in the UI
47+
role = "admin"
48+
)
49+
50+
// step2 - initialize StreamVideo. For a production app we recommend adding the client to your Application class or di module.
51+
val client = StreamVideoBuilder(
52+
context = context,
53+
apiKey = BuildConfig.STREAM_API_KEY,
54+
geo = GEO.GlobalEdgeNetwork,
55+
user = user,
56+
token = userToken,
57+
ensureSingleInstance = false
58+
).build()
59+
60+
// step3 - join a call, which type is `default` and id is `123`.
61+
call = client.call("livestream", callId)
62+
63+
// join the cal
64+
val result = call?.join()
65+
result?.onError {
66+
Toast.makeText(context, "uh oh $it", Toast.LENGTH_SHORT).show()
67+
}
68+
}
6569
}

0 commit comments

Comments
 (0)