Cleanup.
This commit is contained in:
parent
c0fc862bb4
commit
191b09060b
6 changed files with 132 additions and 96 deletions
9
.idea/bitly-shorten.iml
generated
Normal file
9
.idea/bitly-shorten.iml
generated
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/bitly-shorten.iml" filepath="$PROJECT_DIR$/.idea/bitly-shorten.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -69,25 +69,25 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun clicks(
|
fun clicks(
|
||||||
bitlink: String,
|
bitlink: String,
|
||||||
unit: Units = Units.DAY,
|
unit: Units = Units.DAY,
|
||||||
units: Int = -1,
|
units: Int = -1,
|
||||||
size: Int = 50,
|
size: Int = 50,
|
||||||
unit_reference: String = Constants.EMPTY,
|
unit_reference: String = Constants.EMPTY,
|
||||||
toJson: Boolean = false
|
toJson: Boolean = false
|
||||||
): String {
|
): String {
|
||||||
var clicks = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
|
var clicks = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
|
||||||
if (bitlink.isNotBlank()) {
|
if (bitlink.isNotBlank()) {
|
||||||
lastCallResponse = Utils.call(
|
lastCallResponse = Utils.call(
|
||||||
accessToken,
|
accessToken,
|
||||||
("/bitlinks/${bitlink.removeHttp()}/clicks/summary").toEndPoint(),
|
("/bitlinks/${bitlink.removeHttp()}/clicks/summary").toEndPoint(),
|
||||||
mapOf(
|
mapOf(
|
||||||
"unit" to unit.toString().lowercase(),
|
"unit" to unit.toString().lowercase(),
|
||||||
"units" to units.toString(),
|
"units" to units.toString(),
|
||||||
"size" to size.toString(),
|
"size" to size.toString(),
|
||||||
"unit_reference" to unit_reference
|
"unit_reference" to unit_reference
|
||||||
),
|
),
|
||||||
Methods.GET
|
Methods.GET
|
||||||
)
|
)
|
||||||
clicks = parseJsonResponse(lastCallResponse, "total_clicks", clicks, toJson)
|
clicks = parseJsonResponse(lastCallResponse, "total_clicks", clicks, toJson)
|
||||||
}
|
}
|
||||||
|
@ -108,26 +108,26 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun create(
|
fun create(
|
||||||
domain: String = Constants.EMPTY,
|
domain: String = Constants.EMPTY,
|
||||||
title: String = Constants.EMPTY,
|
title: String = Constants.EMPTY,
|
||||||
group_guid: String = Constants.EMPTY,
|
group_guid: String = Constants.EMPTY,
|
||||||
tags: Array<String> = emptyArray(),
|
tags: Array<String> = emptyArray(),
|
||||||
deeplinks: Array<Map<String, String>> = emptyArray(),
|
deeplinks: Array<Map<String, String>> = emptyArray(),
|
||||||
long_url: String,
|
long_url: String,
|
||||||
toJson: Boolean = false
|
toJson: Boolean = false
|
||||||
): String {
|
): String {
|
||||||
var link = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
|
var link = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
|
||||||
if (long_url.isNotBlank()) {
|
if (long_url.isNotBlank()) {
|
||||||
lastCallResponse = Utils.call(
|
lastCallResponse = Utils.call(
|
||||||
accessToken,
|
accessToken,
|
||||||
"/bitlinks".toEndPoint(),
|
"/bitlinks".toEndPoint(),
|
||||||
mutableMapOf<String, Any>("long_url" to long_url).apply {
|
mutableMapOf<String, Any>("long_url" to long_url).apply {
|
||||||
if (domain.isNotBlank()) put("domain", domain)
|
if (domain.isNotBlank()) put("domain", domain)
|
||||||
if (title.isNotBlank()) put("title", title)
|
if (title.isNotBlank()) put("title", title)
|
||||||
if (group_guid.isNotBlank()) put("group_guid", group_guid)
|
if (group_guid.isNotBlank()) put("group_guid", group_guid)
|
||||||
if (tags.isNotEmpty()) put("tags", tags)
|
if (tags.isNotEmpty()) put("tags", tags)
|
||||||
if (deeplinks.isNotEmpty()) put("deeplinks", deeplinks)
|
if (deeplinks.isNotEmpty()) put("deeplinks", deeplinks)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
link = parseJsonResponse(lastCallResponse, "link", link, toJson)
|
link = parseJsonResponse(lastCallResponse, "link", link, toJson)
|
||||||
}
|
}
|
||||||
|
@ -149,9 +149,9 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
var longUrl = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
|
var longUrl = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
|
||||||
if (bitlink_id.isNotBlank()) {
|
if (bitlink_id.isNotBlank()) {
|
||||||
lastCallResponse = Utils.call(
|
lastCallResponse = Utils.call(
|
||||||
accessToken,
|
accessToken,
|
||||||
"/expand".toEndPoint(),
|
"/expand".toEndPoint(),
|
||||||
mapOf("bitlink_id" to bitlink_id.removeHttp())
|
mapOf("bitlink_id" to bitlink_id.removeHttp())
|
||||||
)
|
)
|
||||||
longUrl = parseJsonResponse(lastCallResponse, "long_url", longUrl, toJson)
|
longUrl = parseJsonResponse(lastCallResponse, "long_url", longUrl, toJson)
|
||||||
}
|
}
|
||||||
|
@ -198,10 +198,10 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun shorten(
|
fun shorten(
|
||||||
long_url: String,
|
long_url: String,
|
||||||
group_guid: String = Constants.EMPTY,
|
group_guid: String = Constants.EMPTY,
|
||||||
domain: String = Constants.EMPTY,
|
domain: String = Constants.EMPTY,
|
||||||
toJson: Boolean = false
|
toJson: Boolean = false
|
||||||
): String {
|
): String {
|
||||||
var bitlink = if (toJson) Constants.EMPTY_JSON else long_url
|
var bitlink = if (toJson) Constants.EMPTY_JSON else long_url
|
||||||
if (!long_url.isValidUrl()) {
|
if (!long_url.isValidUrl()) {
|
||||||
|
@ -234,39 +234,39 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun update(
|
fun update(
|
||||||
bitlink: String,
|
bitlink: String,
|
||||||
references: Map<String, String> = emptyMap(),
|
references: Map<String, String> = emptyMap(),
|
||||||
archived: Boolean = false,
|
archived: Boolean = false,
|
||||||
tags: Array<String> = emptyArray(),
|
tags: Array<String> = emptyArray(),
|
||||||
created_at: String = Constants.EMPTY,
|
created_at: String = Constants.EMPTY,
|
||||||
title: String = Constants.EMPTY,
|
title: String = Constants.EMPTY,
|
||||||
deeplinks: Array<Map<String, String>> = emptyArray(),
|
deeplinks: Array<Map<String, String>> = emptyArray(),
|
||||||
created_by: String = Constants.EMPTY,
|
created_by: String = Constants.EMPTY,
|
||||||
long_url: String = Constants.EMPTY,
|
long_url: String = Constants.EMPTY,
|
||||||
client_id: String = Constants.EMPTY,
|
client_id: String = Constants.EMPTY,
|
||||||
custom_bitlinks: Array<String> = emptyArray(),
|
custom_bitlinks: Array<String> = emptyArray(),
|
||||||
link: String = Constants.EMPTY,
|
link: String = Constants.EMPTY,
|
||||||
id: String = Constants.EMPTY,
|
id: String = Constants.EMPTY,
|
||||||
toJson: Boolean = false
|
toJson: Boolean = false
|
||||||
): String {
|
): String {
|
||||||
var result = if (toJson) Constants.EMPTY_JSON else Constants.FALSE
|
var result = if (toJson) Constants.EMPTY_JSON else Constants.FALSE
|
||||||
if (bitlink.isNotBlank()) {
|
if (bitlink.isNotBlank()) {
|
||||||
lastCallResponse = Utils.call(
|
lastCallResponse = Utils.call(
|
||||||
accessToken, "/bitlinks/${bitlink.removeHttp()}".toEndPoint(), mutableMapOf<String, Any>().apply {
|
accessToken, "/bitlinks/${bitlink.removeHttp()}".toEndPoint(), mutableMapOf<String, Any>().apply {
|
||||||
if (references.isNotEmpty()) put("references", references)
|
if (references.isNotEmpty()) put("references", references)
|
||||||
if (archived) put("archived", archived)
|
if (archived) put("archived", archived)
|
||||||
if (tags.isNotEmpty()) put("tags", tags)
|
if (tags.isNotEmpty()) put("tags", tags)
|
||||||
if (created_at.isNotBlank()) put("created_at", created_at)
|
if (created_at.isNotBlank()) put("created_at", created_at)
|
||||||
if (title.isNotBlank()) put("title", title)
|
if (title.isNotBlank()) put("title", title)
|
||||||
if (deeplinks.isNotEmpty()) put("deeplinks", deeplinks)
|
if (deeplinks.isNotEmpty()) put("deeplinks", deeplinks)
|
||||||
if (created_by.isNotBlank()) put("created_by", created_by)
|
if (created_by.isNotBlank()) put("created_by", created_by)
|
||||||
if (long_url.isNotBlank()) put("long_url", long_url)
|
if (long_url.isNotBlank()) put("long_url", long_url)
|
||||||
if (client_id.isNotBlank()) put("client_id", client_id)
|
if (client_id.isNotBlank()) put("client_id", client_id)
|
||||||
if (custom_bitlinks.isNotEmpty()) put("custom_bitlinks", custom_bitlinks)
|
if (custom_bitlinks.isNotEmpty()) put("custom_bitlinks", custom_bitlinks)
|
||||||
if (link.isNotBlank()) put("link", link)
|
if (link.isNotBlank()) put("link", link)
|
||||||
if (id.isNotBlank()) put("id", id)
|
if (id.isNotBlank()) put("id", id)
|
||||||
},
|
},
|
||||||
Methods.PATCH
|
Methods.PATCH
|
||||||
)
|
)
|
||||||
|
|
||||||
if (lastCallResponse.isSuccessful) {
|
if (lastCallResponse.isSuccessful) {
|
||||||
|
|
|
@ -49,7 +49,7 @@ open class Bitly() {
|
||||||
* [Authentication](https://dev.bitly.com/docs/getting-started/authentication).
|
* [Authentication](https://dev.bitly.com/docs/getting-started/authentication).
|
||||||
**/
|
**/
|
||||||
var accessToken: String = System.getenv(Constants.ENV_ACCESS_TOKEN)
|
var accessToken: String = System.getenv(Constants.ENV_ACCESS_TOKEN)
|
||||||
?: (System.getProperty(Constants.ENV_ACCESS_TOKEN) ?: Constants.EMPTY)
|
?: (System.getProperty(Constants.ENV_ACCESS_TOKEN) ?: Constants.EMPTY)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance using an [API Access Token][accessToken].
|
* Creates a new instance using an [API Access Token][accessToken].
|
||||||
|
|
|
@ -50,7 +50,7 @@ import java.util.logging.Logger
|
||||||
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.name) }
|
val logger: Logger by lazy { Logger.getLogger(Utils::class.java.name) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes an API call.
|
* Executes an API call.
|
||||||
|
@ -70,8 +70,7 @@ open class Utils private constructor() {
|
||||||
): CallResponse {
|
): CallResponse {
|
||||||
val response = CallResponse()
|
val response = CallResponse()
|
||||||
if (validateCall(accessToken, endPoint)) {
|
if (validateCall(accessToken, endPoint)) {
|
||||||
val apiUrl = endPoint.toHttpUrlOrNull()
|
endPoint.toHttpUrlOrNull()?.let { apiUrl ->
|
||||||
if (apiUrl != null) {
|
|
||||||
val builder = when (method) {
|
val builder = when (method) {
|
||||||
Methods.POST, Methods.PATCH -> {
|
Methods.POST, Methods.PATCH -> {
|
||||||
val formBody = JSONObject(params).toString()
|
val formBody = JSONObject(params).toString()
|
||||||
|
@ -117,8 +116,7 @@ open class Utils private constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseBody(endPoint: String, result: Response): String {
|
private fun parseBody(endPoint: String, result: Response): String {
|
||||||
val body = result.body?.string()
|
result.body?.string()?.let { body ->
|
||||||
if (body != null) {
|
|
||||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
with(JSONObject(body)) {
|
with(JSONObject(body)) {
|
||||||
|
|
|
@ -32,16 +32,14 @@
|
||||||
|
|
||||||
package net.thauvin.erik.bitly
|
package net.thauvin.erik.bitly
|
||||||
|
|
||||||
|
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
||||||
import org.json.JSONObject
|
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
|
||||||
import kotlin.test.Test
|
import kotlin.test.*
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertNotEquals
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class BitlyTest {
|
class BitlyTest {
|
||||||
private val bitly = with(File("local.properties")) {
|
private val bitly = with(File("local.properties")) {
|
||||||
|
@ -74,8 +72,8 @@ class BitlyTest {
|
||||||
fun `token should be valid`() {
|
fun `token should be valid`() {
|
||||||
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", toJson = true)
|
test.bitlinks().shorten("https://erik.thauvin.net/blog", toJson = true)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,6 +87,11 @@ class BitlyTest {
|
||||||
assertEquals(shortUrl, bitly.bitlinks().shorten(shortUrl))
|
assertEquals(shortUrl, bitly.bitlinks().shorten(shortUrl))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `endPoint should be specified`() {
|
||||||
|
assertFalse(bitly.call("").isSuccessful)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `shorten = expand`() {
|
fun `shorten = expand`() {
|
||||||
val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly")
|
val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly")
|
||||||
|
@ -102,19 +105,23 @@ class BitlyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `get user`() {
|
fun `get user`() {
|
||||||
assertTrue(bitly.call("/user".toEndPoint(), method = Methods.GET).isSuccessful)
|
assertTrue(bitly.call("user".toEndPoint(), method = Methods.GET).isSuccessful)
|
||||||
|
assertTrue(
|
||||||
|
Utils.call(bitly.accessToken, "/user".toEndPoint(), method = Methods.GET).isSuccessful,
|
||||||
|
"call(/user)"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `created by`() {
|
fun `created by`() {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"ethauvin",
|
"ethauvin",
|
||||||
JSONObject(
|
JSONObject(
|
||||||
bitly.call(
|
bitly.call(
|
||||||
"/bitlinks/${shortUrl.removeHttp()}".toEndPoint(),
|
"/bitlinks/${shortUrl.removeHttp()}".toEndPoint(),
|
||||||
method = Methods.GET
|
method = Methods.GET
|
||||||
).body
|
).body
|
||||||
).getString("created_by")
|
).getString("created_by")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,14 +161,18 @@ class BitlyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `create bitlink`() {
|
fun `create bitlink`() {
|
||||||
|
assertTrue(
|
||||||
|
bitly.bitlinks().create(long_url = longUrl).matches("https://\\w+.\\w{2}/\\w{7}".toRegex()),
|
||||||
|
"just long_url"
|
||||||
|
)
|
||||||
assertEquals(
|
assertEquals(
|
||||||
shortUrl,
|
shortUrl,
|
||||||
bitly.bitlinks().create(
|
bitly.bitlinks().create(
|
||||||
domain = "bit.ly",
|
domain = "bit.ly",
|
||||||
title = "Erik's Blog",
|
title = "Erik's Blog",
|
||||||
tags = arrayOf("erik", "thauvin", "blog", "weblog"),
|
tags = arrayOf("erik", "thauvin", "blog", "weblog"),
|
||||||
long_url = longUrl
|
long_url = longUrl
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,11 +180,21 @@ class BitlyTest {
|
||||||
fun `update bitlink`() {
|
fun `update bitlink`() {
|
||||||
val bl = bitly.bitlinks()
|
val bl = bitly.bitlinks()
|
||||||
assertEquals(
|
assertEquals(
|
||||||
Constants.TRUE,
|
Constants.TRUE,
|
||||||
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
|
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
|
||||||
|
)
|
||||||
|
|
||||||
|
assertTrue(
|
||||||
|
bl.update(shortUrl, tags = emptyArray(), toJson = true).contains("\"tags\":[]"), "update tags toJson"
|
||||||
)
|
)
|
||||||
|
|
||||||
bl.update(shortUrl, link = longUrl)
|
bl.update(shortUrl, link = longUrl)
|
||||||
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
|
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `validate URL`() {
|
||||||
|
assertTrue("https://www.example.com".isValidUrl(), "valid url")
|
||||||
|
assertFalse("this is a test".isValidUrl(), "invalid url")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue