Skip to content

Commit

Permalink
Utilise arguments over environment variables (#498)
Browse files Browse the repository at this point in the history
* Utilise arguments over environment variables

* Remove res values from later pull request
  • Loading branch information
ashdavies authored Aug 26, 2023
1 parent 8d5e191 commit 5850462
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 54 deletions.
10 changes: 3 additions & 7 deletions app-launcher/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ plugins {
}

android {
namespace = "io.ashdavies.playground"

buildFeatures {
buildConfig = true
}

defaultConfig {
buildConfigString("PLAYGROUND_API_KEY") { stringPropertyOrNull("playground.api.key") }
resValue("playground_api_key") { stringPropertyOrNull("playground.api.key") }

versionName = "1.0"
versionCode = 1
}

namespace = "io.ashdavies.playground"

val main by sourceSets.getting {
// Overwrite manifest.srcFile io.ashdavies.android.gradle.kts:56
manifest.srcFile("src/main/AndroidManifest.xml")
Expand Down
8 changes: 4 additions & 4 deletions build-plugins/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import org.gradle.kotlin.dsl.getting
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet

public fun VariantDimension.buildConfigString(key: String, block: () -> String?) {
buildConfigField("String", key, "\"${block()}\"")
}

public fun NamedDomainObjectCollection<KotlinSourceSet>.dependencies(
configure: KotlinDependencyHandler.() -> Unit,
) = getting { dependencies(configure) }
Expand All @@ -35,6 +31,10 @@ public fun <T : ModuleDependency> T.exclude(
dependency: MinimalExternalModuleDependency,
): T = exclude(dependency.group, dependency.name)

public fun VariantDimension.resValue(key: String, block: () -> String?) {
resValue("string", key, "${block()}")
}

public inline fun <reified T> ExtensionContainer.withType(
configure: T.() -> Unit,
) = findByType(T::class.java)?.configure()
9 changes: 0 additions & 9 deletions notion-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ plugins {

android {
namespace = "io.ashdavies.notion"

buildFeatures {
buildConfig = true
}

defaultConfig {
buildConfigString("NOTION_CLIENT_ID") { stringPropertyOrNull("notion.client.id") }
buildConfigString("NOTION_CLIENT_SECRET") { stringPropertyOrNull("notion.client.secret") }
}
}

kotlin {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ function closeWindow() {
""".trimIndent()

public fun getNotionHttpClient(
accessTokenFileString: String,
accessTokenFileString: () -> String,
notionClientId: () -> String,
notionClientSecret: () -> String,
openUri: (String) -> Unit,
): HttpClient = APPLICATION_HTTP_CLIENT.config {
install(Auth) {
bearer {
loadTokens {
val accessTokenFile = File(accessTokenFileString)
val accessTokenFile = File(accessTokenFileString())
if (accessTokenFile.exists()) {
return@loadTokens BearerTokens(
accessToken = accessTokenFile.readText(),
Expand All @@ -88,7 +90,7 @@ public fun getNotionHttpClient(

val authorizationUrlQuery = parameters {
append("redirect_uri", "http://localhost:8080/callback")
append("client_id", NotionClientId)
append("client_id", notionClientId())
append("response_type", "code")
append("owner", "user")
}.formUrlEncode()
Expand All @@ -103,8 +105,8 @@ public fun getNotionHttpClient(
val tokenUrlString = "https://api.notion.com/v1/oauth/token"
val tokenResponse = APPLICATION_HTTP_CLIENT.post(tokenUrlString) {
basicAuth(
username = NotionClientId,
password = NotionClientSecret,
username = notionClientId(),
password = notionClientSecret(),
)

headers {
Expand Down Expand Up @@ -154,10 +156,19 @@ public fun getNotionHttpClient(
}

@Deprecated("Do not call this method directly")
public suspend fun getAccessToken(openUri: (String) -> Unit): String {
val notionHttpClient = getNotionHttpClient("tokens.db", openUri)
val response = notionHttpClient.get("users/me")
public suspend fun getAccessToken(
notionClientId: () -> String,
notionClientSecret: () -> String,
openUri: (String) -> Unit,
): String {
val notionHttpClient = getNotionHttpClient(
accessTokenFileString = { "tokens.db" },
notionClientId = notionClientId,
notionClientSecret = notionClientSecret,
openUri = openUri,
)

val response = notionHttpClient.get("users/me")
if (response.status != HttpStatusCode.OK) {
val error = response.body<Notion.Object.Error>()
throw RuntimeException(error.message)
Expand Down

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions notion-console/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ kotlin {
implementation(composeCli)
implementation(localStorage)
implementation(notionClient)
implementation(platformSupport)
}

implementation(libs.jraf.klibnotion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.cli.ExperimentalCli
@ExperimentalCli
@NotionScopeMarker
internal fun ArgParser.AuthCommand(
authCredentials: AuthCredentials = LocalAuthCredentials.current,
tokenQueries: TokenQueries = rememberTokenQueries(),
uriHandler: UriHandler = LocalUriHandler.current,
onAuthState: (AuthState) -> Unit = { },
Expand All @@ -26,7 +27,13 @@ internal fun ArgParser.AuthCommand(
onAuthState(AuthState.Authenticated)
} else {
onAuthState(AuthState.Awaiting)
getAccessToken(uriHandler::openUri)

getAccessToken(
notionClientId = { authCredentials.clientId },
notionClientSecret = { authCredentials.clientSecret },
openUri = uriHandler::openUri,
)

onAuthState(AuthState.Authenticated)
}
},
Expand All @@ -40,6 +47,11 @@ internal fun ArgParser.AuthCommand(
)
}

internal data class AuthCredentials(
val clientId: String,
val clientSecret: String,
)

internal sealed interface AuthState : NotionState {
data object Awaiting : AuthState
data object Authenticated : AuthState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import org.jraf.klibnotion.client.Authentication
import org.jraf.klibnotion.client.ClientConfiguration
import org.jraf.klibnotion.client.NotionClient

internal val LocalAuthCredentials = compositionLocalOf<AuthCredentials> {
error("CompositionLocal LocalAuthCredentials not present")
}

internal val LocalNotionClient = compositionLocalOf<NotionClient> {
error("CompositionLocal LocalNotionClient not present")
}
Expand All @@ -28,13 +32,18 @@ internal fun ProvidePlaygroundDatabase(content: @Composable () -> Unit) {
}

@Composable
internal fun ProvideNotionClient(authentication: Authentication, content: @Composable () -> Unit) {
internal fun NotionCompositionLocals(
authentication: Authentication,
credentials: AuthCredentials,
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalNotionClient provides NotionClient.newInstance(
configuration = ClientConfiguration(
authentication = authentication,
),
),
LocalAuthCredentials provides credentials,
content = content,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
package io.ashdavies.notion

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import kotlinx.cli.ArgType
import kotlinx.cli.ExperimentalCli
import kotlinx.cli.required
import org.jraf.klibnotion.client.Authentication

internal sealed interface NotionState {
Expand All @@ -17,11 +18,17 @@ internal sealed interface NotionState {
@Composable
@OptIn(ExperimentalCli::class)
internal fun NotionCli(args: Array<String>, onState: (NotionState) -> Unit) {
ProvidePlaygroundDatabase {
val authentication by produceAuthenticationState()
ComposeCli("notion", args) {
ProvidePlaygroundDatabase {
val clientSecret by option(ArgType.String, "client_secret").required()
val clientId by option(ArgType.String, "client_id").required()
val authentication by produceAuthenticationState()
val credentials = AuthCredentials(
clientSecret = clientSecret,
clientId = clientId,
)

ProvideNotionClient(authentication) {
ComposeCli("notion", args) {
NotionCompositionLocals(authentication, credentials) {
SearchCommand { onState(it) }
AuthCommand { onState(it) }
}
Expand Down

0 comments on commit 5850462

Please sign in to comment.