// Fiskon Relay Terminal SDK for Kotlin (Android and JVM). No dependencies beyond org.json (built into Android). // Calls are BLOCKING: run them on a background thread or a coroutine on Dispatchers.IO. // // val relay = Relay(apiKey = "YOUR_SECRET_KEY") // servers / trusted code only // val device = Relay(publicKey = "pk_...") // safe inside your app // device.registerDevice(mapOf("token" to fcmToken, "platform" to "android", "external_id" to "user-42")) package com.fiskon.relay import org.json.JSONObject import java.net.HttpURLConnection import java.net.URL class RelayException(message: String, val code: String?, val status: Int, val data: JSONObject?) : Exception(message) class Relay( private val apiKey: String? = null, private val publicKey: String? = null, baseUrl: String = DEFAULT_BASE, private val timeoutMs: Int = 30000, ) { companion object { const val DEFAULT_BASE = "https://relay.naweriindustries.com/api/v1" } private val base = baseUrl.trimEnd('/') init { require(apiKey != null || publicKey != null) { "Provide an apiKey or a publicKey" } } private fun request(method: String, path: String, body: Map? = null, idempotencyKey: String? = null, client: Boolean = false): JSONObject { val conn = URL(base + path).openConnection() as HttpURLConnection conn.requestMethod = method conn.connectTimeout = timeoutMs conn.readTimeout = timeoutMs conn.setRequestProperty("Accept", "application/json") when { client && publicKey != null && apiKey == null -> conn.setRequestProperty("X-Public-Key", publicKey) apiKey != null -> conn.setRequestProperty("Authorization", "Bearer $apiKey") else -> throw IllegalStateException("This call needs the secret apiKey") } if (idempotencyKey != null) conn.setRequestProperty("Idempotency-Key", idempotencyKey) if (body != null) { conn.doOutput = true conn.setRequestProperty("Content-Type", "application/json") conn.outputStream.use { it.write(JSONObject(body).toString().toByteArray(Charsets.UTF_8)) } } val status = conn.responseCode val stream = if (status >= 400) conn.errorStream else conn.inputStream val raw = stream?.bufferedReader()?.use { it.readText() } ?: "" val json = try { JSONObject(raw) } catch (e: Exception) { JSONObject("""{"error":1,"code":"bad_response","message":"HTTP $status"}""") } if (status >= 400 || json.optInt("error", 0) != 0) { throw RelayException(json.optString("message", "Request failed"), json.optString("code", null), status, json.optJSONObject("data")) } return json } fun sendEmail(p: Map, idempotencyKey: String? = null) = request("POST", "/email", p, idempotencyKey) fun sendSms(p: Map, idempotencyKey: String? = null) = request("POST", "/sms", p, idempotencyKey) fun sendWhatsapp(p: Map, idempotencyKey: String? = null) = request("POST", "/whatsapp", p, idempotencyKey) fun sendPush(p: Map, idempotencyKey: String? = null) = request("POST", "/push", p, idempotencyKey) fun registerDevice(p: Map) = request("POST", "/push/devices", p, client = true) fun removeDevice(p: Map) = request("DELETE", "/push/devices", p, client = true) fun sendTransactional(p: Map, idempotencyKey: String? = null) = request("POST", "/transactional/email", p, idempotencyKey) fun templates() = request("GET", "/templates") fun sendOtp(p: Map) = request("POST", "/otp", p) fun verifyOtp(p: Map) = request("POST", "/otp/verify", p) fun balance() = request("GET", "/balance") fun status(channel: String, id: Int) = request("GET", "/messages/$channel/$id") }