Implemented IsgdException. Closes #1
This commit is contained in:
parent
5b64333e8a
commit
a55fddb77b
9 changed files with 136 additions and 15 deletions
10
.github/workflows/gradle.yml
vendored
10
.github/workflows/gradle.yml
vendored
|
@ -5,22 +5,28 @@ on: [push, pull_request, workflow_dispatch]
|
|||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
env:
|
||||
GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m"
|
||||
SONAR_JDK: "11"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
java-version: [ 1.8, 11, 15 ]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up JDK ${{ matrix.java-version }}
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: ${{ matrix.java-version }}
|
||||
|
||||
- name: Grant execute permission for gradlew
|
||||
run: chmod +x gradlew
|
||||
|
||||
- name: Cache SonarCloud packages
|
||||
if: matrix.java-version == env.SONAR_JDK
|
||||
uses: actions/cache@v1
|
||||
|
@ -28,6 +34,7 @@ jobs:
|
|||
path: ~/.sonar/cache
|
||||
key: ${{ runner.os }}-sonar
|
||||
restore-keys: ${{ runner.os }}-sonar
|
||||
|
||||
- name: Cache Gradle packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
|
@ -37,14 +44,17 @@ jobs:
|
|||
key: ${{ runner.os }}-gradle-${{ matrix.java-version }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-gradle-${{ matrix.java-version }}-
|
||||
|
||||
- name: Test with Gradle
|
||||
run: ./gradlew build check --stacktrace
|
||||
|
||||
- name: SonarCloud
|
||||
if: success() && matrix.java-version == env.SONAR_JDK
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
run: ./gradlew sonarqube
|
||||
|
||||
- name: Cleanup Gradle Cache
|
||||
run: |
|
||||
rm -f ~/.gradle/caches/modules-2/modules-2.lock
|
||||
|
|
18
README.md
18
README.md
|
@ -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).
|
||||
|
||||
### 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
|
||||
|
||||
Additionally, link can be shortened using [v.gd](https://v.gd/) by setting the `isVgd` flag:
|
||||
|
|
|
@ -3,5 +3,7 @@
|
|||
<ManuallySuppressedIssues/>
|
||||
<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>MagicNumber:Isgd.kt$Isgd.Companion$200</ID>
|
||||
<ID>MagicNumber:Isgd.kt$Isgd.Companion$399</ID>
|
||||
</CurrentIssues>
|
||||
</SmellBaseline>
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
package com.example;
|
||||
|
||||
import net.thauvin.erik.isgd.Isgd;
|
||||
import net.thauvin.erik.isgd.IsgdException;
|
||||
|
||||
public final class IsgdSample {
|
||||
public static void main(final String[] args) {
|
||||
if (args.length > 0) {
|
||||
for (final String arg : args) {
|
||||
try {
|
||||
if (arg.contains("is.gd")) {
|
||||
System.out.println(arg + " <-- " + Isgd.lookup(arg));
|
||||
} else {
|
||||
System.out.println(arg + " --> " + Isgd.shorten(arg));
|
||||
}
|
||||
} catch (IsgdException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.err.println("Try specifying one or more URLs as arguments.");
|
||||
|
|
|
@ -1,15 +1,20 @@
|
|||
package com.example
|
||||
|
||||
import net.thauvin.erik.isgd.Isgd
|
||||
import net.thauvin.erik.isgd.IsgdException
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.isNotEmpty()) {
|
||||
args.forEach {
|
||||
try {
|
||||
if (it.contains("is.gd"))
|
||||
println(it + " <-- " + Isgd.lookup(it))
|
||||
else
|
||||
println(it + " --> " + Isgd.shorten(it))
|
||||
} catch (e: IsgdException) {
|
||||
println(e.message)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println("Try specifying one or more URLs as arguments.")
|
||||
|
|
13
pom.xml
13
pom.xml
|
@ -35,11 +35,22 @@
|
|||
<system>GitHub</system>
|
||||
<url>https://github.com/ethauvin/isgd-shorten/issues</url>
|
||||
</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>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<version>1.5.10</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
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()}")
|
||||
|
|
45
src/main/kotlin/net/thauvin/erik/isgd/IsgdException.kt
Normal file
45
src/main/kotlin/net/thauvin/erik/isgd/IsgdException.kt
Normal 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
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue