From 8060f856eecef22e0d473007ad1b8c4b50c21868 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 17 Mar 2020 17:01:09 -0700 Subject: [PATCH] Removed toJson() in CallResponse. --- README.md | 7 +++++-- .../net.thauvin.erik.bitly/-call-response/body.md | 2 +- .../-call-response/index.md | 8 +------- .../-call-response/is-successful.md | 2 +- .../-call-response/result-code.md | 2 +- .../src/main/java/com/example/BitlySample.java | 2 +- .../src/main/kotlin/com/example/BitlyRetrieve.kt | 15 +++++++++++---- .../kotlin/net/thauvin/erik/bitly/CallResponse.kt | 6 ------ .../kotlin/net/thauvin/erik/bitly/BitlyTest.kt | 10 +++++++--- 9 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 0379088..143bc58 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,10 @@ bitly.bitlinks().shorten("https://www.erik.thauvin.net/blog", toJson = true) Non-implemented methods can also be called directly: ```kotlin -bitly.call("/user".toEndPoint(), method = Methods.GET).toJson() +val response = bitly.call("/user".toEndPoint(), method = Methods.GET) +if (response.isSuccessful) { + println(response.body) +} ``` ```json { @@ -87,5 +90,5 @@ bitly.call("/user".toEndPoint(), method = Methods.GET).toJson() ``` - View [Example](https://github.com/ethauvin/bitly-shorten/blob/master/examples/src/main/kotlin/com/example/BitlyRetrieve.kt) -### More +### More... If all else fails, there's always more [Documentation](https://ethauvin.github.io/bitly-shorten/). diff --git a/docs/net.thauvin.erik.bitly/-call-response/body.md b/docs/net.thauvin.erik.bitly/-call-response/body.md index c9bd5b9..b97c76a 100644 --- a/docs/net.thauvin.erik.bitly/-call-response/body.md +++ b/docs/net.thauvin.erik.bitly/-call-response/body.md @@ -2,4 +2,4 @@ # body -`var body: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L40) \ No newline at end of file +`var body: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L38) \ No newline at end of file diff --git a/docs/net.thauvin.erik.bitly/-call-response/index.md b/docs/net.thauvin.erik.bitly/-call-response/index.md index db0dc84..b0559fb 100644 --- a/docs/net.thauvin.erik.bitly/-call-response/index.md +++ b/docs/net.thauvin.erik.bitly/-call-response/index.md @@ -2,7 +2,7 @@ # CallResponse -`data class CallResponse` [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L40) +`data class CallResponse` [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L38) Provides a data class to hold the JSON response. @@ -19,9 +19,3 @@ Provides a data class to hold the JSON response. | [body](body.md) | `var body: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) | | [isSuccessful](is-successful.md) | `val isSuccessful: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | | [resultCode](result-code.md) | `var resultCode: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | - -### Functions - -| Name | Summary | -|---|---| -| [toJson](to-json.md) | `fun toJson(): JSONObject` | diff --git a/docs/net.thauvin.erik.bitly/-call-response/is-successful.md b/docs/net.thauvin.erik.bitly/-call-response/is-successful.md index a93d2de..35e46ed 100644 --- a/docs/net.thauvin.erik.bitly/-call-response/is-successful.md +++ b/docs/net.thauvin.erik.bitly/-call-response/is-successful.md @@ -2,4 +2,4 @@ # isSuccessful -`val isSuccessful: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L41) \ No newline at end of file +`val isSuccessful: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L39) \ No newline at end of file diff --git a/docs/net.thauvin.erik.bitly/-call-response/result-code.md b/docs/net.thauvin.erik.bitly/-call-response/result-code.md index 17d20ee..6fee1af 100644 --- a/docs/net.thauvin.erik.bitly/-call-response/result-code.md +++ b/docs/net.thauvin.erik.bitly/-call-response/result-code.md @@ -2,4 +2,4 @@ # resultCode -`var resultCode: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L40) \ No newline at end of file +`var resultCode: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) [(source)](https://github.com/ethauvin/bitly-shorten/tree/master/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt#L38) \ No newline at end of file diff --git a/examples/src/main/java/com/example/BitlySample.java b/examples/src/main/java/com/example/BitlySample.java index ea73792..94214d0 100644 --- a/examples/src/main/java/com/example/BitlySample.java +++ b/examples/src/main/java/com/example/BitlySample.java @@ -4,8 +4,8 @@ import net.thauvin.erik.bitly.Bitly; public class BitlySample { public static void main(String[] args) { - final Bitly bitly = new Bitly(/* "YOUR_API_TOKEN from https://bitly.is/accesstoken" */); if (args.length > 0) { + final Bitly bitly = new Bitly(/* "YOUR_API_TOKEN from https://bitly.is/accesstoken" */); for (final String arg : args) { if (arg.contains("bit.ly")) { System.out.println(arg + " <-- " + bitly.bitlinks().expand(arg)); diff --git a/examples/src/main/kotlin/com/example/BitlyRetrieve.kt b/examples/src/main/kotlin/com/example/BitlyRetrieve.kt index daa1dae..d3fe1cf 100644 --- a/examples/src/main/kotlin/com/example/BitlyRetrieve.kt +++ b/examples/src/main/kotlin/com/example/BitlyRetrieve.kt @@ -3,16 +3,23 @@ package com.example import net.thauvin.erik.bitly.Bitly import net.thauvin.erik.bitly.Methods import net.thauvin.erik.bitly.Utils.Companion.toEndPoint +import org.json.JSONObject import kotlin.system.exitProcess fun main() { val bitly = Bitly(/* "YOUR_API_ACCESS_TOKEN from https://bitly.is/accesstoken" */) - val json = bitly.call("/bitlinks/bit.ly/380ojFd".toEndPoint(), method = Methods.GET).toJson() + // See https://dev.bitly.com/v4/#operation/getBitlink + val response = bitly.call("/bitlinks/bit.ly/380ojFd".toEndPoint(), method = Methods.GET) - println("Bitlink is titled : " + json.getString("title")) - println("Bitlink created by: " + json.getString("created_by")) + if (response.isSuccessful) { + val json = JSONObject(response.body) + println("Title : " + json.getString("title")) + println("URL : " + json.getString("long_url")) + println("By : " + json.getString("created_by")) + } else { + println("Invalid Response: ${response.resultCode}") + } exitProcess(0) } - diff --git a/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt b/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt index 0038b7f..40078e9 100644 --- a/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt +++ b/src/main/kotlin/net/thauvin/erik/bitly/CallResponse.kt @@ -32,16 +32,10 @@ package net.thauvin.erik.bitly -import org.json.JSONObject - /** * Provides a data class to hold the JSON response. */ data class CallResponse(var body: String = Constants.EMPTY_JSON, var resultCode: Int = -1) { val isSuccessful: Boolean get() = resultCode in 200..299 - - fun toJson(): JSONObject { - return JSONObject(body) - } } diff --git a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt index fc6aff4..26bbb7f 100644 --- a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt +++ b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt @@ -34,6 +34,7 @@ package net.thauvin.erik.bitly import net.thauvin.erik.bitly.Utils.Companion.removeHttp import net.thauvin.erik.bitly.Utils.Companion.toEndPoint +import org.json.JSONObject import org.junit.Before import java.io.File import java.util.logging.Level @@ -103,9 +104,12 @@ class BitlyTest { fun `created by`() { assertEquals( "ethauvin", - bitly.call("/bitlinks/${shortUrl.removeHttp()}".toEndPoint(), method = Methods.GET) - .toJson() - .getString("created_by") + JSONObject( + bitly.call( + "/bitlinks/${shortUrl.removeHttp()}".toEndPoint(), + method = Methods.GET + ).body + ).getString("created_by") ) }