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

@ -5,22 +5,28 @@ on: [push, pull_request, workflow_dispatch]
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m" GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m"
SONAR_JDK: "11" SONAR_JDK: "11"
strategy: strategy:
matrix: matrix:
java-version: [ 1.8, 11, 15 ] java-version: [ 1.8, 11, 15 ]
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Set up JDK ${{ matrix.java-version }} - name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v1 uses: actions/setup-java@v1
with: with:
java-version: ${{ matrix.java-version }} java-version: ${{ matrix.java-version }}
- name: Grant execute permission for gradlew - name: Grant execute permission for gradlew
run: chmod +x gradlew run: chmod +x gradlew
- name: Cache SonarCloud packages - name: Cache SonarCloud packages
if: matrix.java-version == env.SONAR_JDK if: matrix.java-version == env.SONAR_JDK
uses: actions/cache@v1 uses: actions/cache@v1
@ -28,6 +34,7 @@ jobs:
path: ~/.sonar/cache path: ~/.sonar/cache
key: ${{ runner.os }}-sonar key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages - name: Cache Gradle packages
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
@ -37,14 +44,17 @@ jobs:
key: ${{ runner.os }}-gradle-${{ matrix.java-version }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} key: ${{ runner.os }}-gradle-${{ matrix.java-version }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: | restore-keys: |
${{ runner.os }}-gradle-${{ matrix.java-version }}- ${{ runner.os }}-gradle-${{ matrix.java-version }}-
- name: Test with Gradle - name: Test with Gradle
run: ./gradlew build check --stacktrace run: ./gradlew build check --stacktrace
- name: SonarCloud - name: SonarCloud
if: success() && matrix.java-version == env.SONAR_JDK if: success() && matrix.java-version == env.SONAR_JDK
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: ./gradlew sonarqube run: ./gradlew sonarqube
- name: Cleanup Gradle Cache - name: Cleanup Gradle Cache
run: | run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock rm -f ~/.gradle/caches/modules-2/modules-2.lock

View file

@ -58,6 +58,24 @@ dependencies {
``` ```
Instructions for using with Maven, Ivy, etc. can be found on [Maven Central](https://search.maven.org/artifact/net.thauvin.erik/isgd-shorten/0.9.2/jar). Instructions for using with Maven, Ivy, etc. can be found on [Maven Central](https://search.maven.org/artifact/net.thauvin.erik/isgd-shorten/0.9.2/jar).
### Errors
An `IsgdException` is thrown when an API error occurs. The error message (text, XML or JSON) and HTTP status code can be retrieved as follows:
```kotlin
try {
Isgd.shorten("http://is.gd/Pt2sET") // already shorten
} catch (e: IsgdException)
println("Status Code: ${e.statusCode}")
println("${e.message})
}
```
```
Status Code: 400
Error: Sorry, the URL you entered is on our internal blacklist. It may have been used abusively in the past, or it may link to another URL redirection service.
```
### v.gd ### v.gd
Additionally, link can be shortened using [v.gd](https://v.gd/) by setting the `isVgd` flag: Additionally, link can be shortened using [v.gd](https://v.gd/) by setting the `isVgd` flag:

View file

@ -3,5 +3,7 @@
<ManuallySuppressedIssues/> <ManuallySuppressedIssues/>
<CurrentIssues> <CurrentIssues>
<ID>LongParameterList:Isgd.kt$Isgd.Companion$( url: String, shorturl: String = "", callback: String = "", logstats: Boolean = false, format: Format = Format.SIMPLE, isVgd: Boolean = false )</ID> <ID>LongParameterList:Isgd.kt$Isgd.Companion$( url: String, shorturl: String = "", callback: String = "", logstats: Boolean = false, format: Format = Format.SIMPLE, isVgd: Boolean = false )</ID>
<ID>MagicNumber:Isgd.kt$Isgd.Companion$200</ID>
<ID>MagicNumber:Isgd.kt$Isgd.Companion$399</ID>
</CurrentIssues> </CurrentIssues>
</SmellBaseline> </SmellBaseline>

View file

@ -1,15 +1,20 @@
package com.example; package com.example;
import net.thauvin.erik.isgd.Isgd; import net.thauvin.erik.isgd.Isgd;
import net.thauvin.erik.isgd.IsgdException;
public final class IsgdSample { public final class IsgdSample {
public static void main(final String[] args) { public static void main(final String[] args) {
if (args.length > 0) { if (args.length > 0) {
for (final String arg : args) { for (final String arg : args) {
if (arg.contains("is.gd")) { try {
System.out.println(arg + " <-- " + Isgd.lookup(arg)); if (arg.contains("is.gd")) {
} else { System.out.println(arg + " <-- " + Isgd.lookup(arg));
System.out.println(arg + " --> " + Isgd.shorten(arg)); } else {
System.out.println(arg + " --> " + Isgd.shorten(arg));
}
} catch (IsgdException e) {
System.out.println(e.getMessage());
} }
} }
} else { } else {

View file

@ -1,15 +1,20 @@
package com.example package com.example
import net.thauvin.erik.isgd.Isgd import net.thauvin.erik.isgd.Isgd
import net.thauvin.erik.isgd.IsgdException
import kotlin.system.exitProcess import kotlin.system.exitProcess
fun main(args: Array<String>) { fun main(args: Array<String>) {
if (args.isNotEmpty()) { if (args.isNotEmpty()) {
args.forEach { args.forEach {
if (it.contains("is.gd")) try {
println(it + " <-- " + Isgd.lookup(it)) if (it.contains("is.gd"))
else println(it + " <-- " + Isgd.lookup(it))
println(it + " --> " + Isgd.shorten(it)) else
println(it + " --> " + Isgd.shorten(it))
} catch (e: IsgdException) {
println(e.message)
}
} }
} else { } else {
println("Try specifying one or more URLs as arguments.") println("Try specifying one or more URLs as arguments.")

13
pom.xml
View file

@ -35,11 +35,22 @@
<system>GitHub</system> <system>GitHub</system>
<url>https://github.com/ethauvin/isgd-shorten/issues</url> <url>https://github.com/ethauvin/isgd-shorten/issues</url>
</issueManagement> </issueManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>1.5.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId> <artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.5.0</version> <version>1.5.10</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -38,7 +38,7 @@ import java.net.URLEncoder
import java.nio.charset.StandardCharsets 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) { enum class Format(val type: String) {
WEB("web"), SIMPLE("simple"), XML("xml"), JSON("json") WEB("web"), SIMPLE("simple"), XML("xml"), JSON("json")
@ -48,18 +48,25 @@ fun String.encode(): String {
return URLEncoder.encode(this, StandardCharsets.UTF_8.name()) return URLEncoder.encode(this, StandardCharsets.UTF_8.name())
} }
/**
* Implements the [is.gd API](https://is.gd/developers.php).
*/
class Isgd private constructor() { class Isgd private constructor() {
companion object { companion object {
private fun callApi(url: String): String { private fun callApi(url: String): String {
val connection = URL(url).openConnection() as HttpURLConnection val connection = URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty( connection.setRequestProperty(
"User-Agent", "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" return if (isVgd) "v.gd" else "is.gd"
} }
@ -68,6 +75,7 @@ class Isgd private constructor() {
*/ */
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
@Throws(IsgdException::class)
fun lookup( fun lookup(
shorturl: String, shorturl: String,
callback: String = "", callback: String = "",
@ -78,7 +86,7 @@ class Isgd private constructor() {
throw IllegalArgumentException("Please specify a valid short URL to lookup.") 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()) { if (callback.isNotEmpty()) {
sb.append("&callback=${callback.encode()}") sb.append("&callback=${callback.encode()}")
@ -94,6 +102,7 @@ class Isgd private constructor() {
*/ */
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
@Throws(IsgdException::class)
fun shorten( fun shorten(
url: String, url: String,
shorturl: String = "", shorturl: String = "",
@ -106,7 +115,7 @@ class Isgd private constructor() {
throw IllegalArgumentException("Please enter a valid URL to shorten.") 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()) { if (shorturl.isNotEmpty()) {
sb.append("&shorturl=${shorturl.encode()}") 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.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue import kotlin.test.assertTrue
class IsgdTest { class IsgdTest {
@ -41,6 +42,21 @@ class IsgdTest {
private val shortUrl = "https://is.gd/Pt2sET" private val shortUrl = "https://is.gd/Pt2sET"
private val shortVgdUrl = "https://v.gd/2z2ncj" 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 @Test
fun testLookupDefault() { fun testLookupDefault() {
assertEquals(url, Isgd.lookup(shortUrl)) assertEquals(url, Isgd.lookup(shortUrl))