-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Right now there is no way to provide any configuration by the users. You can now configure the level of logging in the library.
- Loading branch information
1 parent
814264b
commit e1aa76e
Showing
12 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/ConfigProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.rubenquadros.kovibes.api | ||
|
||
import io.github.rubenquadros.kovibes.api.config.Config | ||
import io.github.rubenquadros.kovibes.api.config.logger.LogLevel | ||
|
||
/** | ||
* ConfigProvider is responsible for providing the different [Config]. | ||
*/ | ||
internal class ConfigProvider { | ||
|
||
private var config: Config? = null | ||
|
||
/** | ||
* Init the config provider. | ||
* | ||
* @param config | ||
*/ | ||
fun init(config: Config) { | ||
this.config = config | ||
} | ||
|
||
/** | ||
* Return the log level for the API. | ||
* | ||
* @return [LogLevel] | ||
*/ | ||
fun getLogLevel(): LogLevel { | ||
if (config == null) error("Method init has not been called.") | ||
return config!!.logLevel | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/config/Config.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.rubenquadros.kovibes.api.config | ||
|
||
import io.github.rubenquadros.kovibes.api.config.logger.LogLevel | ||
|
||
/** | ||
* This represents all the configurations of the [io.github.rubenquadros.kovibes.api.KoVibesApi]. | ||
* | ||
* If no configurations are provided explicitly then the default values are taken. | ||
*/ | ||
data class Config( | ||
val logLevel: LogLevel = LogLevel.NONE | ||
) |
46 changes: 46 additions & 0 deletions
46
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/config/logger/Logger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.github.rubenquadros.kovibes.api.config.logger | ||
|
||
/** | ||
* Log level decides the logging level of the entire API. | ||
*/ | ||
enum class LogLevel { | ||
/** | ||
* Everything is logged - [HEADERS], [BODY] and [INFO] | ||
*/ | ||
ALL, | ||
|
||
/** | ||
* Only headers are logged. | ||
*/ | ||
HEADERS, | ||
|
||
/** | ||
* Only body is logged. | ||
*/ | ||
BODY, | ||
|
||
/** | ||
* Only info logs are provided - request type and response status. | ||
*/ | ||
INFO, | ||
|
||
/** | ||
* No logs are provided. | ||
* This is the default log level. | ||
*/ | ||
NONE | ||
} | ||
|
||
/** | ||
* @suppress | ||
* Map [LogLevel] to [io.ktor.client.plugins.logging.LogLevel] | ||
*/ | ||
internal fun LogLevel.toKtorLogLevel(): io.ktor.client.plugins.logging.LogLevel { | ||
return when(this) { | ||
LogLevel.ALL -> io.ktor.client.plugins.logging.LogLevel.ALL | ||
LogLevel.HEADERS -> io.ktor.client.plugins.logging.LogLevel.HEADERS | ||
LogLevel.BODY -> io.ktor.client.plugins.logging.LogLevel.BODY | ||
LogLevel.INFO -> io.ktor.client.plugins.logging.LogLevel.INFO | ||
LogLevel.NONE -> io.ktor.client.plugins.logging.LogLevel.NONE | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../commonTest/kotlin/io/github/rubenquadros/kovibes/api/test/authstorage/AuthStorageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.rubenquadros.kovibes.api.test.authstorage | ||
|
||
import io.github.rubenquadros.kovibes.api.AuthStorage | ||
import io.github.rubenquadros.kovibes.api.test.authToken | ||
import io.github.rubenquadros.kovibes.api.test.clientId | ||
import io.github.rubenquadros.kovibes.api.test.clientSecret | ||
import io.github.rubenquadros.kovibes.api.test.encodedCred | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class AuthStorageTest { | ||
|
||
private val authStorage = AuthStorage() | ||
|
||
@Test | ||
fun `initially the auth token is not available`() { | ||
assertTrue { authStorage.getAccessToken().isEmpty() } | ||
} | ||
|
||
@Test | ||
fun `when auth storage is initialised then then encoded credentials is available`() { | ||
authStorage.init(clientId, clientSecret) | ||
|
||
assertTrue { authStorage.getEncodedCredentials() == encodedCred } | ||
} | ||
|
||
@Test | ||
fun `when token is updated then it is stored`() { | ||
assertTrue { authStorage.getAccessToken().isEmpty() } | ||
|
||
authStorage.updateAccessToken(authToken) | ||
|
||
assertTrue { authStorage.getAccessToken() == authToken } | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rc/commonTest/kotlin/io/github/rubenquadros/kovibes/api/test/config/ConfigProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.rubenquadros.kovibes.api.test.config | ||
|
||
import io.github.rubenquadros.kovibes.api.ConfigProvider | ||
import io.github.rubenquadros.kovibes.api.config.Config | ||
import io.github.rubenquadros.kovibes.api.config.logger.LogLevel | ||
import kotlin.test.Test | ||
import kotlin.test.assertFails | ||
import kotlin.test.assertTrue | ||
|
||
class ConfigProviderTest { | ||
|
||
private val configProvider = ConfigProvider() | ||
|
||
@Test | ||
fun `retrieving the config before initializing the provider throws error`() { | ||
val error = assertFails { configProvider.getLogLevel() } | ||
|
||
assertTrue { error is IllegalStateException } | ||
assertTrue { error.message == "Method init has not been called." } | ||
} | ||
|
||
@Test | ||
fun `we are able to retrieve the configurations`() { | ||
LogLevel.entries.forEach { | ||
configProvider.init(config = Config(it)) | ||
val logLevel = configProvider.getLogLevel() | ||
|
||
assertTrue { logLevel == it } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters