Skip to content

Commit 3967e66

Browse files
committed
task: use a class, not an object for Api
1 parent fa4c170 commit 3967e66

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ Config.environment = Environment.PRODUCTION // For production
4141
```
4242

4343
### API object
44-
Interaction with the APIs is done via the `Api` singleton. The Api singleton provides access to a configured Ktor client for interaction with the Api. A session token can also be applied:
44+
Interaction with the APIs is done via the `Api` class. The Api class provides access to a configured Ktor client for interaction with the Api. A session token can also be applied:
4545

4646
```kotlin
47-
Api.applySessionToken("valid_session_token")
47+
val api = Api()
48+
api.applySessionToken("valid_session_token")
4849
```
4950

5051
### Applying Ktor config
5152
Ktor configuration can be applied using the `Api.withHttpClientConfig` method:
5253

5354
```kotlin
54-
Api.withHttpClientConfig {
55+
api.withHttpClientConfig {
5556
install(Logging) {
5657
level = LogLevel.ALL
5758
}
@@ -78,20 +79,21 @@ val response: List<Vehicle> = Api.vehicles.all("organisation-id", VehicleRequest
7879
```kotlin
7980
runBlocking {
8081
Config.environment = Environment.STAGING
81-
val authResponse = Api.auth.initiate()
82-
Api.withHttpClientConfig {
82+
val api = Api()
83+
val authResponse = api.auth.initiate()
84+
api.withHttpClientConfig {
8385
install(Logging) {
8486
level = LogLevel.ALL
8587
}
8688
}
8789

88-
val response = Api.auth.complete(authResponse.id, payload = LoginPayload(
90+
val response = api.auth.complete(authResponse.id, payload = LoginPayload(
8991
identifier = "[email protected]",
9092
password = "Password1!",
9193
))
9294

93-
Api.applySessionToken(response.sessionToken)
94-
val vehicles = Api.vehicles.all("org-123")
95+
api.applySessionToken(response.sessionToken)
96+
val vehicles = api.vehicles.all("org-123")
9597
println(vehicles.size)
9698
}
9799
```

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import io.ktor.client.*
66
/**
77
* The facade object through which interaction with the API occurs.
88
*/
9-
object Api {
10-
private var sessionToken: String? = null
9+
class Api(
1110
var httpClient: HttpClient = KtorClientFactory.create()
12-
private set
11+
) {
12+
private var sessionToken: String? = null
1313

1414
fun withHttpClientConfig(config: HttpClientConfig<*>.() -> Unit) {
1515
httpClient = KtorClientFactory.create(configBlock = config)

0 commit comments

Comments
 (0)