From 517aeb016af122ff6274684f4119b8addde2fd0c Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 18 May 2024 17:04:53 -0700 Subject: [PATCH] Implement custom bitlink update. Closes #15 --- .idea/misc.xml | 16 +----- README.md | 8 ++- config/detekt/baseline.xml | 40 ++++----------- .../bld/java/com/example/ExampleBuild.java | 2 +- examples/gradle/build.gradle.kts | 2 +- .../thauvin/erik/bitly/BitlyShortenBuild.java | 2 +- .../kotlin/net/thauvin/erik/bitly/Bitlinks.kt | 38 ++++++++++++++ src/test/kotlin/DisableOnCi.kt | 43 ++++++++++++++++ src/test/kotlin/DisableOnCiCondition.kt | 50 +++++++++++++++++++ .../net/thauvin/erik/bitly/BitlyTest.kt | 11 ++++ 10 files changed, 160 insertions(+), 52 deletions(-) create mode 100644 src/test/kotlin/DisableOnCi.kt create mode 100644 src/test/kotlin/DisableOnCiCondition.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index d5f4077..f40d83c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,21 +8,7 @@ - - - - \ No newline at end of file + diff --git a/README.md b/README.md index fd2f043..5224299 100644 --- a/README.md +++ b/README.md @@ -90,18 +90,16 @@ Instructions for using with Maven, Ivy, etc. can be found on [Maven Central](htt To make it easier to use the library with Java, configuration builders are available: ```java -var config = new CreateConfig.Builder() +var config = new CreateConfig.Builder("https://erik.thauvin.net/blog") .title("Erik's Weblog") .tags(new String[] { "blog", "weblog"}) - .longUrl("https://erik.thauvin.net/blog") .build(); bitly.bitlinks().create(config); ``` ```java -var config = new UpdateConfig.Builder() - .bitlink("https://bit.ly/380ojFd") +var config = new UpdateConfig.Builder("https://bit.ly/380ojFd") .title("Erik's Weblog") .tags(new String[] { "blog", "weblog"}) .build(); @@ -171,7 +169,7 @@ if (response.isSuccessful) { } ``` -- View [Example](https://github.com/ethauvin/bitly-shorten/blob/master/examples/src/main/kotlin/com/example/BitlyRetrieve.kt) +- View [Example](https://github.com/ethauvin/bitly-shorten/blob/master/examples/bld/src/main/kotlin/com/example/BitlyRetrieve.kt) ## Contributing diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 711430a..3d4032c 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -1,38 +1,17 @@ - + - + - ConstructorParameterNaming:CreateConfig.kt$CreateConfig$val group_guid: String - ConstructorParameterNaming:CreateConfig.kt$CreateConfig$val long_url: String - ConstructorParameterNaming:CreateConfig.kt$CreateConfig.Builder$private var group_guid: String = Constants.EMPTY - ConstructorParameterNaming:CreateConfig.kt$CreateConfig.Builder$private var long_url: String = Constants.EMPTY - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig$val client_id: String - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig$val created_at: String - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig$val created_by: String - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig$val custom_bitlinks: Array<String> - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig$val long_url: String - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig.Builder$private var client_id: String = Constants.EMPTY - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig.Builder$private var created_at: String = Constants.EMPTY - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig.Builder$private var created_by: String = Constants.EMPTY - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig.Builder$private var custom_bitlinks: Array<String> = emptyArray() - ConstructorParameterNaming:UpdateConfig.kt$UpdateConfig.Builder$private var long_url: String = Constants.EMPTY - CyclomaticComplexMethod:Bitlinks.kt$Bitlinks$@Synchronized fun update( bitlink: String, references: Map<String, String> = emptyMap(), archived: Boolean = false, tags: Array<String> = emptyArray(), created_at: String = Constants.EMPTY, title: String = Constants.EMPTY, deeplinks: Array<Map<String, String>> = emptyArray(), created_by: String = Constants.EMPTY, long_url: String = Constants.EMPTY, client_id: String = Constants.EMPTY, custom_bitlinks: Array<String> = emptyArray(), link: String = Constants.EMPTY, id: String = Constants.EMPTY, toJson: Boolean = false ): String + ConstructorParameterNaming:CreateConfig.kt$CreateConfig.Builder$var long_url: String FunctionParameterNaming:Bitlinks.kt$Bitlinks$bitlink_id: String - FunctionParameterNaming:Bitlinks.kt$Bitlinks$client_id: String = Constants.EMPTY - FunctionParameterNaming:Bitlinks.kt$Bitlinks$created_at: String = Constants.EMPTY - FunctionParameterNaming:Bitlinks.kt$Bitlinks$created_by: String = Constants.EMPTY - FunctionParameterNaming:Bitlinks.kt$Bitlinks$custom_bitlinks: Array<String> = emptyArray() + FunctionParameterNaming:Bitlinks.kt$Bitlinks$custom_bitlink: String FunctionParameterNaming:Bitlinks.kt$Bitlinks$group_guid: String = Constants.EMPTY FunctionParameterNaming:Bitlinks.kt$Bitlinks$long_url: String - FunctionParameterNaming:Bitlinks.kt$Bitlinks$long_url: String = Constants.EMPTY FunctionParameterNaming:Bitlinks.kt$Bitlinks$unit_reference: String = Constants.EMPTY FunctionParameterNaming:CreateConfig.kt$CreateConfig.Builder$group_guid: String FunctionParameterNaming:CreateConfig.kt$CreateConfig.Builder$long_url: String - LongParameterList:Bitlinks.kt$Bitlinks$( bitlink: String, references: Map<String, String> = emptyMap(), archived: Boolean = false, tags: Array<String> = emptyArray(), created_at: String = Constants.EMPTY, title: String = Constants.EMPTY, deeplinks: Array<Map<String, String>> = emptyArray(), created_by: String = Constants.EMPTY, long_url: String = Constants.EMPTY, client_id: String = Constants.EMPTY, custom_bitlinks: Array<String> = emptyArray(), link: String = Constants.EMPTY, id: String = Constants.EMPTY, toJson: Boolean = false ) - LongParameterList:Bitlinks.kt$Bitlinks$( bitlink: String, unit: Units = Units.DAY, units: Int = -1, size: Int = 50, unit_reference: String = Constants.EMPTY, toJson: Boolean = false ) - LongParameterList:Bitlinks.kt$Bitlinks$( 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 ) - LongParameterList:CreateConfig.kt$CreateConfig$( val domain: String, val title: String, val group_guid: String, val tags: Array<String>, val deepLinks: Array<Map<String, String>>, val long_url: String, val toJson: Boolean ) - LongParameterList:UpdateConfig.kt$UpdateConfig$( val bitlink: String, val references: Map<String, String>, val archived: Boolean, val tags: Array<String>, val created_at: String, val title: String, val deepLinks: Array<Map<String, String>>, val created_by: String, val long_url: String, val client_id: String, val custom_bitlinks: Array<String>, val link: String, val id: String, val toJson: Boolean, ) + LongParameterList:Bitlinks.kt$Bitlinks$( bitlink: String, title: String = Constants.EMPTY, archived: Boolean = false, tags: Array<String> = emptyArray(), deeplinks: Array<Map<String, String>> = emptyArray(), toJson: Boolean = false ) + LongParameterList:Bitlinks.kt$Bitlinks$( long_url: String, domain: String = Constants.EMPTY, group_guid: String = Constants.EMPTY, title: String = Constants.EMPTY, tags: Array<String> = emptyArray(), deeplinks: Array<Map<String, String>> = emptyArray(), toJson: Boolean = false ) MagicNumber:CallResponse.kt$CallResponse$200 MagicNumber:CallResponse.kt$CallResponse$201 MagicNumber:CallResponse.kt$CallResponse$299 @@ -47,9 +26,12 @@ MagicNumber:CallResponse.kt$CallResponse$500 MagicNumber:CallResponse.kt$CallResponse$503 NestedBlockDepth:Bitlinks.kt$Bitlinks$private fun parseJsonResponse(response: CallResponse, key: String, default: String, toJson: Boolean): String - NestedBlockDepth:Utils.kt$Utils$@JvmStatic @JvmOverloads fun call( accessToken: String, endPoint: String, params: Map<String, Any> = emptyMap(), method: Methods = Methods.POST ): CallResponse + NestedBlockDepth:BitlyExample.kt$fun main(args: Array<String>) + NestedBlockDepth:Utils.kt$Utils$@JvmStatic @JvmOverloads fun call( accessToken: String, endPoint: String, params: Map<String, Any> = emptyMap(), method: Methods = Methods.POST ): CallResponse NestedBlockDepth:Utils.kt$Utils$private fun parseResponse(response: Response, endPoint: String): CallResponse - TooManyFunctions:UpdateConfig.kt$UpdateConfig$Builder + VariableNaming:CreateConfig.kt$CreateConfig$val group_guid: String + VariableNaming:CreateConfig.kt$CreateConfig$val long_url: String + VariableNaming:CreateConfig.kt$CreateConfig.Builder$var group_guid: String = Constants.EMPTY WildcardImport:BitlyTest.kt$import assertk.assertions.* WildcardImport:BitlyTest.kt$import kotlin.test.* diff --git a/examples/bld/src/bld/java/com/example/ExampleBuild.java b/examples/bld/src/bld/java/com/example/ExampleBuild.java index 3845db9..68625de 100644 --- a/examples/bld/src/bld/java/com/example/ExampleBuild.java +++ b/examples/bld/src/bld/java/com/example/ExampleBuild.java @@ -24,7 +24,7 @@ public class ExampleBuild extends BaseProject { repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, SONATYPE_SNAPSHOTS_LEGACY); scope(compile) - .include(dependency("net.thauvin.erik:bitly-shorten:1.0.2-SNAPSHOT")) + .include(dependency("net.thauvin.erik:bitly-shorten:2.0.0")) .include(dependency("org.json:json:20240303")); } diff --git a/examples/gradle/build.gradle.kts b/examples/gradle/build.gradle.kts index b75967e..5a66af6 100644 --- a/examples/gradle/build.gradle.kts +++ b/examples/gradle/build.gradle.kts @@ -17,7 +17,7 @@ repositories { } dependencies { - implementation("net.thauvin.erik:bitly-shorten:1.0.1") + implementation("net.thauvin.erik:bitly-shorten:2.0.0") implementation("org.json:json:20240303") } diff --git a/src/bld/java/net/thauvin/erik/bitly/BitlyShortenBuild.java b/src/bld/java/net/thauvin/erik/bitly/BitlyShortenBuild.java index 5c3f77d..ed8bb60 100644 --- a/src/bld/java/net/thauvin/erik/bitly/BitlyShortenBuild.java +++ b/src/bld/java/net/thauvin/erik/bitly/BitlyShortenBuild.java @@ -61,7 +61,7 @@ public class BitlyShortenBuild extends Project { public BitlyShortenBuild() { pkg = "net.thauvin.erik"; name = "bitly-shorten"; - version = version(1, 0, 2, "SNAPSHOT"); + version = version(2, 0, 0); javaRelease = 11; downloadSources = true; diff --git a/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt b/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt index 4cfee73..455a783 100644 --- a/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt +++ b/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt @@ -39,6 +39,8 @@ import net.thauvin.erik.bitly.config.CreateConfig import net.thauvin.erik.bitly.config.UpdateConfig import org.json.JSONException import org.json.JSONObject +import java.net.URLEncoder +import java.nio.charset.StandardCharsets import java.util.logging.Level /** @@ -301,4 +303,40 @@ open class Bitlinks(private val accessToken: String) { } return result } + + /** + * Move a keyword (or custom back-half) to a different Bitlink (domains must match). + * + * See the [Bit.ly API](https://dev.bitly.com/api-reference/#updateCustomBitlink) for more information. + * + * @param custom_bitlink A Custom Bitlink made of the domain and keyword. + * @param toJson Returns the full JSON response if `true`. + * @return [Constants.TRUE] if the update was successful, [Constants.FALSE] otherwise. + */ + @Synchronized + @JvmOverloads + fun updateCustom( + custom_bitlink: String, + bitlink_id: String, + toJson: Boolean = false + ): String { + var result = if (toJson) Constants.EMPTY_JSON else Constants.FALSE + if (custom_bitlink.isNotBlank() && bitlink_id.isNotBlank()) { + lastCallResponse = Utils.call( + accessToken, + "custom_bitlinks/${custom_bitlink.removeHttp()}".toEndPoint(), + mutableMapOf().apply { put("bitlink_id", bitlink_id) }, + Methods.PATCH + ) + + if (lastCallResponse.isSuccessful) { + result = if (toJson) { + lastCallResponse.body + } else { + Constants.TRUE + } + } + } + return result + } } diff --git a/src/test/kotlin/DisableOnCi.kt b/src/test/kotlin/DisableOnCi.kt new file mode 100644 index 0000000..59539c1 --- /dev/null +++ b/src/test/kotlin/DisableOnCi.kt @@ -0,0 +1,43 @@ +/* + * DisableOnCi.kt + * + * Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net) + * + * 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. + */ + +import org.junit.jupiter.api.extension.ExtendWith + +/** + * Disables tests on CI annotation. + * + * @author [Erik C. Thauvin](https://erik.thauvin.net/) + * @since 1.0 + */ +@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +@ExtendWith(DisableOnCiCondition::class) +annotation class DisableOnCi diff --git a/src/test/kotlin/DisableOnCiCondition.kt b/src/test/kotlin/DisableOnCiCondition.kt new file mode 100644 index 0000000..6be68f3 --- /dev/null +++ b/src/test/kotlin/DisableOnCiCondition.kt @@ -0,0 +1,50 @@ +/* + * DisableOnCiCondition.kt + * + * Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net) + * + * 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. + */ + +import org.junit.jupiter.api.extension.ConditionEvaluationResult +import org.junit.jupiter.api.extension.ExecutionCondition +import org.junit.jupiter.api.extension.ExtensionContext + +/** + * Disables tests on CI condition. + * + * @author [Erik C. Thauvin](https://erik.thauvin.net/) + * @since 2.0 + */ +class DisableOnCiCondition : ExecutionCondition { + override fun evaluateExecutionCondition(context: ExtensionContext): ConditionEvaluationResult { + return if (System.getenv("CI") != null) { + ConditionEvaluationResult.disabled("Test disabled on CI") + } else { + ConditionEvaluationResult.enabled("Test enabled") + } + } +} diff --git a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt index c34efa8..f60e4fa 100644 --- a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt +++ b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt @@ -31,6 +31,7 @@ package net.thauvin.erik.bitly +import DisableOnCi import assertk.all import assertk.assertThat import assertk.assertions.* @@ -227,6 +228,16 @@ class BitlyTest { } } + @Test + @DisableOnCi + fun `update custom bitlink`() { + val bl = bitly.bitlinks() + assertEquals( + Constants.TRUE, + bl.updateCustom("https://thauv.in/2NwtljT", "thauv.in/2NwtljT") + ) + } + @Test fun `update bitlink`() { val bl = bitly.bitlinks()