|
1 | 1 | package com.adamratzman.spotify.utils |
2 | 2 |
|
3 | 3 | import com.adamratzman.spotify.main.SpotifyAPI |
4 | | -import com.adamratzman.spotify.main.SpotifyClientAPI |
5 | | -import com.adamratzman.spotify.main.base |
6 | 4 | import com.google.gson.Gson |
7 | 5 | import org.json.JSONObject |
8 | | -import org.jsoup.Connection |
9 | | -import org.jsoup.Jsoup |
10 | 6 | import java.io.InvalidObjectException |
11 | 7 | import java.net.URLEncoder |
12 | 8 | import java.util.* |
13 | 9 | import java.util.function.Supplier |
14 | 10 |
|
15 | | -abstract class SpotifyEndpoint(val api: SpotifyAPI) { |
16 | | - fun get(url: String): String { |
17 | | - return execute(url) |
18 | | - } |
19 | | - |
20 | | - fun post(url: String, body: String? = null): String { |
21 | | - return execute(url, body, Connection.Method.POST) |
22 | | - } |
23 | | - |
24 | | - fun put(url: String, body: String? = null, contentType: String? = null): String { |
25 | | - return execute(url, body, Connection.Method.PUT, contentType = contentType) |
26 | | - } |
27 | | - |
28 | | - fun delete(url: String, body: String? = null, data: List<Pair<String, String>>? = null, contentType: String? = null): String { |
29 | | - return execute(url, body, Connection.Method.DELETE, data = data, contentType = contentType) |
30 | | - } |
31 | | - |
32 | | - private fun execute(url: String, body: String? = null, method: Connection.Method = Connection.Method.GET, |
33 | | - retry202: Boolean = true, contentType: String? = null, data: List<Pair<String, String>>? = null): String { |
34 | | - if (api !is SpotifyClientAPI && System.currentTimeMillis() >= api.expireTime) { |
35 | | - api.refreshClient() |
36 | | - api.expireTime = System.currentTimeMillis() + api.token.expires_in * 1000 |
37 | | - } |
38 | | - var connection = Jsoup.connect(url).ignoreContentType(true) |
39 | | - data?.forEach { connection.data(it.first, it.second) } |
40 | | - if (contentType != null) connection.header("Content-Type", contentType) |
41 | | - if (body != null) { |
42 | | - if (contentType != null) connection.requestBody(body) |
43 | | - else |
44 | | - connection = if (method == Connection.Method.DELETE) { |
45 | | - val key = JSONObject(body).keySet().toList()[0] |
46 | | - connection.data(key, JSONObject(body).getJSONArray(key).toString()) |
47 | | - } else connection.requestBody(body) |
48 | | - } |
49 | | - connection = connection.header("Authorization", "Bearer ${api.token.access_token}") |
50 | | - val document = connection.method(method).ignoreHttpErrors(true).execute() |
51 | | - if (document.statusCode() / 200 != 1 /* Check if status is 2xx */) throw BadRequestException(api.gson.fromJson(document.body(), ErrorResponse::class.java).error) |
52 | | - else if (document.statusCode() == 202 && retry202) return execute(url, body, method, false) |
53 | | - return document.body() |
54 | | - } |
55 | | - |
56 | | - fun <T> toAction(supplier: Supplier<T>) = SpotifyRestAction(api, supplier) |
57 | | -} |
58 | | - |
59 | | -internal class EndpointBuilder(val path: String) { |
60 | | - val builder = StringBuilder(base) |
61 | | - |
62 | | - init { |
63 | | - builder.append(path) |
64 | | - } |
65 | | - |
66 | | - fun with(key: String, value: Any?): EndpointBuilder { |
67 | | - if (value != null && (value !is String || value.isNotEmpty())) { |
68 | | - if (builder.toString() == base + path) builder.append("?") |
69 | | - else builder.append("&") |
70 | | - builder.append(key).append("=").append(value.toString()) |
71 | | - } |
72 | | - return this |
73 | | - } |
74 | | - |
75 | | - fun build() = builder.toString() |
76 | | -} |
77 | | - |
78 | 11 | data class CursorBasedPagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String?, val cursors: Cursor, |
79 | 12 | val total: Int, val endpoint: SpotifyEndpoint) { |
80 | 13 | inline fun <reified T> getNext(): SpotifyRestAction<CursorBasedPagingObject<T>> = endpoint.toAction( |
|
0 commit comments