Skip to content

Commit 9a7bdba

Browse files
committed
task: handle different base URLs dependent on router
1 parent a48712d commit 9a7bdba

File tree

6 files changed

+91
-9
lines changed

6 files changed

+91
-9
lines changed

src/main/kotlin/com/ctrlhub/core/Api.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package com.ctrlhub.core
33
import com.ctrlhub.core.api.KtorApiClient
44
import com.ctrlhub.core.router.AuthRouter
55

6-
class Api(val apiClient: KtorApiClient) {
6+
class Api private constructor(apiClient: KtorApiClient) {
77
val auth: AuthRouter = AuthRouter(apiClient)
8+
9+
companion object {
10+
fun create(): Api {
11+
return Api(KtorApiClient.create(Config.apiBaseUrl))
12+
}
13+
}
814
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ctrlhub.core
2+
3+
enum class Environment {
4+
STAGING,
5+
PRODUCTION
6+
}
7+
8+
object Config {
9+
var environment: Environment = Environment.PRODUCTION
10+
11+
val authBaseUrl: String
12+
get() = when (environment) {
13+
Environment.STAGING -> "auth.ctrl-hub.dev"
14+
Environment.PRODUCTION -> "auth.ctrl-hub.com"
15+
}
16+
17+
val apiBaseUrl: String
18+
get() = when (environment) {
19+
Environment.STAGING -> "api.ctrl-hub.dev"
20+
Environment.PRODUCTION -> "api.ctrl-hub.com"
21+
}
22+
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package com.ctrlhub.core.api
22

3-
// TODO - flesh this out more with different types of exceptions
4-
class ApiException(message: String, e: Throwable): Exception(message, e)
3+
import io.ktor.client.statement.HttpResponse
4+
5+
open class ApiException(message: String, e: Throwable) : Exception(message, e)
6+
7+
class ApiClientException(message: String, val response: HttpResponse, e: Throwable) : ApiException(message, e) {
8+
fun statusCode(): Int {
9+
return response.status.value
10+
}
11+
}

src/main/kotlin/com/ctrlhub/core/router/AuthRouter.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
package com.ctrlhub.core.router
22

3+
import com.ctrlhub.core.Config
4+
import com.ctrlhub.core.api.ApiClientException
35
import com.ctrlhub.core.api.ApiException
46
import com.ctrlhub.core.api.KtorApiClient
57
import com.ctrlhub.core.api.payload.auth.LoginPayload
68
import com.ctrlhub.core.api.response.auth.AuthFlowResponse
79
import com.ctrlhub.core.api.response.auth.CompleteResponse
810
import io.ktor.client.call.*
11+
import io.ktor.client.plugins.ClientRequestException
912

10-
class AuthRouter(apiClient: KtorApiClient) : Router(apiClient) {
13+
class AuthRouter(apiClient: KtorApiClient) : Router(apiClient = apiClient) {
1114
suspend fun initiate(): AuthFlowResponse {
1215
return try {
13-
return apiClient.get(url = "self-service/login/api").body()
16+
return apiClient.get(url = "${Config.authBaseUrl}/self-service/login/api").body()
17+
} catch (e: ClientRequestException) {
18+
throw ApiClientException("Failed to initiate auth", e.response, e)
1419
} catch (e: Exception) {
1520
throw ApiException("Failed to initiate auth", e)
1621
}
1722
}
1823

1924
suspend fun complete(flowId: String, payload: LoginPayload): CompleteResponse {
2025
return try {
21-
apiClient.post(url = "self-service/login?flow=$flowId", body = payload).body()
26+
apiClient.post(url = "${Config.authBaseUrl}/self-service/login?flow=$flowId", body = payload).body()
27+
} catch (e: ClientRequestException) {
28+
throw ApiClientException("Failed to complete auth", e.response, e)
2229
} catch (e: Exception) {
2330
throw ApiException("Failed to complete auth", e)
2431
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.ctrlhub.core.router
22

3+
import com.ctrlhub.core.Config
34
import com.ctrlhub.core.api.KtorApiClient
45

5-
abstract class Router(
6-
protected val apiClient: KtorApiClient
7-
)
6+
abstract class Router(val apiClient: KtorApiClient)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.ctrlhub.core
2+
3+
import kotlin.test.AfterTest
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class ConfigTest {
8+
@AfterTest
9+
fun tearDown() {
10+
// Reset the environment to its default after each test
11+
Config.environment = Environment.PRODUCTION
12+
}
13+
14+
@Test
15+
fun `authBaseUrl should return correct value for PRODUCTION`() {
16+
Config.environment = Environment.PRODUCTION
17+
18+
assertEquals("auth.ctrl-hub.com", Config.authBaseUrl)
19+
}
20+
21+
@Test
22+
fun `authBaseUrl should return correct value for STAGING`() {
23+
Config.environment = Environment.STAGING
24+
25+
assertEquals("auth.ctrl-hub.dev", Config.authBaseUrl)
26+
}
27+
28+
@Test
29+
fun `apiBaseUrl should return correct value for PRODUCTION`() {
30+
Config.environment = Environment.PRODUCTION
31+
32+
assertEquals("api.ctrl-hub.com", Config.apiBaseUrl)
33+
}
34+
35+
@Test
36+
fun `apiBaseUrl should return correct value for STAGING`() {
37+
Config.environment = Environment.STAGING
38+
39+
assertEquals("api.ctrl-hub.dev", Config.apiBaseUrl)
40+
}
41+
}

0 commit comments

Comments
 (0)