Skip to content

Commit 3c45b0d

Browse files
committed
Remove Jsoup
* Replaced missing Jsoup accesses with HttpConnection * Removed Jsoup dependency * Defined endpoint function to be internal
1 parent 7f78198 commit 3c45b0d

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ repositories {
2121
dependencies {
2222
// Actual library dependencies
2323
implementation(group: 'org.json', name: 'json', version: '20180130')
24-
implementation('org.jsoup:jsoup:1.10.3')
2524
implementation("com.google.code.gson:gson:2.8.1")
2625

2726
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11"

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import com.adamratzman.spotify.endpoints.public.PlaylistsAPI
1515
import com.adamratzman.spotify.endpoints.public.SearchAPI
1616
import com.adamratzman.spotify.endpoints.public.TracksAPI
1717
import com.adamratzman.spotify.endpoints.public.UserAPI
18+
import com.adamratzman.spotify.utils.HttpConnection
19+
import com.adamratzman.spotify.utils.HttpHeader
20+
import com.adamratzman.spotify.utils.HttpRequestMethod
1821
import com.adamratzman.spotify.utils.SpotifyEndpoint
1922
import com.adamratzman.spotify.utils.Token
2023
import com.adamratzman.spotify.utils.byteEncode
2124
import com.adamratzman.spotify.utils.toObject
2225
import com.google.gson.Gson
2326
import com.google.gson.GsonBuilder
24-
import org.jsoup.Jsoup
2527
import java.util.concurrent.Executors
2628
import java.util.concurrent.TimeUnit
2729

@@ -159,17 +161,10 @@ class SpotifyApiBuilder {
159161
)
160162
}
161163
else -> try {
162-
val token = Gson().fromJson(
163-
Jsoup.connect("https://accounts.spotify.com/api/token")
164-
.data("grant_type", "client_credentials")
165-
.header("Authorization", "Basic " + ("$clientId:$clientSecret".byteEncode()))
166-
.ignoreContentType(true).post().body().text(), Token::class.java
167-
) ?: throw IllegalArgumentException("Invalid credentials provided")
168-
SpotifyAppAPI(
169-
clientId ?: throw IllegalArgumentException(),
170-
clientSecret ?: throw IllegalArgumentException(),
171-
token
172-
)
164+
if (clientId == null || clientSecret == null) throw IllegalArgumentException("Illegal credentials provided")
165+
val token = getCredentialedToken(clientId, clientSecret)
166+
?: throw IllegalArgumentException("Invalid credentials provided")
167+
SpotifyAppAPI(clientId, clientSecret, token)
173168
} catch (e: Exception) {
174169
throw SpotifyException("Invalid credentials provided in the login process", e)
175170
}
@@ -210,12 +205,12 @@ class SpotifyApiBuilder {
210205
SpotifyClientAPI(
211206
clientId ?: throw IllegalArgumentException(),
212207
clientSecret ?: throw IllegalArgumentException(),
213-
Jsoup.connect("https://accounts.spotify.com/api/token")
214-
.data("grant_type", "authorization_code")
215-
.data("code", authorizationCode)
216-
.data("redirect_uri", redirectUri)
217-
.header("Authorization", "Basic " + ("$clientId:$clientSecret").byteEncode())
218-
.ignoreContentType(true).post().body().text().toObject(Gson(), Token::class.java),
208+
HttpConnection(
209+
url = "https://accounts.spotify.com/api/token",
210+
method = HttpRequestMethod.POST,
211+
body = "grant_type=authorization_code&code=$authorizationCode&redirect_uri=$redirectUri",
212+
contentType = "application/x-www-form-urlencoded"
213+
).execute(HttpHeader("Authorization", "Basic ${"$clientId:$clientSecret".byteEncode()}")).body.toObject(Gson(), Token::class.java),
219214
automaticRefresh,
220215
redirectUri ?: throw IllegalArgumentException()
221216
)
@@ -237,7 +232,7 @@ class SpotifyApiBuilder {
237232
)
238233
else -> throw IllegalArgumentException(
239234
"At least one of: authorizationCode, tokenString, or token must be provided " +
240-
"to build a SpotifyClientAPI object"
235+
"to build a SpotifyClientAPI object"
241236
)
242237
}
243238
}
@@ -300,12 +295,7 @@ class SpotifyAppAPI internal constructor(clientId: String, clientSecret: String,
300295

301296
override fun refreshToken() {
302297
if (clientId != "not-set" && clientSecret != "not-set")
303-
token = gson.fromJson(
304-
Jsoup.connect("https://accounts.spotify.com/api/token")
305-
.data("grant_type", "client_credentials")
306-
.header("Authorization", "Basic " + ("$clientId:$clientSecret".byteEncode()))
307-
.ignoreContentType(true).post().body().text(), Token::class.java
308-
)
298+
token = getCredentialedToken(clientId, clientSecret)
309299
expireTime = System.currentTimeMillis() + token.expires_in * 1000
310300
}
311301

@@ -372,11 +362,13 @@ class SpotifyClientAPI internal constructor(
372362

373363
override fun refreshToken() {
374364
val tempToken = gson.fromJson(
375-
Jsoup.connect("https://accounts.spotify.com/api/token")
376-
.data("grant_type", "refresh_token")
377-
.data("refresh_token", token.refresh_token ?: "")
378-
.header("Authorization", "Basic " + ("$clientId:$clientSecret").byteEncode())
379-
.ignoreContentType(true).post().body().text(), Token::class.java
365+
HttpConnection(
366+
url = "https://accounts.spotify.com/api/token",
367+
method = HttpRequestMethod.POST,
368+
body = "grant_type=refresh_token&refresh_token=${token.refresh_token ?: ""}",
369+
contentType = "application/x-www-form-urlencoded"
370+
).execute(HttpHeader("Authorization", "Basic ${"$clientId:$clientSecret".byteEncode()}")).body,
371+
Token::class.java
380372
)
381373
if (tempToken == null) {
382374
logger.logWarning("Spotify token refresh failed")
@@ -407,7 +399,17 @@ class SpotifyClientAPI internal constructor(
407399

408400
private fun getAuthUrlFull(vararg scopes: SpotifyScope, clientId: String, redirectUri: String): String {
409401
return "https://accounts.spotify.com/authorize/?client_id=$clientId" +
410-
"&response_type=code" +
411-
"&redirect_uri=$redirectUri" +
412-
if (scopes.isEmpty()) "" else "&scope=${scopes.joinToString("%20") { it.uri }}"
402+
"&response_type=code" +
403+
"&redirect_uri=$redirectUri" +
404+
if (scopes.isEmpty()) "" else "&scope=${scopes.joinToString("%20") { it.uri }}"
413405
}
406+
407+
private fun getCredentialedToken(clientId: String, clientSecret: String) = Gson().fromJson(
408+
HttpConnection(
409+
url = "https://accounts.spotify.com/api/token",
410+
method = HttpRequestMethod.POST,
411+
body = "grant_type=client_credentials",
412+
contentType = "application/x-www-form-urlencoded"
413+
).execute(HttpHeader("Authorization", "Basic ${"$clientId:$clientSecret".byteEncode()}")).body,
414+
Token::class.java
415+
)

src/main/kotlin/com/adamratzman/spotify/utils/Endpoints.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import java.util.function.Supplier
1313
abstract class SpotifyEndpoint(val api: SpotifyAPI) {
1414
internal val cache = SpotifyCache()
1515

16-
fun get(url: String): String {
16+
internal fun get(url: String): String {
1717
return execute(url)
1818
}
1919

20-
fun post(url: String, body: String? = null): String {
20+
internal fun post(url: String, body: String? = null): String {
2121
return execute(url, body, HttpRequestMethod.POST)
2222
}
2323

24-
fun put(url: String, body: String? = null, contentType: String? = null): String {
24+
internal fun put(url: String, body: String? = null, contentType: String? = null): String {
2525
return execute(url, body, HttpRequestMethod.PUT, contentType = contentType)
2626
}
2727

28-
fun delete(
28+
internal fun delete(
2929
url: String,
3030
body: String? = null,
3131
contentType: String? = null

0 commit comments

Comments
 (0)