Cleanup.
This commit is contained in:
parent
25d2904a6e
commit
364bacc619
1 changed files with 47 additions and 42 deletions
|
@ -80,14 +80,14 @@ open class Bitly() {
|
||||||
private var client: OkHttpClient
|
private var client: OkHttpClient
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (logger.isLoggable(Level.FINE)) {
|
client = if (logger.isLoggable(Level.FINE)) {
|
||||||
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
|
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
|
||||||
level = HttpLoggingInterceptor.Level.BODY
|
level = HttpLoggingInterceptor.Level.BODY
|
||||||
redactHeader("Authorization")
|
redactHeader("Authorization")
|
||||||
}
|
}
|
||||||
client = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
|
OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
|
||||||
} else {
|
} else {
|
||||||
client = OkHttpClient.Builder().build()
|
OkHttpClient.Builder().build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,36 +162,37 @@ open class Bitly() {
|
||||||
* @return The response (JSON) from the API.
|
* @return The response (JSON) from the API.
|
||||||
*/
|
*/
|
||||||
fun executeCall(endPoint: String, params: Map<String, String>, method: Methods = Methods.POST): String {
|
fun executeCall(endPoint: String, params: Map<String, String>, method: Methods = Methods.POST): String {
|
||||||
var returnValue = ""
|
var response = ""
|
||||||
if (endPoint.isBlank()) {
|
if (endPoint.isBlank()) {
|
||||||
logger.severe("Please specify a valid API endpoint.")
|
logger.severe("Please specify a valid API endpoint.")
|
||||||
} else if (accessToken.isBlank()) {
|
} else if (accessToken.isBlank()) {
|
||||||
logger.severe("Please specify a valid API access token.")
|
logger.severe("Please specify a valid API access token.")
|
||||||
} else {
|
} else {
|
||||||
val apiUrl = endPoint.toHttpUrlOrNull()
|
val apiUrl = endPoint.toHttpUrlOrNull()
|
||||||
val builder: Request.Builder
|
|
||||||
if (apiUrl != null) {
|
if (apiUrl != null) {
|
||||||
if (method == Methods.POST || method == Methods.PATCH) {
|
val builder = when (method) {
|
||||||
|
Methods.POST, Methods.PATCH -> {
|
||||||
val formBody = JSONObject(params).toString()
|
val formBody = JSONObject(params).toString()
|
||||||
.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
|
.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
|
||||||
builder = Request.Builder().apply {
|
Request.Builder().apply {
|
||||||
url(apiUrl.newBuilder().build())
|
url(apiUrl.newBuilder().build())
|
||||||
if (method == Methods.POST)
|
if (method == Methods.POST) {
|
||||||
post(formBody)
|
post(formBody)
|
||||||
else
|
} else {
|
||||||
patch(formBody)
|
patch(formBody)
|
||||||
}
|
}
|
||||||
} else if (method == Methods.DELETE) {
|
}
|
||||||
builder = Request.Builder().url(apiUrl.newBuilder().build()).delete()
|
}
|
||||||
} else {
|
Methods.DELETE -> Request.Builder().url(apiUrl.newBuilder().build()).delete()
|
||||||
|
else -> {
|
||||||
val httpUrl = apiUrl.newBuilder().apply {
|
val httpUrl = apiUrl.newBuilder().apply {
|
||||||
params.forEach {
|
params.forEach {
|
||||||
addQueryParameter(it.key, it.value)
|
addQueryParameter(it.key, it.value)
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
builder = Request.Builder().url(httpUrl)
|
Request.Builder().url(httpUrl)
|
||||||
}
|
}
|
||||||
builder.addHeader("Authorization", "Bearer $accessToken")
|
}.addHeader("Authorization", "Bearer $accessToken")
|
||||||
|
|
||||||
val result = client.newCall(builder.build()).execute()
|
val result = client.newCall(builder.build()).execute()
|
||||||
|
|
||||||
|
@ -200,12 +201,12 @@ open class Bitly() {
|
||||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||||
logApiError(body, result.code)
|
logApiError(body, result.code)
|
||||||
}
|
}
|
||||||
returnValue = body
|
response = body
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnValue
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -221,27 +222,30 @@ open class Bitly() {
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun shorten(long_url: String, group_guid: String = "", domain: String = "", isJson: Boolean = false): String {
|
fun shorten(long_url: String, group_guid: String = "", domain: String = "", isJson: Boolean = false): String {
|
||||||
var returnValue = if (isJson) "{}" else ""
|
var bitlink = if (isJson) "{}" else ""
|
||||||
if (!validateUrl(long_url)) {
|
if (!validateUrl(long_url)) {
|
||||||
logger.severe("Please specify a valid URL to shorten.")
|
logger.severe("Please specify a valid URL to shorten.")
|
||||||
} else {
|
} else {
|
||||||
val params: HashMap<String, String> = HashMap()
|
val params: HashMap<String, String> = HashMap()
|
||||||
if (group_guid.isNotBlank())
|
if (group_guid.isNotBlank()) {
|
||||||
params["group_guid"] = group_guid
|
params["group_guid"] = group_guid
|
||||||
if (domain.isNotBlank())
|
}
|
||||||
|
if (domain.isNotBlank()) {
|
||||||
params["domain"] = domain
|
params["domain"] = domain
|
||||||
|
}
|
||||||
params["long_url"] = long_url
|
params["long_url"] = long_url
|
||||||
|
|
||||||
val response = executeCall(buildEndPointUrl("/shorten"), params)
|
val response = executeCall(buildEndPointUrl("/shorten"), params)
|
||||||
|
|
||||||
if (response.isNotEmpty()) {
|
if (response.isNotEmpty()) {
|
||||||
if (isJson) {
|
if (isJson) {
|
||||||
returnValue = response
|
bitlink = response
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
val json = JSONObject(response)
|
val json = JSONObject(response)
|
||||||
if (json.has("link"))
|
if (json.has("link")) {
|
||||||
returnValue = json.getString("link")
|
bitlink = json.getString("link")
|
||||||
|
}
|
||||||
} catch (ignore: JSONException) {
|
} catch (ignore: JSONException) {
|
||||||
logger.severe("An error occurred parsing the response from bitly.")
|
logger.severe("An error occurred parsing the response from bitly.")
|
||||||
}
|
}
|
||||||
|
@ -249,21 +253,22 @@ open class Bitly() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnValue
|
return bitlink
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun logApiError(body: String, resultCode: Int) {
|
private fun logApiError(body: String, resultCode: Int) {
|
||||||
try {
|
try {
|
||||||
val jsonResponse = JSONObject(body)
|
with(JSONObject(body)) {
|
||||||
if (jsonResponse.has("message")) {
|
if (has("message")) {
|
||||||
logger.severe(jsonResponse.getString("message") + " ($resultCode)")
|
logger.severe(getString("message") + " ($resultCode)")
|
||||||
}
|
}
|
||||||
if (jsonResponse.has("description")) {
|
if (has("description")) {
|
||||||
val description = jsonResponse.getString("description")
|
val description = getString("description")
|
||||||
if (description.isNotBlank()) {
|
if (description.isNotBlank()) {
|
||||||
logger.severe(description)
|
logger.severe(description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (ignore: JSONException) {
|
} catch (ignore: JSONException) {
|
||||||
logger.severe("An error occurred parsing the error response from bitly.")
|
logger.severe("An error occurred parsing the error response from bitly.")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue