Reduced complexity.
This commit is contained in:
parent
abdae1cc77
commit
42348476f6
2 changed files with 36 additions and 25 deletions
|
@ -114,17 +114,18 @@ class Bitlinks(private val accessToken: String) {
|
|||
}
|
||||
|
||||
private fun parseJsonResponse(response: String, key: String, default: String, isJson: Boolean): String {
|
||||
var parsed = default
|
||||
if (response.isNotEmpty()) {
|
||||
if (isJson) {
|
||||
return response
|
||||
parsed = response
|
||||
} else {
|
||||
try {
|
||||
return JSONObject(response).getString(key, default)
|
||||
parsed = JSONObject(response).getString(key, default)
|
||||
} catch (jse: JSONException) {
|
||||
Utils.logger.log(Level.SEVERE, "An error occurred parsing the response from bitly.", jse)
|
||||
Utils.logger.log(Level.SEVERE, "An error occurred parsing the response from Bitly.", jse)
|
||||
}
|
||||
}
|
||||
}
|
||||
return default
|
||||
return parsed
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
|
@ -118,37 +119,46 @@ class Utils private constructor() {
|
|||
}.addHeader("Authorization", "Bearer $accessToken")
|
||||
|
||||
val result = client.newCall(builder.build()).execute()
|
||||
|
||||
val body = result.body?.string()
|
||||
if (body != null) {
|
||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||
logApiError(body, result.code)
|
||||
}
|
||||
response = body
|
||||
response = parseBody(endPoint, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
private fun logApiError(body: String, resultCode: Int) {
|
||||
private fun parseBody(endPoint: String, result: Response): String {
|
||||
val body = result.body?.string()
|
||||
if (body != null) {
|
||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||
try {
|
||||
with(JSONObject(body)) {
|
||||
if (has("message")) {
|
||||
logger.severe(getString("message") + " ($resultCode)")
|
||||
logger.severe(getString("message") + " (${result.code})")
|
||||
}
|
||||
if (has("description")) {
|
||||
val description = getString("description")
|
||||
if (description.isNotBlank()) {
|
||||
logger.severe(description)
|
||||
}
|
||||
logger.severe(getString("description"))
|
||||
}
|
||||
}
|
||||
} catch (jse: JSONException) {
|
||||
logger.log(Level.SEVERE, "An error occurred parsing the error response from bitly.", jse)
|
||||
logger.log(
|
||||
Level.SEVERE,
|
||||
"An error occurred parsing the error response from Bitly. [$endPoint]",
|
||||
jse
|
||||
)
|
||||
}
|
||||
}
|
||||
return body
|
||||
}
|
||||
return Constants.EMPTY
|
||||
}
|
||||
|
||||
private fun validateCall(accessToken: String, endPoint: String): Boolean {
|
||||
when {
|
||||
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
|
||||
accessToken.isBlank() -> logger.severe("Please specify a valid API access token.")
|
||||
else -> return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a URL.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue