Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.1 - Add tracks API #6

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ jobs:

steps:
- name: Fetch Sources
uses: actions/checkout@v2
uses: actions/checkout@v4.1.5

- name: Gradle Wrapper Validation
uses: gradle/wrapper-validation-action@v1
uses: gradle/actions/wrapper-validation@v3.3.2

build:
runs-on: ubuntu-latest
needs: gradleValidation
name: Build
steps:
- name: Fetch Sources
uses: actions/checkout@v2
uses: actions/checkout@v4.1.5

- name: Setup JDK
uses: actions/setup-java@v2
uses: actions/setup-java@v4.2.1
with:
distribution: 'adopt'
java-version: 11

# Cache Gradle dependencies
- name: Setup Gradle Dependencies Cache
uses: actions/cache@v2.1.6
uses: actions/cache@v4.0.2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle', '**/*.gradle.kts', 'gradle.properties') }}

# Cache Gradle Wrapper
- name: Setup Gradle Wrapper Cache
uses: actions/cache@v2.1.6
uses: actions/cache@v4.0.2
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
Expand All @@ -52,24 +52,24 @@ jobs:
needs: build
steps:
- name: Fetch Sources
uses: actions/checkout@v2
uses: actions/checkout@v4.1.5

- name: Setup JDK
uses: actions/setup-java@v2
uses: actions/setup-java@v4.2.1
with:
distribution: 'adopt'
java-version: 11

# Cache Gradle dependencies
- name: Setup Gradle Dependencies Cache
uses: actions/cache@v2.1.6
uses: actions/cache@v4.0.2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle', '**/*.gradle.kts', 'gradle.properties') }}

# Cache Gradle Wrapper
- name: Setup Gradle Wrapper Cache
uses: actions/cache@v2.1.6
uses: actions/cache@v4.0.2
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
Expand All @@ -83,24 +83,24 @@ jobs:
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' && !contains(github.event.commits[0].message, '[skip ci]')
steps:
- name: Fetch Sources
uses: actions/checkout@v2
uses: actions/checkout@v4.1.5

- name: Setup JDK
uses: actions/setup-java@v2
uses: actions/setup-java@v4.2.1
with:
distribution: 'adopt'
java-version: 11

# Cache Gradle dependencies
- name: Setup Gradle Dependencies Cache
uses: actions/cache@v2.1.6
uses: actions/cache@v4.0.2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle', '**/*.gradle.kts', 'gradle.properties') }}

# Cache Gradle Wrapper
- name: Setup Gradle Wrapper Cache
uses: actions/cache@v2.1.6
uses: actions/cache@v4.0.2
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
Expand All @@ -109,7 +109,7 @@ jobs:
run: ./gradlew dokkaHtml

- name: Deploy Docs to GitHub Pages
uses: JamesIves/github-pages-deploy-action@4.1.4
uses: JamesIves/github-pages-deploy-action@4.6.0
with:
branch: gh-pages
folder: build/dokka/html
folder: build/dokka/html
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation("ee.bjarn", "ktify", "0.1")
implementation("ee.bjarn", "ktify", "0.1.1")
}
```

Expand Down
53 changes: 50 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import com.vanniktech.maven.publish.SonatypeHost
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.9.23"
kotlin("plugin.serialization") version "1.9.23"
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
id("org.jetbrains.dokka") version "1.9.20"
id("com.vanniktech.maven.publish") version "0.28.0"
}

group = "ee.bjarn"
version = "0.1"
version = "0.1.1"

repositories {
mavenCentral()
Expand Down Expand Up @@ -57,6 +61,51 @@ tasks {
}
}

mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
signAllPublications()

coordinates(project.group.toString(), project.name, project.version.toString())

configure(KotlinJvm(
javadocJar = JavadocJar.Dokka("dokkaHtml"),
sourcesJar = true
))

pom {
name.set(project.name)
description.set("A coroutine based wrapper around the Spotify Web API, written in Kotlin.")
url.set("https://github.com/warriorzz/ktify")
inceptionYear.set("2021")

licenses {
license {
name.set("MIT License")
url.set("https://github.com/warriorzz/ktify/blob/main/LICENSE")
}
}

issueManagement {
system.set("GitHub")
url.set("https://github.com/warriorzz/ktify/issues")
}

scm {
connection.set("https://github.com/warriorzz/ktify.git")
url.set("https://github.com/warriorzz/ktify")
}

developers {
developer {
name.set("Bjarne Eberhardt")
email.set("[email protected]")
url.set("https://bjarn.ee")
timezone.set("Europe/Berlin")
}
}
}
}

ktlint {
verbose.set(true)
filter {
Expand All @@ -72,5 +121,3 @@ java {
// This avoids a Gradle warning
sourceCompatibility = JavaVersion.VERSION_11
}

apply(from = "publishing.gradle.kts")
96 changes: 0 additions & 96 deletions publishing.gradle.kts

This file was deleted.

19 changes: 15 additions & 4 deletions src/main/kotlin/ee/bjarn/ktify/Ktify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Scope>): 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")
Expand Down
11 changes: 0 additions & 11 deletions src/main/kotlin/ee/bjarn/ktify/model/Episode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ data class Episode(
val uri: String,
) : KtifyObject()

@Serializable
data class EpisodePagingObject(
val href: String,
val items: List<Episode>,
val limit: Int,
val next: String? = null,
val offset: Int,
val previous: String? = null,
val total: Int,
)

@Serializable
data class SavedEpisodeObject(
@SerialName("added_at")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ee/bjarn/ktify/model/KtifyObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class RawKtifyObject(
) : KtifyObject()

object KtifyObjectSerializer : JsonContentPolymorphicSerializer<KtifyObject>(KtifyObject::class) {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out KtifyObject> {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<KtifyObject> {
return when (element.jsonObject["item"]?.jsonObject?.get("type")?.jsonPrimitive?.content) {
"track" -> Track.serializer()
"album" -> Album.serializer()
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/ee/bjarn/ktify/model/PaginationObject.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ee.bjarn.ktify.model

import kotlinx.serialization.Serializable

@Serializable
data class PaginationObject<T>(
val href: String,
val limit: Int,
val offset: Int,
val total: Int,
val items: List<T>,
val previous: String? = null,
val next: String? = null,
)
2 changes: 1 addition & 1 deletion src/main/kotlin/ee/bjarn/ktify/model/Playlist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ sealed class PlaylistTrackObject {
}

object PlaylistTrackObjectSerializer : JsonContentPolymorphicSerializer<PlaylistTrackObject>(PlaylistTrackObject::class) {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out PlaylistTrackObject> {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<PlaylistTrackObject> {
return if (element.jsonObject["track"] != null) PlaylistTrack.serializer() else PlaylistTrackRef.serializer()
}
}
Loading