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 {
|
private fun parseJsonResponse(response: String, key: String, default: String, isJson: Boolean): String {
|
||||||
|
var parsed = default
|
||||||
if (response.isNotEmpty()) {
|
if (response.isNotEmpty()) {
|
||||||
if (isJson) {
|
if (isJson) {
|
||||||
return response
|
parsed = response
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return JSONObject(response).getString(key, default)
|
parsed = JSONObject(response).getString(key, default)
|
||||||
} catch (jse: JSONException) {
|
} 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.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
import okhttp3.Response
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
@ -118,36 +119,45 @@ class Utils private constructor() {
|
||||||
}.addHeader("Authorization", "Bearer $accessToken")
|
}.addHeader("Authorization", "Bearer $accessToken")
|
||||||
|
|
||||||
val result = client.newCall(builder.build()).execute()
|
val result = client.newCall(builder.build()).execute()
|
||||||
|
response = parseBody(endPoint, result)
|
||||||
val body = result.body?.string()
|
|
||||||
if (body != null) {
|
|
||||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
|
||||||
logApiError(body, result.code)
|
|
||||||
}
|
|
||||||
response = body
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun logApiError(body: String, resultCode: Int) {
|
private fun parseBody(endPoint: String, result: Response): String {
|
||||||
try {
|
val body = result.body?.string()
|
||||||
with(JSONObject(body)) {
|
if (body != null) {
|
||||||
if (has("message")) {
|
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||||
logger.severe(getString("message") + " ($resultCode)")
|
try {
|
||||||
}
|
with(JSONObject(body)) {
|
||||||
if (has("description")) {
|
if (has("message")) {
|
||||||
val description = getString("description")
|
logger.severe(getString("message") + " (${result.code})")
|
||||||
if (description.isNotBlank()) {
|
}
|
||||||
logger.severe(description)
|
if (has("description")) {
|
||||||
|
logger.severe(getString("description"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (jse: JSONException) {
|
||||||
|
logger.log(
|
||||||
|
Level.SEVERE,
|
||||||
|
"An error occurred parsing the error response from Bitly. [$endPoint]",
|
||||||
|
jse
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (jse: JSONException) {
|
return body
|
||||||
logger.log(Level.SEVERE, "An error occurred parsing the error response from bitly.", jse)
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue