Skip to content

Commit 569575c

Browse files
committed
convert httpconnection bodymaps to java maps
1 parent 7f2adc0 commit 569575c

File tree

4 files changed

+49
-43
lines changed

4 files changed

+49
-43
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ apply plugin: 'kotlin'
2222
apply plugin: 'org.jetbrains.dokka'
2323

2424
group 'com.adamratzman'
25-
version '2.3.02'
25+
version '2.3.03'
2626

2727
archivesBaseName = 'spotify-api-kotlin'
2828

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ fun spotifyApi(block: SpotifyApiBuilderDsl.() -> Unit) = SpotifyApiBuilderDsl().
1616
* Spotify traditional Java style API builder
1717
*/
1818
class SpotifyApiBuilder(
19-
private var clientId: String,
20-
private var clientSecret: String
19+
private var clientId: String,
20+
private var clientSecret: String
2121
) {
2222
private var redirectUri: String? = null
2323
private var authorizationCode: String? = null
@@ -190,17 +190,17 @@ data class SpotifyCredentials(val clientId: String?, val clientSecret: String?,
190190
* limited time constraint on these before the API automatically refreshes them
191191
*/
192192
class SpotifyUserAuthorizationBuilder(
193-
var authorizationCode: String? = null,
194-
var tokenString: String? = null,
195-
var token: Token? = null
193+
var authorizationCode: String? = null,
194+
var tokenString: String? = null,
195+
var token: Token? = null
196196
) {
197197
fun build() = SpotifyUserAuthorization(authorizationCode, tokenString, token)
198198
}
199199

200200
data class SpotifyUserAuthorization(
201-
val authorizationCode: String?,
202-
val tokenString: String?,
203-
val token: Token?
201+
val authorizationCode: String?,
202+
val tokenString: String?,
203+
val token: Token?
204204
)
205205

206206
/**
@@ -215,17 +215,18 @@ data class SpotifyUserAuthorization(
215215
* @property enableAllUtilities Whether to enable all provided utilities
216216
*/
217217
class SpotifyUtilitiesBuilder(
218-
var useCache: Boolean = true,
219-
var cacheLimit: Int? = 200,
220-
var automaticRefresh: Boolean = true,
221-
var retryWhenRateLimited: Boolean = true,
222-
var enableLogger: Boolean = true,
223-
var testTokenValidity: Boolean = false,
224-
var enableAllUtilities: Boolean = false
218+
var useCache: Boolean = true,
219+
var cacheLimit: Int? = 200,
220+
var automaticRefresh: Boolean = true,
221+
var retryWhenRateLimited: Boolean = true,
222+
var enableLogger: Boolean = true,
223+
var testTokenValidity: Boolean = false,
224+
var enableAllUtilities: Boolean = false
225225
) {
226226
fun build() =
227227
if (enableAllUtilities)
228-
SpotifyUtilities(true,
228+
SpotifyUtilities(
229+
true,
229230
200,
230231
automaticRefresh = true,
231232
retryWhenRateLimited = true,
@@ -244,12 +245,12 @@ class SpotifyUtilitiesBuilder(
244245
}
245246

246247
data class SpotifyUtilities(
247-
val useCache: Boolean,
248-
val cacheLimit: Int?,
249-
val automaticRefresh: Boolean,
250-
val retryWhenRateLimited: Boolean,
251-
val enableLogger: Boolean,
252-
val testTokenValidity: Boolean
248+
val useCache: Boolean,
249+
val cacheLimit: Int?,
250+
val automaticRefresh: Boolean,
251+
val retryWhenRateLimited: Boolean,
252+
val enableLogger: Boolean,
253+
val testTokenValidity: Boolean
253254
)
254255

255256
/**
@@ -397,9 +398,9 @@ class SpotifyApiBuilderDsl {
397398
* @param token [Token] object (useful if you already have exchanged an authorization code yourself
398399
*/
399400
private fun buildClient(
400-
authorizationCode: String? = null,
401-
tokenString: String? = null,
402-
token: Token? = null
401+
authorizationCode: String? = null,
402+
tokenString: String? = null,
403+
token: Token? = null
403404
): SpotifyClientAPI {
404405
val clientId = credentials.clientId
405406
val clientSecret = credentials.clientSecret
@@ -417,8 +418,12 @@ class SpotifyApiBuilderDsl {
417418
val response = executeTokenRequest(HttpConnection(
418419
url = "https://accounts.spotify.com/api/token",
419420
method = HttpRequestMethod.POST,
420-
bodyMap = null,
421-
bodyString = "grant_type=authorization_code&code=$authorizationCode&redirect_uri=$redirectUri",
421+
bodyMap = mapOf(
422+
"grant_type" to "authorization_code",
423+
"code" to authorizationCode,
424+
"redirect_uri" to redirectUri
425+
),
426+
bodyString = null,
422427
contentType = "application/x-www-form-urlencoded",
423428
api = null
424429
), clientId, clientSecret)

src/main/kotlin/com/adamratzman/spotify/SpotifyAPI.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,11 @@ class SpotifyClientAPI internal constructor(
366366
val response = executeTokenRequest(HttpConnection(
367367
url = "https://accounts.spotify.com/api/token",
368368
method = HttpRequestMethod.POST,
369-
bodyMap = null,
370-
bodyString = "grant_type=refresh_token&refresh_token=${token.refreshToken ?: ""}",
369+
bodyMap = mapOf(
370+
"grant_type" to "refresh_token",
371+
"refresh_token" to token.refreshToken
372+
),
373+
bodyString = null,
371374
contentType = "application/x-www-form-urlencoded",
372375
api = this
373376
), clientId, clientSecret)
@@ -452,8 +455,8 @@ fun getCredentialedToken(clientId: String, clientSecret: String, api: SpotifyAPI
452455
val response = executeTokenRequest(HttpConnection(
453456
url = "https://accounts.spotify.com/api/token",
454457
method = HttpRequestMethod.POST,
455-
bodyMap = null,
456-
bodyString = "grant_type=client_credentials",
458+
bodyMap = mapOf("grant_type" to "client_credentials"),
459+
bodyString = null,
457460
contentType = "application/x-www-form-urlencoded",
458461
api = api
459462
), clientId, clientSecret)

src/main/kotlin/com/adamratzman/spotify/http/HttpConnection.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ import com.adamratzman.spotify.SpotifyAPI
55
import com.adamratzman.spotify.models.SpotifyRatelimitedException
66
import com.google.api.client.http.ByteArrayContent
77
import com.google.api.client.http.GenericUrl
8-
import com.google.api.client.http.UrlEncodedContent
98
import com.google.api.client.http.javanet.NetHttpTransport
109
import java.util.concurrent.TimeUnit
1110

1211
enum class HttpRequestMethod { GET, POST, PUT, DELETE }
1312
data class HttpHeader(val key: String, val value: String)
1413

1514
internal class HttpConnection(
16-
private val url: String,
17-
private val method: HttpRequestMethod,
18-
private val bodyMap: Map<Any, Any>?,
19-
private val bodyString: String?,
20-
private val contentType: String?,
21-
private val headers: List<HttpHeader> = listOf(),
22-
val api: SpotifyAPI? = null
15+
private val url: String,
16+
private val method: HttpRequestMethod,
17+
private val bodyMap: Map<*, *>?,
18+
private val bodyString: String?,
19+
private val contentType: String?,
20+
private val headers: List<HttpHeader> = listOf(),
21+
val api: SpotifyAPI? = null
2322
) {
2423

2524
companion object {
@@ -33,9 +32,8 @@ internal class HttpConnection(
3332
HttpRequestMethod.DELETE -> requestFactory.buildDeleteRequest(genericUrl)
3433
HttpRequestMethod.PUT, HttpRequestMethod.POST -> {
3534
val content = if (contentType == "application/x-www-form-urlencoded") {
36-
bodyMap?.let { body ->
37-
UrlEncodedContent(body.map { it.key.toString() to it.value.toString() }.toMap())
38-
} ?: ByteArrayContent.fromString(contentType, bodyString)
35+
bodyMap?.map { "${it.key}=${it.value}" }?.joinToString("&")?.let { ByteArrayContent.fromString(contentType, it) }
36+
?: ByteArrayContent.fromString(contentType, bodyString)
3937
} else bodyString?.let { ByteArrayContent.fromString(contentType, bodyString) }
4038

4139
if (method == HttpRequestMethod.PUT) requestFactory.buildPutRequest(genericUrl, content)

0 commit comments

Comments
 (0)