This commit is contained in:
Erik C. Thauvin 2020-02-27 00:42:09 -08:00
parent 7cfd200138
commit 06ea6b91a2
3 changed files with 19 additions and 13 deletions

View file

@ -48,8 +48,8 @@ class Bitlinks(private val accessToken: String) {
* See the [Bit.ly API](https://dev.bitly.com/v4/#operation/expandBitlink) for more information. * See the [Bit.ly API](https://dev.bitly.com/v4/#operation/expandBitlink) for more information.
* *
* @param bitlink_id The bitlink ID. * @param bitlink_id The bitlink ID.
* @param isJson Returns the full JSON API response if `true` * @param isJson Returns the full JSON response if `true`
* @return THe long URL or JSON API response. * @return The long URL or JSON response, or on error, an empty string/JSON object.
*/ */
@JvmOverloads @JvmOverloads
fun expand(bitlink_id: String, isJson: Boolean = false): String { fun expand(bitlink_id: String, isJson: Boolean = false): String {
@ -81,9 +81,9 @@ class Bitlinks(private val accessToken: String) {
* *
* @param long_url The long URL. * @param long_url The long URL.
* @param group_guid The group UID. * @param group_guid The group UID.
* @param domain The domain for the short URL, defaults to `bit.ly`. * @param domain The domain for the short URL.
* @param isJson Returns the full JSON API response if `true` * @param isJson Returns the full JSON response if `true`
* @return THe short URL or JSON API response. * @return The short URL or JSON response, or on error, the [long_url] or an empty JSON object.
*/ */
@JvmOverloads @JvmOverloads
fun shorten( fun shorten(

View file

@ -50,11 +50,16 @@ enum class Methods {
* @constructor Creates new instance. * @constructor Creates new instance.
*/ */
open class Bitly() { open class Bitly() {
/** The API access token. **/ /** The API access token.
var accessToken: String = System.getenv(Constants.ENV_ACCESS_TOKEN) ?: Constants.EMPTY *
* See [Generic Access Token](https://bitly.is/accesstoken) or
* [Authentication](https://dev.bitly.com/v4/#section/Authentication).
**/
var accessToken: String = System.getenv(Constants.ENV_ACCESS_TOKEN)
?: (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][Bitly.accessToken].
* *
* @param accessToken The API access token. * @param accessToken The API access token.
*/ */

View file

@ -47,7 +47,8 @@ class BitlyTest {
Bitly() Bitly()
} }
} }
private val blog = "https://erik.thauvin.net/blog" private val longUrl = "https://erik.thauvin.net/blog"
private val shortUrl = "http://bit.ly/380ojFd"
@Before @Before
fun before() { fun before() {
@ -62,7 +63,7 @@ class BitlyTest {
if (System.getenv("CI") == "true") { if (System.getenv("CI") == "true") {
test.accessToken = Constants.EMPTY test.accessToken = Constants.EMPTY
} }
assertEquals(Constants.EMPTY, test.bitlinks().shorten(blog)) assertEquals(longUrl, test.bitlinks().shorten(longUrl))
} }
@Test @Test
@ -78,13 +79,13 @@ class BitlyTest {
@Test @Test
fun `shorten = expand`() { fun `shorten = expand`() {
val shortUrl = bitly.bitlinks().shorten(blog, domain = "bit.ly") val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly")
assertEquals(blog, bitly.bitlinks().expand(shortUrl)) assertEquals(longUrl, bitly.bitlinks().expand(shortUrl))
} }
@Test @Test
fun `as json`() { fun `as json`() {
assertTrue(bitly.bitlinks().shorten(blog, isJson = true).startsWith("{\"created_at\":")) assertTrue(bitly.bitlinks().shorten(longUrl, isJson = true).startsWith("{\"created_at\":"))
} }
@Test @Test