Skip to content

Commit f83ed16

Browse files
committed
add an api builder for pkce authorization with an existing token
1 parent e2413e3 commit f83ed16

File tree

1 file changed

+82
-8
lines changed
  • src/commonMain/kotlin/com.adamratzman.spotify

1 file changed

+82
-8
lines changed

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

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ expect fun getSpotifyPkceCodeChallenge(codeVerifier: String): String
9898

9999
/**
100100
* Instantiate a new [SpotifyImplicitGrantApi] using a Spotify [clientId], [redirectUri], and [token] retrieved from the implicit
101-
* grant flow
101+
* grant flow.
102+
*
103+
* Use case: I have a token obtained after implicit grant authorization.
102104
*
103105
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
104106
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
@@ -145,6 +147,10 @@ fun spotifyImplicitGrantApi(
145147
/**
146148
* Instantiate a new [SpotifyAppApiBuilder] using a Spotify [clientId] and [clientSecret]
147149
*
150+
* Use case: I am using the client credentials flow.
151+
* I only need access to public Spotify API endpoints, don't have an existing token,
152+
* and don't want to deal with advanced configuration.
153+
*
148154
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
149155
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
150156
* @param options Override default API options such as the cache limit
@@ -163,6 +169,10 @@ fun spotifyAppApi(
163169
* Instantiate a new [SpotifyAppApiBuilder] using a Spotify [clientId] and [clientSecret], with the ability to configure
164170
* the api settings by providing a builder initialization [block]
165171
*
172+
* Use case: I am using the client credentials flow.
173+
* I only need access to public Spotify API endpoints, might have an existing token,
174+
* and might want to deal with advanced configuration.
175+
*
166176
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
167177
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
168178
* @param block Api settings block
@@ -183,6 +193,10 @@ fun spotifyAppApi(
183193
/**
184194
* Instantiate a new [SpotifyAppApiBuilder] using a [Token]
185195
*
196+
* Use case: I am using the client credentials flow.
197+
* I only need access to public Spotify API endpoints, I have an existing token,
198+
* and I don't want to deal with advanced configuration.
199+
*
186200
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
187201
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
188202
* @param apiToken The access token that will be associated with the built API instance
@@ -212,12 +226,17 @@ fun spotifyAppApi(
212226
*
213227
* **Note**: You **must** provide your app credentials in the [SpotifyAppApiBuilder.credentials] block
214228
*
229+
* Use case: I am using the client credentials flow.
230+
* I only need access to public Spotify API endpoints, and I want to use the [SpotifyAppApiBuilder] DSL
231+
* to configure everything myself.
232+
*
215233
* @param block Api settings block
216234
*
217235
* @return Configurable [SpotifyAppApiBuilder] that, when built, creates a new [SpotifyAppApi]
218236
*/
219237
fun spotifyAppApi(block: SpotifyAppApiBuilder.() -> Unit) = SpotifyAppApiBuilder().apply(block)
220238

239+
221240
// Client Api Builders
222241
/*
223242
____________________________
@@ -238,6 +257,10 @@ fun spotifyAppApi(block: SpotifyAppApiBuilder.() -> Unit) = SpotifyAppApiBuilder
238257
* **Note**: If trying to build [SpotifyClientApi], you **must** provide client authorization in the [SpotifyClientApiBuilder.authorization]
239258
* block
240259
*
260+
* Use case: I am using the client authorization flow.
261+
* I want access to both public and client Spotify API endpoints, and I want
262+
* to configure authorization and other settings myself.
263+
*
241264
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
242265
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
243266
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
@@ -261,6 +284,9 @@ fun spotifyClientApi(
261284
/**
262285
* Instantiate a new [SpotifyClientApiBuilder] using a [Token]
263286
*
287+
* Use case: I am using the client authorization flow.
288+
* I want access to both public and client Spotify API endpoints, I have an existing token,
289+
* and I might want to configure api options.
264290
*
265291
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
266292
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
@@ -293,6 +319,9 @@ fun spotifyClientApi(
293319
/**
294320
* Instantiate a new [SpotifyClientApiBuilder] using an [authCode]
295321
*
322+
* Use case: I am using the client authorization flow.
323+
* I want access to both public and client Spotify API endpoints, I have an authorization code,
324+
* and I might want to configure api options.
296325
*
297326
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
298327
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
@@ -327,6 +356,10 @@ fun spotifyClientApi(
327356
* with an existing [SpotifyUserAuthorization] and with the ability to configure the api settings by providing a
328357
* builder initialization [block]
329358
*
359+
* Use case: I am using the client authorization flow.
360+
* I want access to both public and client Spotify API endpoints and I want to configure [authorization]
361+
* and [options] without using the DSL.
362+
*
330363
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
331364
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
332365
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
@@ -340,12 +373,11 @@ fun spotifyClientApi(
340373
*/
341374
fun spotifyClientApi(
342375
clientId: String,
343-
clientSecret: String,
376+
clientSecret: String?,
344377
redirectUri: String,
345378
authorization: SpotifyUserAuthorization,
346-
options: SpotifyApiOptionsBuilder? = null,
347-
block: SpotifyClientApiBuilder.() -> Unit = {}
348-
) = SpotifyClientApiBuilder().apply(block).apply {
379+
options: SpotifyApiOptionsBuilder? = null
380+
) = SpotifyClientApiBuilder().apply {
349381
credentials {
350382
this.clientId = clientId
351383
this.clientSecret = clientSecret
@@ -361,6 +393,10 @@ fun spotifyClientApi(
361393
* **Note**: If trying to build [SpotifyClientApi], you **must** provide client authorization in the [SpotifyClientApiBuilder.authorization]
362394
* block
363395
*
396+
* Use case: I am using the client authorization flow.
397+
* I want access to both public and client Spotify API endpoints and I want to handle configuration
398+
* via the DSL myself.
399+
*
364400
* @param block Api settings block
365401
*
366402
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
@@ -383,11 +419,13 @@ fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientA
383419
/**
384420
* Instantiate a new [SpotifyClientApiBuilder] using an [authCode] and [codeVerifier]. This is for **PKCE authorization**.
385421
*
422+
* Use case: I am using the PKCE client authorization flow.
423+
* I want access to both public and client Spotify API endpoints, I have an existing api [token], and I might
424+
* want to set api [options] without using the DSL.
386425
*
387426
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
388-
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
389427
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
390-
* @param authCode The authorization code retrieved after OAuth authorization
428+
* @param token Api token retrieved using PKCE authorization flow.
391429
* @param codeVerifier Your code verifier plaintext.
392430
* @param options Override default API options such as the cache limit
393431
*
@@ -396,7 +434,7 @@ fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientA
396434
fun spotifyClientPkceApi(
397435
clientId: String?,
398436
redirectUri: String?,
399-
authCode: String,
437+
token: Token,
400438
codeVerifier: String,
401439
options: SpotifyApiOptionsBuilder? = null
402440
) = SpotifyClientApiBuilder().apply {
@@ -405,6 +443,41 @@ fun spotifyClientPkceApi(
405443
this.redirectUri = redirectUri
406444
}
407445

446+
authentication {
447+
this.token = token
448+
pkceCodeVerifier = codeVerifier
449+
}
450+
451+
options?.let { this.options = it.build() }
452+
}
453+
454+
/**
455+
* Instantiate a new [SpotifyClientApiBuilder] using a [token] and [codeVerifier]. This is for **PKCE authorization**.
456+
*
457+
* Use case: I am using the PKCE client authorization flow.
458+
* I want access to both public and client Spotify API endpoints, I have an authorization code, and I might
459+
* want to set api [options] without using the DSL.
460+
*
461+
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
462+
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
463+
* @param authCode The authorization code retrieved after OAuth authorization
464+
* @param codeVerifier Your code verifier plaintext.
465+
* @param options Override default API options such as the cache limit
466+
*
467+
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
468+
*/
469+
fun spotifyClientPkceApi(
470+
clientId: String?,
471+
redirectUri: String?,
472+
authCode: String,
473+
codeVerifier: String,
474+
options: SpotifyApiOptionsBuilder? = null
475+
) = SpotifyClientApiBuilder().apply {
476+
credentials {
477+
this.clientId = clientId
478+
this.redirectUri = redirectUri
479+
}
480+
408481
authentication {
409482
authorizationCode = authCode
410483
pkceCodeVerifier = codeVerifier
@@ -413,6 +486,7 @@ fun spotifyClientPkceApi(
413486
options?.let { this.options = it.build() }
414487
}
415488

489+
416490
/**
417491
* Spotify API builder
418492
*/

0 commit comments

Comments
 (0)