@@ -75,13 +75,12 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
75
75
}
76
76
}
77
77
78
-
79
78
internal suspend fun get (url : String ): String {
80
79
return execute<String >(url)
81
80
}
82
81
83
82
internal suspend fun getNullable (url : String ): String? {
84
- return execute<String ?>(url)
83
+ return execute<String ?>(url, retryOnNull = false )
85
84
}
86
85
87
86
internal suspend fun post (url : String , body : String? = null, contentType : String? = null): String {
@@ -100,14 +99,16 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
100
99
return execute<String >(url, body, HttpRequestMethod .DELETE , contentType = contentType)
101
100
}
102
101
103
- private suspend fun <T : String ?> execute (
102
+ @Suppress(" UNCHECKED_CAST" )
103
+ private suspend fun <ReturnType : String ?> execute (
104
104
url : String ,
105
105
body : String? = null,
106
106
method : HttpRequestMethod = HttpRequestMethod .GET ,
107
107
retry202 : Boolean = true,
108
108
contentType : String? = null,
109
- attemptedRefresh : Boolean = false
110
- ): String {
109
+ attemptedRefresh : Boolean = false,
110
+ retryOnNull : Boolean = true
111
+ ): ReturnType {
111
112
if (api.token.shouldRefresh()) {
112
113
if (! api.spotifyApiOptions.automaticRefresh) throw SpotifyException .ReAuthenticationNeededException (message = " The access token has expired." )
113
114
else api.refreshToken()
@@ -116,7 +117,7 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
116
117
val spotifyRequest = SpotifyRequest (url, method, body, api)
117
118
val cacheState = if (api.useCache) cache[spotifyRequest] else null
118
119
119
- if (cacheState?.isStillValid() == true ) return cacheState.data
120
+ if (cacheState?.isStillValid() == true ) return cacheState.data as ReturnType
120
121
else if (cacheState?.let { it.eTag == null } == true ) {
121
122
cache - = spotifyRequest
122
123
}
@@ -131,18 +132,18 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
131
132
retryIfInternalServerErrorLeft = api.spotifyApiOptions.retryOnInternalServerErrorTimes
132
133
)
133
134
134
- handleResponse(document, cacheState, spotifyRequest, retry202) ? : execute< T >(
135
- url,
136
- body,
137
- method,
138
- false ,
139
- contentType
140
- )
135
+ handleResponse(document, cacheState, spotifyRequest, retry202) ? : run {
136
+ if (retryOnNull) {
137
+ execute< ReturnType >(url, body, method, false , contentType)
138
+ } else {
139
+ null
140
+ }
141
+ }
141
142
} catch (e: BadRequestException ) {
142
143
if (e.statusCode == 401 && ! attemptedRefresh) {
143
144
api.refreshToken()
144
145
145
- execute<T >(
146
+ execute<ReturnType >(
146
147
url,
147
148
body,
148
149
method,
@@ -152,7 +153,7 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
152
153
)
153
154
} else throw e
154
155
}
155
- }
156
+ } as ReturnType
156
157
} catch (e: CancellationException ) {
157
158
throw TimeoutException (
158
159
e.message
0 commit comments