Added bitlink create method.
This commit is contained in:
parent
0aeb385238
commit
597b5516e1
4 changed files with 60 additions and 15 deletions
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
# [Bitly](https://dev.bitly.com/v4/) Shortener for Kotlin/Java.
|
# [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)
|
## Examples (TL;DR)
|
||||||
|
|
||||||
|
@ -17,7 +17,10 @@ bitly.bitlinks().shorten("https://erik.thauvin.net/blog")
|
||||||
bitly.bitlinks().expand("http://bit.ly/380ojFd")
|
bitly.bitlinks().expand("http://bit.ly/380ojFd")
|
||||||
|
|
||||||
// Clicks Summary
|
// 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.
|
- 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.
|
||||||
|
|
|
@ -86,6 +86,44 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
return clicks
|
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<String> = emptyArray(),
|
||||||
|
deeplinks: Array<Map<String, String>> = 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<String, Any>(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.
|
* 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.
|
* See the [Bit.ly API](https://dev.bitly.com/v4/#operation/createBitlink) for more information.
|
||||||
*
|
*
|
||||||
* @param long_url The long URL.
|
* @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`
|
* @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.
|
* @return The short URL or JSON response, or on error, the [long_url] or an empty JSON object.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -65,7 +65,7 @@ open class Utils private constructor() {
|
||||||
fun call(
|
fun call(
|
||||||
accessToken: String,
|
accessToken: String,
|
||||||
endPoint: String,
|
endPoint: String,
|
||||||
params: Map<String, String> = emptyMap(),
|
params: Map<String, Any> = emptyMap(),
|
||||||
method: Methods = Methods.POST
|
method: Methods = Methods.POST
|
||||||
): String {
|
): String {
|
||||||
var response = Constants.EMPTY
|
var response = Constants.EMPTY
|
||||||
|
@ -86,10 +86,12 @@ open class Utils private constructor() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Methods.DELETE -> Request.Builder().url(apiUrl.newBuilder().build()).delete()
|
Methods.DELETE -> Request.Builder().url(apiUrl.newBuilder().build()).delete()
|
||||||
else -> {
|
else -> { // Methods.GET
|
||||||
val httpUrl = apiUrl.newBuilder().apply {
|
val httpUrl = apiUrl.newBuilder().apply {
|
||||||
params.forEach {
|
params.forEach {
|
||||||
addQueryParameter(it.key, it.value)
|
if (it.value is String) {
|
||||||
|
addQueryParameter(it.key, it.value.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
Request.Builder().url(httpUrl)
|
Request.Builder().url(httpUrl)
|
||||||
|
@ -104,15 +106,14 @@ open class Utils private constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createHttpClient(): OkHttpClient {
|
private fun createHttpClient(): OkHttpClient {
|
||||||
return if (logger.isLoggable(Level.FINE)) {
|
return OkHttpClient.Builder().apply {
|
||||||
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
|
if (logger.isLoggable(Level.FINE)) {
|
||||||
level = HttpLoggingInterceptor.Level.BODY
|
addInterceptor(HttpLoggingInterceptor().apply {
|
||||||
redactHeader("Authorization")
|
level = HttpLoggingInterceptor.Level.BODY
|
||||||
|
redactHeader("Authorization")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
|
}.build()
|
||||||
} else {
|
|
||||||
OkHttpClient.Builder().build()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseBody(endPoint: String, result: Response): String {
|
private fun parseBody(endPoint: String, result: Response): String {
|
||||||
|
|
|
@ -124,4 +124,9 @@ class BitlyTest {
|
||||||
fun `clicks summary`() {
|
fun `clicks summary`() {
|
||||||
assertNotEquals(Constants.EMPTY, bitly.bitlinks().clicks(shortUrl))
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue