Skip to content

Commit ec4671d

Browse files
committed
put searching shows and episodes in ClientSearchApi
1 parent 310d206 commit ec4671d

File tree

11 files changed

+190
-193
lines changed

11 files changed

+190
-193
lines changed

build.gradle.kts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import com.moowork.gradle.node.task.NodeTask
12
import org.jetbrains.dokka.gradle.DokkaTask
2-
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
3+
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackOutput.Target
34
import java.net.URI
5+
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
46

57
plugins {
68
`maven-publish`
@@ -36,7 +38,19 @@ repositories {
3638

3739
kotlin {
3840
jvm()
39-
js()
41+
js {
42+
browser {
43+
dceTask {
44+
keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
45+
}
46+
47+
webpackTask {
48+
output.libraryTarget = Target.UMD
49+
}
50+
}
51+
52+
nodejs()
53+
}
4054

4155
targets {
4256
sourceSets {
@@ -89,9 +103,13 @@ kotlin {
89103

90104
val jsMain by getting {
91105
dependencies {
106+
implementation(npm("text-encoding", "0.7.0"))
92107
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutineVersion")
93108
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion")
94109
implementation("io.ktor:ktor-client-js:$ktorVersion")
110+
implementation(npm("abort-controller", "3.0.0"))
111+
implementation(npm("node-fetch", "2.6.0"))
112+
95113
compileOnly(kotlin("stdlib-js"))
96114
}
97115
}
@@ -284,22 +302,10 @@ tasks {
284302
}
285303

286304

287-
getByName<KotlinJsCompile>("compileKotlinJs") {
288-
kotlinOptions {
289-
moduleKind = "umd"
290-
noStdlib = false
291-
metaInfo = true
292-
}
293-
}
294-
295305
getByName<Test>("jvmTest") {
296306
useJUnitPlatform()
297307
}
298308

299-
withType<GenerateModuleMetadata> {
300-
enabled = false
301-
}
302-
303309

304310
val publishJvm by registering(Task::class) {
305311
dependsOn.add(check)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Fri May 08 04:56:22 EDT 2020
2+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
13
distributionBase=GRADLE_USER_HOME
24
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
4-
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
zipStoreBase=GRADLE_USER_HOME

src/commonMain/kotlin/com.adamratzman.spotify/Builder.kt

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ fun getSpotifyAuthorizationUrl(vararg scopes: SpotifyScope, clientId: String, re
3333
return SpotifyApi.getAuthUrlFull(*scopes, clientId = clientId, redirectUri = redirectUri, isImplicitGrantFlow = isImplicitGrantFlow, shouldShowDialog = shouldShowDialog)
3434
}
3535

36-
3736
// ==============================================
3837

3938
// Implicit grant builder
@@ -77,7 +76,6 @@ fun spotifyImplicitGrantApi(clientId: String, redirectUri: String, token: Token,
7776
options.json
7877
)
7978

80-
8179
// App Api builders
8280

8381
/*
@@ -131,7 +129,6 @@ fun spotifyAppApi(clientId: String, clientSecret: String, block: SpotifyAppApiBu
131129
*/
132130
fun spotifyAppApi(block: SpotifyAppApiBuilder.() -> Unit) = SpotifyAppApiBuilder().apply(block)
133131

134-
135132
// Client Api Builders
136133
/*
137134
____________________________
@@ -160,10 +157,10 @@ fun spotifyAppApi(block: SpotifyAppApiBuilder.() -> Unit) = SpotifyAppApiBuilder
160157
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
161158
*/
162159
fun spotifyClientApi(
163-
clientId: String,
164-
clientSecret: String,
165-
redirectUri: String,
166-
block: SpotifyClientApiBuilder.() -> Unit
160+
clientId: String,
161+
clientSecret: String,
162+
redirectUri: String,
163+
block: SpotifyClientApiBuilder.() -> Unit
167164
) = SpotifyClientApiBuilder().apply(block).apply {
168165
credentials {
169166
this.clientId = clientId
@@ -189,12 +186,12 @@ fun spotifyClientApi(
189186
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
190187
*/
191188
fun spotifyClientApi(
192-
clientId: String,
193-
clientSecret: String,
194-
redirectUri: String,
195-
authorization: SpotifyUserAuthorization,
196-
options: SpotifyApiOptions? = null,
197-
block: SpotifyClientApiBuilder.() -> Unit = {}
189+
clientId: String,
190+
clientSecret: String,
191+
redirectUri: String,
192+
authorization: SpotifyUserAuthorization,
193+
options: SpotifyApiOptions? = null,
194+
block: SpotifyClientApiBuilder.() -> Unit = {}
198195
) = SpotifyClientApiBuilder().apply(block).apply {
199196
credentials {
200197
this.clientId = clientId
@@ -217,14 +214,13 @@ fun spotifyClientApi(
217214
*/
218215
fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientApiBuilder().apply(block)
219216

220-
221217
/**
222218
* Spotify API builder
223219
*/
224220
class SpotifyApiBuilder(
225-
private var clientId: String?,
226-
private var clientSecret: String?,
227-
private var redirectUri: String?
221+
private var clientId: String?,
222+
private var clientSecret: String?,
223+
private var redirectUri: String?
228224
) {
229225
/**
230226
* Allows you to authenticate a [SpotifyClientApi] with an authorization code
@@ -501,9 +497,9 @@ interface ISpotifyClientApiBuilder : ISpotifyApiBuilder<SpotifyClientApi, Spotif
501497
* [SpotifyClientApi] builder for api creation using client authorization
502498
*/
503499
class SpotifyClientApiBuilder(
504-
override var credentials: SpotifyCredentials = SpotifyCredentialsBuilder().build(),
505-
override var authorization: SpotifyUserAuthorization = SpotifyUserAuthorizationBuilder().build(),
506-
override var options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
500+
override var credentials: SpotifyCredentials = SpotifyCredentialsBuilder().build(),
501+
override var authorization: SpotifyUserAuthorization = SpotifyUserAuthorizationBuilder().build(),
502+
override var options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
507503
) : ISpotifyClientApiBuilder {
508504
override fun getAuthorizationUrl(vararg scopes: SpotifyScope): String {
509505
require(credentials.redirectUri != null && credentials.clientId != null) { "You didn't specify a redirect uri or client id in the credentials block!" }
@@ -613,9 +609,9 @@ interface ISpotifyAppApiBuilder : ISpotifyApiBuilder<SpotifyAppApi, SpotifyAppAp
613609
* [SpotifyAppApi] builder for api creation using client authorization
614610
*/
615611
class SpotifyAppApiBuilder(
616-
override var credentials: SpotifyCredentials = SpotifyCredentialsBuilder().build(),
617-
override var authorization: SpotifyUserAuthorization = SpotifyUserAuthorizationBuilder().build(),
618-
override var options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
612+
override var credentials: SpotifyCredentials = SpotifyCredentialsBuilder().build(),
613+
override var authorization: SpotifyUserAuthorization = SpotifyUserAuthorizationBuilder().build(),
614+
override var options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
619615
) : ISpotifyAppApiBuilder {
620616
/**
621617
* Build a public [SpotifyAppApi] using the provided credentials
@@ -734,10 +730,10 @@ data class SpotifyCredentials(val clientId: String?, val clientSecret: String?,
734730
* limited time constraint on these before the API automatically refreshes them
735731
*/
736732
class SpotifyUserAuthorizationBuilder(
737-
var authorizationCode: String? = null,
738-
var tokenString: String? = null,
739-
var token: Token? = null,
740-
var refreshTokenString: String? = null
733+
var authorizationCode: String? = null,
734+
var tokenString: String? = null,
735+
var token: Token? = null,
736+
var refreshTokenString: String? = null
741737
) {
742738
fun build() = SpotifyUserAuthorization(authorizationCode, tokenString, token, refreshTokenString)
743739
}
@@ -754,10 +750,10 @@ class SpotifyUserAuthorizationBuilder(
754750
* @property refreshTokenString Refresh token, given as a string, to be exchanged to Spotify for a new token
755751
*/
756752
data class SpotifyUserAuthorization(
757-
var authorizationCode: String? = null,
758-
var tokenString: String? = null,
759-
var token: Token? = null,
760-
var refreshTokenString: String? = null
753+
var authorizationCode: String? = null,
754+
var tokenString: String? = null,
755+
var token: Token? = null,
756+
var refreshTokenString: String? = null
761757
)
762758

763759
/**
@@ -777,17 +773,17 @@ data class SpotifyUserAuthorization(
777773
*
778774
*/
779775
class SpotifyApiOptionsBuilder(
780-
var useCache: Boolean = true,
781-
var cacheLimit: Int? = 200,
782-
var automaticRefresh: Boolean = true,
783-
var retryWhenRateLimited: Boolean = true,
784-
var enableLogger: Boolean = true,
785-
var testTokenValidity: Boolean = false,
786-
var enableAllOptions: Boolean = false,
787-
var defaultLimit: Int = 50,
788-
var allowBulkRequests: Boolean = true,
789-
var requestTimeoutMillis: Long? = null,
790-
var json: Json = nonstrictJson
776+
var useCache: Boolean = true,
777+
var cacheLimit: Int? = 200,
778+
var automaticRefresh: Boolean = true,
779+
var retryWhenRateLimited: Boolean = true,
780+
var enableLogger: Boolean = true,
781+
var testTokenValidity: Boolean = false,
782+
var enableAllOptions: Boolean = false,
783+
var defaultLimit: Int = 50,
784+
var allowBulkRequests: Boolean = true,
785+
var requestTimeoutMillis: Long? = null,
786+
var json: Json = nonstrictJson
791787
) {
792788
fun build() =
793789
if (enableAllOptions)
@@ -835,16 +831,16 @@ class SpotifyApiOptionsBuilder(
835831
*/
836832

837833
data class SpotifyApiOptions(
838-
var useCache: Boolean,
839-
var cacheLimit: Int?,
840-
var automaticRefresh: Boolean,
841-
var retryWhenRateLimited: Boolean,
842-
var enableLogger: Boolean,
843-
var testTokenValidity: Boolean,
844-
var defaultLimit: Int,
845-
var allowBulkRequests: Boolean,
846-
var requestTimeoutMillis: Long?,
847-
var json: Json
834+
var useCache: Boolean,
835+
var cacheLimit: Int?,
836+
var automaticRefresh: Boolean,
837+
var retryWhenRateLimited: Boolean,
838+
var enableLogger: Boolean,
839+
var testTokenValidity: Boolean,
840+
var defaultLimit: Int,
841+
var allowBulkRequests: Boolean,
842+
var requestTimeoutMillis: Long?,
843+
var json: Json
848844
)
849845

850846
@Deprecated("Name has been replaced by `options`", ReplaceWith("SpotifyApiOptions"))
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package com.adamratzman.spotify.endpoints.client
3+
4+
import com.adamratzman.spotify.SpotifyApi
5+
import com.adamratzman.spotify.SpotifyException.BadRequestException
6+
import com.adamratzman.spotify.SpotifyRestActionPaging
7+
import com.adamratzman.spotify.endpoints.public.SearchApi
8+
import com.adamratzman.spotify.models.PagingObject
9+
import com.adamratzman.spotify.models.SimpleEpisode
10+
import com.adamratzman.spotify.models.SimpleShow
11+
import com.adamratzman.spotify.models.serialization.toPagingObject
12+
import com.adamratzman.spotify.utils.Market
13+
14+
/**
15+
* Get Spotify catalog information about artists, albums, tracks, playlists, episodes, or shows that match a keyword string.
16+
*
17+
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/search/search/)**
18+
*/
19+
class ClientSearchApi(api: SpotifyApi<*, *>) : SearchApi(api) {
20+
21+
/**
22+
* Get Spotify Catalog information about shows that match the keyword string. See [SearchApi.search] for more information
23+
*
24+
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/search/search/)**
25+
*
26+
* @param query Search query keywords and optional field filters and operators.
27+
* @param market Provide this parameter if you want to apply [Track Relinking](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#track-relinking)
28+
* @param limit The number of objects to return. Default: 50 (or api limit). Minimum: 1. Maximum: 50.
29+
* @param offset The index of the first item to return. Default: 0. Use with limit to get the next set of items
30+
*
31+
* @see [SearchApi.search]
32+
*
33+
* @return [PagingObject] of non-full [SimpleShow] objects ordered by likelihood of correct match
34+
*
35+
* @throws BadRequestException if filters are illegal or query is malformed
36+
*/
37+
fun searchShow(
38+
query: String,
39+
limit: Int? = api.defaultLimit,
40+
offset: Int? = null,
41+
market: Market? = null
42+
): SpotifyRestActionPaging<SimpleShow, PagingObject<SimpleShow>> {
43+
return toActionPaging {
44+
get(build(query, market, limit, offset, SearchType.SHOW))
45+
.toPagingObject(SimpleShow.serializer(), "shows", this, json)
46+
}
47+
}
48+
49+
/**
50+
* Get Spotify Catalog information about episodes that match the keyword string. See [SearchApi.search] for more information
51+
*
52+
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/search/search/)**
53+
*
54+
* @param query Search query keywords and optional field filters and operators.
55+
* @param market Provide this parameter if you want to apply [Track Relinking](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#track-relinking)
56+
* @param limit The number of objects to return. Default: 50 (or api limit). Minimum: 1. Maximum: 50.
57+
* @param offset The index of the first item to return. Default: 0. Use with limit to get the next set of items
58+
*
59+
* @see [SearchApi.search]
60+
*
61+
* @return [PagingObject] of non-full [SimpleEpisode] objects ordered by likelihood of correct match
62+
*
63+
* @throws BadRequestException if filters are illegal or query is malformed
64+
*/
65+
fun searchEpisode(
66+
query: String,
67+
limit: Int? = api.defaultLimit,
68+
offset: Int? = null,
69+
market: Market? = null
70+
): SpotifyRestActionPaging<SimpleEpisode, PagingObject<SimpleEpisode>> {
71+
return toActionPaging {
72+
get(build(query, market, limit, offset, SearchType.EPISODE))
73+
.toPagingObject(SimpleEpisode.serializer(), "episodes", this, json)
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)