Changed Clicks/summary to clicks.

This commit is contained in:
Erik C. Thauvin 2020-03-01 20:40:48 -08:00
parent 7587da57e5
commit b7066f6ff7

View file

@ -32,6 +32,9 @@
package net.thauvin.erik.bitly package net.thauvin.erik.bitly
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import java.util.logging.Level import java.util.logging.Level
@ -42,32 +45,45 @@ import java.util.logging.Level
* See the [Bitly API](https://dev.bitly.com/v4/#tag/Bitlinks) for more information. * See the [Bitly API](https://dev.bitly.com/v4/#tag/Bitlinks) for more information.
*/ */
open class Bitlinks(val accessToken: String) { open class Bitlinks(val accessToken: String) {
inner class Clicks { /**
fun summary( * Returns the click counts for a specified Bitlink.
bitlink: String, *
unit: Units = Units.DAY, * See the [Bitly API](https://dev.bitly.com/v4/#operation/getClicksSummaryForBitlink) for more information.
units: Int = -1, *
size: Int = 50, * @param bitlink The bitlink.
unit_reference: String = Constants.EMPTY, * @param unit A unit of time.
isJson: Boolean = false * @param units An integer representing the time units to query data for. pass -1 to return all units available.
): String { * @param size The quantity of items to be be returned.
var clicks = if (isJson) Constants.EMPTY_JSON else Constants.EMPTY * @param unit_reference An ISO-8601 timestamp, indicating the most recent time for which to pull metrics.
if (bitlink.isNotBlank()) { * Will default to current time.
val response = Utils.call( * @param toJson Returns the full JSON response if `true`
accessToken, * @return The click counts or JSON response object.
Utils.buildEndPointUrl("/bitlinks/" + bitlink.removeHttp() + "/clicks/summary"), */
hashMapOf( @JvmOverloads
Pair("unit", unit.toString().toLowerCase()), fun clicks(
Pair("units", units.toString()), bitlink: String,
Pair("size", size.toString()), unit: Units = Units.DAY,
Pair("unit_reference", unit_reference) units: Int = -1,
), size: Int = 50,
Methods.GET unit_reference: String = Constants.EMPTY,
) toJson: Boolean = false
clicks = parseJsonResponse(response, "total_clicks", clicks, isJson) ): String {
} var clicks = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
return clicks if (bitlink.isNotBlank()) {
val response = Utils.call(
accessToken,
("/bitlinks/" + bitlink.removeHttp() + "/clicks/summary").toEndPoint(),
hashMapOf(
Pair("unit", unit.toString().toLowerCase()),
Pair("units", units.toString()),
Pair("size", size.toString()),
Pair("unit_reference", unit_reference)
),
Methods.GET
)
clicks = parseJsonResponse(response, "total_clicks", clicks, toJson)
} }
return clicks
} }
/** /**
@ -76,20 +92,20 @@ open class Bitlinks(val accessToken: String) {
* See the [Bit.ly API](https://dev.bitly.com/v4/#operation/expandBitlink) for more information. * See the [Bit.ly API](https://dev.bitly.com/v4/#operation/expandBitlink) for more information.
* *
* @param bitlink_id The bitlink ID. * @param bitlink_id The bitlink ID.
* @param isJson Returns the full JSON response if `true` * @param toJson Returns the full JSON response if `true`
* @return The long URL or JSON response, or on error, an empty string/JSON object. * @return The long URL or JSON response, or on error, an empty string/JSON object.
*/ */
@JvmOverloads @JvmOverloads
fun expand(bitlink_id: String, isJson: Boolean = false): String { fun expand(bitlink_id: String, toJson: Boolean = false): String {
var longUrl = if (isJson) Constants.EMPTY_JSON else Constants.EMPTY var longUrl = if (toJson) Constants.EMPTY_JSON else Constants.EMPTY
if (bitlink_id.isNotBlank()) { if (bitlink_id.isNotBlank()) {
val response = Utils.call( val response = Utils.call(
accessToken, accessToken,
Utils.buildEndPointUrl("/expand"), "/expand".toEndPoint(),
mapOf(Pair("bitlink_id", bitlink_id.removeHttp())), mapOf(Pair("bitlink_id", bitlink_id.removeHttp())),
Methods.POST Methods.POST
) )
longUrl = parseJsonResponse(response, "long_url", longUrl, isJson) longUrl = parseJsonResponse(response, "long_url", longUrl, toJson)
} }
return longUrl return longUrl
@ -102,10 +118,10 @@ open class Bitlinks(val accessToken: String) {
default default
} }
private fun parseJsonResponse(response: String, key: String, default: String, isJson: Boolean): String { private fun parseJsonResponse(response: String, key: String, default: String, toJson: Boolean): String {
var parsed = default var parsed = default
if (response.isNotEmpty()) { if (response.isNotEmpty()) {
if (isJson) { if (toJson) {
parsed = response parsed = response
} else { } else {
try { try {
@ -118,10 +134,6 @@ open class Bitlinks(val accessToken: String) {
return parsed return parsed
} }
private fun String.removeHttp(): String {
return this.replaceFirst(Regex("^[Hh][Tt]{2}[Pp][Ss]?://"), "")
}
/** /**
* Shortens a long URL. * Shortens a long URL.
* *
@ -130,7 +142,7 @@ open class Bitlinks(val accessToken: String) {
* @param long_url The long URL. * @param long_url The long URL.
* @param group_guid The group UID. * @param group_guid The group UID.
* @param domain The domain for the short URL. * @param domain The domain for the short URL.
* @param isJson Returns the full JSON response if `true` * @param toJson Returns the full JSON response if `true`
* @return The short URL or JSON response, or on error, the [long_url] or an empty JSON object. * @return The short URL or JSON response, or on error, the [long_url] or an empty JSON object.
*/ */
@JvmOverloads @JvmOverloads
@ -138,10 +150,10 @@ open class Bitlinks(val accessToken: String) {
long_url: String, long_url: String,
group_guid: String = Constants.EMPTY, group_guid: String = Constants.EMPTY,
domain: String = Constants.EMPTY, domain: String = Constants.EMPTY,
isJson: Boolean = false toJson: Boolean = false
): String { ): String {
var bitlink = if (isJson) Constants.EMPTY_JSON else long_url var bitlink = if (toJson) Constants.EMPTY_JSON else long_url
if (!Utils.validateUrl(long_url)) { if (!long_url.isValidUrl()) {
Utils.logger.severe("Please specify a valid URL to shorten.") Utils.logger.severe("Please specify a valid URL to shorten.")
} else { } else {
val params: HashMap<String, String> = HashMap() val params: HashMap<String, String> = HashMap()
@ -153,9 +165,9 @@ open class Bitlinks(val accessToken: String) {
} }
params["long_url"] = long_url params["long_url"] = long_url
val response = Utils.call(accessToken, Utils.buildEndPointUrl("/shorten"), params) val response = Utils.call(accessToken, "/shorten".toEndPoint(), params)
bitlink = parseJsonResponse(response, "link", bitlink, isJson) bitlink = parseJsonResponse(response, "link", bitlink, toJson)
} }
return bitlink return bitlink