Converted string functions to extensions.
This commit is contained in:
parent
c18ec9f022
commit
b8cec20991
2 changed files with 53 additions and 34 deletions
|
@ -49,22 +49,9 @@ import java.util.logging.Logger
|
||||||
/** Useful functions. */
|
/** Useful functions. */
|
||||||
open class Utils private constructor() {
|
open class Utils private constructor() {
|
||||||
companion object {
|
companion object {
|
||||||
/** The logger instance. **/
|
/** The logger instance. */
|
||||||
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.simpleName) }
|
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.simpleName) }
|
||||||
|
|
||||||
/**
|
|
||||||
* Builds the full API endpoint URL using the [Constants.API_BASE_URL].
|
|
||||||
*
|
|
||||||
* @param endPointPath The REST request path. (eg. `/shorten', '/user`)
|
|
||||||
*/
|
|
||||||
fun buildEndPointUrl(endPointPath: String): String {
|
|
||||||
return if (endPointPath.startsWith('/')) {
|
|
||||||
"${Constants.API_BASE_URL}$endPointPath"
|
|
||||||
} else {
|
|
||||||
"${Constants.API_BASE_URL}/$endPointPath"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes an API call.
|
* Executes an API call.
|
||||||
*
|
*
|
||||||
|
@ -74,10 +61,11 @@ open class Utils private constructor() {
|
||||||
* @param method The submission [Method][Methods].
|
* @param method The submission [Method][Methods].
|
||||||
* @return The response (JSON) from the API.
|
* @return The response (JSON) from the API.
|
||||||
*/
|
*/
|
||||||
|
@JvmOverloads
|
||||||
fun call(
|
fun call(
|
||||||
accessToken: String,
|
accessToken: String,
|
||||||
endPoint: String,
|
endPoint: String,
|
||||||
params: Map<String, String>,
|
params: Map<String, String> = emptyMap(),
|
||||||
method: Methods = Methods.POST
|
method: Methods = Methods.POST
|
||||||
): String {
|
): String {
|
||||||
var response = Constants.EMPTY
|
var response = Constants.EMPTY
|
||||||
|
@ -153,6 +141,39 @@ open class Utils private constructor() {
|
||||||
return Constants.EMPTY
|
return Constants.EMPTY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates a URL.
|
||||||
|
*/
|
||||||
|
fun String.isValidUrl(): Boolean {
|
||||||
|
if (this.isNotBlank()) {
|
||||||
|
try {
|
||||||
|
URL(this)
|
||||||
|
return true
|
||||||
|
} catch (e: MalformedURLException) {
|
||||||
|
logger.log(Level.FINE, "Invalid URL: $this", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes http(s) scheme from string.
|
||||||
|
*/
|
||||||
|
fun String.removeHttp(): String {
|
||||||
|
return this.replaceFirst(Regex("^[Hh][Tt]{2}[Pp][Ss]?://"), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the full API endpoint URL using the [Constants.API_BASE_URL].
|
||||||
|
*/
|
||||||
|
fun String.toEndPoint(): String {
|
||||||
|
return if (this.startsWith('/')) {
|
||||||
|
"${Constants.API_BASE_URL}$this"
|
||||||
|
} else {
|
||||||
|
"${Constants.API_BASE_URL}/$this"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun validateCall(accessToken: String, endPoint: String): Boolean {
|
private fun validateCall(accessToken: String, endPoint: String): Boolean {
|
||||||
when {
|
when {
|
||||||
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
|
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
|
||||||
|
@ -161,20 +182,5 @@ open class Utils private constructor() {
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates a URL.
|
|
||||||
*/
|
|
||||||
fun validateUrl(url: String): Boolean {
|
|
||||||
if (url.isNotBlank()) {
|
|
||||||
try {
|
|
||||||
URL(url)
|
|
||||||
return true
|
|
||||||
} catch (e: MalformedURLException) {
|
|
||||||
logger.log(Level.FINE, "Invalid URL: $url", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,9 @@
|
||||||
|
|
||||||
package net.thauvin.erik.bitly
|
package net.thauvin.erik.bitly
|
||||||
|
|
||||||
|
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
||||||
|
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
||||||
|
import org.json.JSONObject
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.logging.Level
|
import java.util.logging.Level
|
||||||
|
@ -72,7 +75,7 @@ class BitlyTest {
|
||||||
val test = Bitly().apply { accessToken = "12345679" }
|
val test = Bitly().apply { accessToken = "12345679" }
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"{\"message\":\"FORBIDDEN\"}",
|
"{\"message\":\"FORBIDDEN\"}",
|
||||||
test.bitlinks().shorten("https://erik.thauvin.net/blog", isJson = true)
|
test.bitlinks().shorten("https://erik.thauvin.net/blog", toJson = true)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,12 +92,22 @@ class BitlyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `as json`() {
|
fun `as json`() {
|
||||||
assertTrue(bitly.bitlinks().shorten(longUrl, isJson = true).startsWith("{\"created_at\":"))
|
assertTrue(bitly.bitlinks().shorten(longUrl, toJson = true).startsWith("{\"created_at\":"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `get user`() {
|
fun `get user`() {
|
||||||
assertTrue(bitly.call(Utils.buildEndPointUrl("user"), emptyMap(), Methods.GET).contains("\"login\":"))
|
assertTrue(bitly.call("/user".toEndPoint(), method = Methods.GET).contains("\"login\":"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `created by`() {
|
||||||
|
assertEquals(
|
||||||
|
"ethauvin",
|
||||||
|
JSONObject(
|
||||||
|
bitly.call("/bitlinks/${shortUrl.removeHttp()}".toEndPoint(), method = Methods.GET)
|
||||||
|
).getString("created_by")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -109,6 +122,6 @@ class BitlyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `clicks summary`() {
|
fun `clicks summary`() {
|
||||||
assertNotEquals(Constants.EMPTY, bitly.bitlinks().Clicks().summary(shortUrl))
|
assertNotEquals(Constants.EMPTY, bitly.bitlinks().clicks(shortUrl))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue