Implemented IsgdException. Closes #1

This commit is contained in:
Erik C. Thauvin 2021-06-05 12:39:58 -07:00
parent 5b64333e8a
commit a55fddb77b
9 changed files with 136 additions and 15 deletions

View file

@ -38,7 +38,7 @@ import java.net.URLEncoder
import java.nio.charset.StandardCharsets
/**
* See the [is.gd API](https://is.gd/apishorteningreference.php)
* See the [is.gd API](https://is.gd/apishorteningreference.php).
*/
enum class Format(val type: String) {
WEB("web"), SIMPLE("simple"), XML("xml"), JSON("json")
@ -48,18 +48,25 @@ fun String.encode(): String {
return URLEncoder.encode(this, StandardCharsets.UTF_8.name())
}
/**
* Implements the [is.gd API](https://is.gd/developers.php).
*/
class Isgd private constructor() {
companion object {
private fun callApi(url: String): String {
val connection = URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0"
"Mozilla/5.0 (Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0"
)
return connection.inputStream.bufferedReader().readText()
if (connection.responseCode in 200..399) {
return connection.inputStream.bufferedReader().readText()
} else {
throw IsgdException(connection.responseCode, connection.errorStream.bufferedReader().readText())
}
}
private fun host(isVgd: Boolean = false): String {
private fun getHost(isVgd: Boolean = false): String {
return if (isVgd) "v.gd" else "is.gd"
}
@ -68,6 +75,7 @@ class Isgd private constructor() {
*/
@JvmStatic
@JvmOverloads
@Throws(IsgdException::class)
fun lookup(
shorturl: String,
callback: String = "",
@ -78,7 +86,7 @@ class Isgd private constructor() {
throw IllegalArgumentException("Please specify a valid short URL to lookup.")
}
val sb = StringBuilder("https://${host(isVgd)}/forward.php?shorturl=${shorturl.encode()}")
val sb = StringBuilder("https://${getHost(isVgd)}/forward.php?shorturl=${shorturl.encode()}")
if (callback.isNotEmpty()) {
sb.append("&callback=${callback.encode()}")
@ -94,6 +102,7 @@ class Isgd private constructor() {
*/
@JvmStatic
@JvmOverloads
@Throws(IsgdException::class)
fun shorten(
url: String,
shorturl: String = "",
@ -106,7 +115,7 @@ class Isgd private constructor() {
throw IllegalArgumentException("Please enter a valid URL to shorten.")
}
val sb = StringBuilder("https://${host(isVgd)}/create.php?url=${url.encode()}")
val sb = StringBuilder("https://${getHost(isVgd)}/create.php?url=${url.encode()}")
if (shorturl.isNotEmpty()) {
sb.append("&shorturl=${shorturl.encode()}")

View file

@ -0,0 +1,45 @@
/*
* IsgdException.kt
*
* Copyright (c) 2020-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.isgd
/**
* Thrown when an exceptional condition has occurred.
*
* @property statusCode The HTTP status code.
* @property message The error message.
*/
class IsgdException(val statusCode: Int, message: String) : Exception(message) {
companion object {
private const val serialVersionUID = 1L
}
}

View file

@ -34,6 +34,7 @@ package net.thauvin.erik.isgd
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class IsgdTest {
@ -41,6 +42,21 @@ class IsgdTest {
private val shortUrl = "https://is.gd/Pt2sET"
private val shortVgdUrl = "https://v.gd/2z2ncj"
@Test
fun testException() {
assertFailsWith(
message = "URL is already shorten",
exceptionClass = IsgdException::class,
block = { Isgd.shorten(shortUrl) }
)
try {
Isgd.shorten(shortUrl)
} catch (e: IsgdException) {
assertTrue(e.statusCode == 400, "status code == 400")
}
}
@Test
fun testLookupDefault() {
assertEquals(url, Isgd.lookup(shortUrl))