Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {

buildTypes {
debug {
buildConfigField("String", "SOCKET_URI", "\"ws://10.0.2.2\"")
buildConfigField("String", "SOCKET_URI", "\"ws://10.0.2.2:3000\"")
buildConfigField("String", "SPOTIFY_CLIENT_ID", "\"0368b2bddb504887b517fc4e8fca9cc5\"")
buildConfigField("String", "SPOTIFY_REDIRECT_URI", "\"feelbeat://callback\"")
buildConfigField("String", "SPOTIFY_SCOPE", "\"user-read-private user-read-email\"")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.feelbeatapp.androidclient.api.feelbeat

import com.github.feelbeatapp.androidclient.game.model.RoomSettings
import com.github.feelbeatapp.androidclient.api.feelbeat.responses.RoomListViewResponse

interface FeelBeatApi {
suspend fun createRoom(payload: RoomSettings): String

suspend fun fetchRooms(): List<RoomListViewResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.github.feelbeatapp.androidclient.api.feelbeat

import com.github.feelbeatapp.androidclient.api.feelbeat.responses.CreateRoomResponse
import com.github.feelbeatapp.androidclient.api.feelbeat.responses.FetchRoomsResponse
import com.github.feelbeatapp.androidclient.api.feelbeat.responses.RoomListViewResponse
import com.github.feelbeatapp.androidclient.game.model.RoomSettings
import com.github.feelbeatapp.androidclient.infra.auth.AuthManager
import com.github.feelbeatapp.androidclient.infra.error.ErrorCode
import com.github.feelbeatapp.androidclient.infra.error.FeelBeatException
import com.github.feelbeatapp.androidclient.infra.error.FeelBeatServerException
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import io.ktor.util.network.UnresolvedAddressException
import java.io.IOException
import javax.inject.Inject
import javax.inject.Named
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class KtorFeelBeatApi
@Inject
constructor(
@Named("API_URL") private val baseUrl: String,
private val httpClient: HttpClient,
private val authManager: AuthManager,
) : FeelBeatApi {
override suspend fun createRoom(payload: RoomSettings): String =
withContext(Dispatchers.IO) {
val token = authManager.getAccessToken()

val res =
try {
httpClient.post("$baseUrl/create") {
headers { set("Authorization", "Bearer $token") }
contentType(ContentType.Application.Json)
setBody(payload)
}
} catch (e: Exception) {
when (e) {
is IOException,
is UnresolvedAddressException ->
throw FeelBeatException(ErrorCode.FEELBEAT_SERVER_UNREACHABLE, e)
else -> throw e
}
}

if (res.status != HttpStatusCode.OK) {
throw FeelBeatServerException(res.bodyAsText().trim())
}

val (roomId) =
try {
res.body<CreateRoomResponse>()
} catch (e: UnsupportedOperationException) {
throw FeelBeatException(
ErrorCode.FEELBEAT_SERVER_INCORRECT_RESPONSE_FORMAT,
"Failed to parse server response",
e,
)
}

roomId
}

override suspend fun fetchRooms(): List<RoomListViewResponse> =
withContext(Dispatchers.IO) {
val token = authManager.getAccessToken()

val res =
try {
httpClient.get("$baseUrl/rooms") {
headers { set("Authorization", "Bearer $token") }
}
} catch (e: Exception) {
when (e) {
is IOException,
is UnresolvedAddressException ->
throw FeelBeatException(ErrorCode.FEELBEAT_SERVER_UNREACHABLE, e)
else -> throw e
}
}

if (res.status != HttpStatusCode.OK) {
throw FeelBeatServerException(res.bodyAsText().trim())
}

val (rooms) =
try {
res.body<FetchRoomsResponse>()
} catch (e: UnsupportedOperationException) {
throw FeelBeatException(
ErrorCode.FEELBEAT_SERVER_INCORRECT_RESPONSE_FORMAT,
"Failed to parse server response",
e,
)
}

rooms
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.feelbeatapp.androidclient.network.api.responses
package com.github.feelbeatapp.androidclient.api.feelbeat.responses

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.feelbeatapp.androidclient.api.feelbeat.responses

import kotlinx.serialization.Serializable

@Serializable data class FetchRoomsResponse(val rooms: List<RoomListViewResponse>)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.feelbeatapp.androidclient.model
package com.github.feelbeatapp.androidclient.api.feelbeat.responses

import kotlinx.serialization.Serializable

@Serializable
data class RoomListView(
data class RoomListViewResponse(
val id: String,
val name: String,
val players: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.feelbeatapp.androidclient.api.spotify

import com.github.feelbeatapp.androidclient.api.spotify.responses.ProfileResponse
import com.github.feelbeatapp.androidclient.infra.auth.AuthManager
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.headers
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

const val SPOTIFY_API_URL = "https://api.spotify.com/v1"

class KtorSpotifyAPI
@Inject
constructor(private val httpClient: HttpClient, private val authManager: AuthManager) : SpotifyAPI {
override suspend fun getProfile(): ProfileResponse =
withContext(Dispatchers.IO) {
val accessToken = authManager.getAccessToken()

val response =
httpClient.get("$SPOTIFY_API_URL/me") {
headers { append("Authorization", "Bearer $accessToken") }
}

response.body<ProfileResponse>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.feelbeatapp.androidclient.api.spotify

import com.github.feelbeatapp.androidclient.api.spotify.responses.ProfileResponse

interface SpotifyAPI {
suspend fun getProfile(): ProfileResponse
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.feelbeatapp.androidclient.network.spotify.responses
package com.github.feelbeatapp.androidclient.api.spotify.responses

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down

This file was deleted.

Loading
Loading