Removed toJson() in CallResponse.

This commit is contained in:
Erik C. Thauvin 2020-03-17 17:01:09 -07:00
parent 353927580d
commit 8060f856ee
9 changed files with 28 additions and 26 deletions

View file

@ -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/).

View file

@ -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)
`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)

View file

@ -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` |

View file

@ -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)
`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)

View file

@ -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)
`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)

View file

@ -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));

View file

@ -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)
}

View file

@ -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)
}
}

View file

@ -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")
)
}