diff --git a/README.md b/README.md index f0a72a5..90ebaa7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # [Bitly](https://dev.bitly.com/v4/) Shortener for Kotlin/Java. -A pretty simple and straightforward implementation of the link shortening ([bitlinks](https://dev.bitly.com/v4/#tag/Bitlinks)) abilities of the [Bitly v4 API](https://dev.bitly.com/v4). +A simple implementation of the link shortening ([bitlinks](https://dev.bitly.com/v4/#tag/Bitlinks)) abilities of the [Bitly v4 API](https://dev.bitly.com/v4). ## Examples (TL;DR) @@ -17,7 +17,10 @@ bitly.bitlinks().shorten("https://erik.thauvin.net/blog") bitly.bitlinks().expand("http://bit.ly/380ojFd") // Clicks Summary -bitly.bitlinks().clicks("http://bit.ly/380ojFd") +bitly.bitlinks().clicks("http://bit.ly/380ojFd") + +// Create a bitlink +bitly.bitlinks().create(title = "Erik's Weblog", long_url = "http://erik.thauvin.net/blog/") ``` - View [Kotlin](https://github.com/ethauvin/bitly-shorten/blob/master/examples/src/main/kotlin/com/example/BitlyExample.kt) or [Java](https://github.com/ethauvin/bitly-shorten/blob/master/examples/src/main/java/com/example/BitlySample.java) Examples. diff --git a/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt b/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt index 27d63e3..e19f78a 100644 --- a/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt +++ b/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt @@ -86,6 +86,44 @@ open class Bitlinks(private val accessToken: String) { return clicks } + /** + * Converts a long url to a Bitlink and sets additional parameters. + * + * See the [Bit.ly API](https://dev.bitly.com/v4/#operation/createFullBitlink) for more information. + * + * @oaran long_url The long URL. + * @param toJson Returns the full JSON response if `true` + * @return The shorten URL or JSON response, or on error, an empty string/JSON object. + */ + @JvmOverloads + fun create( + domain: String = Constants.EMPTY, + title: String = Constants.EMPTY, + group_guid: String = Constants.EMPTY, + tags: Array = emptyArray(), + deeplinks: Array> = emptyArray(), + long_url: String, + toJson: Boolean = false + ): String { + var link = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY + if (long_url.isNotBlank()) { + val response = Utils.call( + accessToken, + "/bitlinks".toEndPoint(), + mutableMapOf(Pair("long_url", long_url)).apply { + if (domain.isNotBlank()) put("domain", domain) + if (title.isNotBlank()) put("title", title) + if (group_guid.isNotBlank()) put("group_guid", group_guid) + if (tags.isNotEmpty()) put("tags", tags) + if (deeplinks.isNotEmpty()) put("deeplinks", deeplinks) + }, + Methods.POST + ) + link = parseJsonResponse(response, "link", link, toJson) + } + return link + } + /** * Expands a Bitlink. * @@ -140,8 +178,6 @@ open class Bitlinks(private val accessToken: String) { * See the [Bit.ly API](https://dev.bitly.com/v4/#operation/createBitlink) for more information. * * @param long_url The long URL. - * @param group_guid The group UID. - * @param domain The domain for the short URL. * @param toJson Returns the full JSON response if `true` * @return The short URL or JSON response, or on error, the [long_url] or an empty JSON object. */ diff --git a/src/main/kotlin/net/thauvin/erik/bitly/Utils.kt b/src/main/kotlin/net/thauvin/erik/bitly/Utils.kt index 53ab79a..630d67a 100644 --- a/src/main/kotlin/net/thauvin/erik/bitly/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/bitly/Utils.kt @@ -65,7 +65,7 @@ open class Utils private constructor() { fun call( accessToken: String, endPoint: String, - params: Map = emptyMap(), + params: Map = emptyMap(), method: Methods = Methods.POST ): String { var response = Constants.EMPTY @@ -86,10 +86,12 @@ open class Utils private constructor() { } } Methods.DELETE -> Request.Builder().url(apiUrl.newBuilder().build()).delete() - else -> { + else -> { // Methods.GET val httpUrl = apiUrl.newBuilder().apply { params.forEach { - addQueryParameter(it.key, it.value) + if (it.value is String) { + addQueryParameter(it.key, it.value.toString()) + } } }.build() Request.Builder().url(httpUrl) @@ -104,15 +106,14 @@ open class Utils private constructor() { } private fun createHttpClient(): OkHttpClient { - return if (logger.isLoggable(Level.FINE)) { - val httpLoggingInterceptor = HttpLoggingInterceptor().apply { - level = HttpLoggingInterceptor.Level.BODY - redactHeader("Authorization") + return OkHttpClient.Builder().apply { + if (logger.isLoggable(Level.FINE)) { + addInterceptor(HttpLoggingInterceptor().apply { + level = HttpLoggingInterceptor.Level.BODY + redactHeader("Authorization") + }) } - OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build() - } else { - OkHttpClient.Builder().build() - } + }.build() } private fun parseBody(endPoint: String, result: Response): String { diff --git a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt index 21602c0..2f7967c 100644 --- a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt +++ b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt @@ -124,4 +124,9 @@ class BitlyTest { fun `clicks summary`() { assertNotEquals(Constants.EMPTY, bitly.bitlinks().clicks(shortUrl)) } + + @Test + fun `create bitlink`() { + assertEquals(shortUrl, bitly.bitlinks().create(domain = "bit.ly", title = "Erik's Weblog", tags = arrayOf("erik", "thauvin", "blog", "weblog"), long_url = longUrl)) + } }