From 4d2a60a0ba60331a4a6cc6880f4fc8e3fcfb2cb0 Mon Sep 17 00:00:00 2001 From: Bjarne Eberhardt Date: Mon, 6 May 2024 15:36:50 +1200 Subject: [PATCH] Add URL generator function --- src/main/kotlin/ee/bjarn/ktify/Ktify.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ee/bjarn/ktify/Ktify.kt b/src/main/kotlin/ee/bjarn/ktify/Ktify.kt index 25c9d38..c02d31f 100755 --- a/src/main/kotlin/ee/bjarn/ktify/Ktify.kt +++ b/src/main/kotlin/ee/bjarn/ktify/Ktify.kt @@ -21,6 +21,7 @@ import io.ktor.serialization.kotlinx.json.* import io.ktor.util.* import kotlinx.serialization.json.JsonObject import mu.KotlinLogging +import java.util.UUID /** * The main wrapper class @@ -84,24 +85,34 @@ class Ktify( } /** - * The builder for the [Ktify] class + * The builder for the [Ktify] class. Currently designed for single use. * @param clientId The client ID, provided by the spotify dashboard * @param clientSecret The client secret, provided by the spotify dashboard - * @param authorizationCode returned by the request to the user * @param redirectUri Your redirect URI (just for confirmation) */ class KtifyBuilder( private val clientId: String, private val clientSecret: String, - private val authorizationCode: String, private val redirectUri: String, ) { + private val state = UUID.randomUUID().toString().replace("-", "") /** + * @param scopes List of scopes required by the application + * @returns The Spotify Authorization URL + */ + fun getAuthorisationURL(scopes: List): String { + val baseUrl = "https://accounts.spotify.com/authorize" + val scopesString = if (scopes.size > 0) scopes.map { it.value + "%20" }.reduce { acc, s -> acc + s }.dropLast(3) else "none" + return "$baseUrl?client_id=&scope=$scopesString&redirect_uri=$redirectUri&state=$state&response_type=code" + } + + /** + * @param authorizationCode returned by the request to the user * @return The [Ktify] instance */ @OptIn(InternalAPI::class) - suspend fun build(): Ktify { + suspend fun build(authorizationCode: String): Ktify { val clientCredentialsResponse: ClientCredentialsResponse = ktifyHttpClient.post("https://accounts.spotify.com/api/token") { header("Content-Type", "application/x-www-form-urlencoded")