feat: initial api support
This commit is contained in:
parent
ad50e700d4
commit
4dd63b7d1d
20 changed files with 156 additions and 62 deletions
|
|
@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
|||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlinMultiplatform)
|
||||
alias(libs.plugins.kotlinSerialization)
|
||||
alias(libs.plugins.androidLibrary)
|
||||
}
|
||||
|
||||
|
|
@ -19,10 +20,22 @@ kotlin {
|
|||
iosSimulatorArm64()
|
||||
|
||||
jvm()
|
||||
|
||||
|
||||
sourceSets {
|
||||
androidMain.dependencies {
|
||||
implementation(libs.ktor.client.okhttp)
|
||||
}
|
||||
commonMain.dependencies {
|
||||
implementation(libs.okio)
|
||||
// put your Multiplatform dependencies here
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.contentnegotiation)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
}
|
||||
iosMain.dependencies {
|
||||
implementation(libs.ktor.client.darwin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
import android.util.Log
|
||||
|
||||
actual fun log(tag: String, msg: String) {
|
||||
Log.i(tag, msg)
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
import android.os.Build
|
||||
|
||||
class AndroidPlatform : Platform {
|
||||
override val name: String = "Android ${Build.VERSION.SDK_INT}"
|
||||
}
|
||||
|
||||
actual fun getPlatform(): Platform = AndroidPlatform()
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
const val SERVER_PORT = 8080
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
object Constants {
|
||||
const val devid: String = ""
|
||||
const val key: String = ""
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
class Greeting {
|
||||
private val platform = getPlatform()
|
||||
|
||||
fun greet(): String {
|
||||
return "Hello, ${platform.name}!"
|
||||
}
|
||||
}
|
||||
3
shared/src/commonMain/kotlin/moe/lava/banksia/Logging.kt
Normal file
3
shared/src/commonMain/kotlin/moe/lava/banksia/Logging.kt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
expect fun log(tag: String, msg: String)
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
interface Platform {
|
||||
val name: String
|
||||
}
|
||||
|
||||
expect fun getPlatform(): Platform
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
package moe.lava.banksia.api.ptv
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.HttpSend
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.plugins.defaultRequest
|
||||
import io.ktor.client.plugins.plugin
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.parameter
|
||||
import io.ktor.http.encodedPath
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.io.bytestring.encodeToByteString
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import moe.lava.banksia.Constants
|
||||
import moe.lava.banksia.log
|
||||
import okio.ByteString.Companion.encodeUtf8
|
||||
|
||||
@Serializable
|
||||
data class Route(
|
||||
@SerialName("route_id") val routeId: Int,
|
||||
@SerialName("route_number") val routeNumber: String,
|
||||
@SerialName("route_name") val routeName: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class RouteResponse(val routes: List<Route>)
|
||||
|
||||
class PtvService {
|
||||
private val client = HttpClient() {
|
||||
install(ContentNegotiation) {
|
||||
json(Json {
|
||||
ignoreUnknownKeys = true
|
||||
})
|
||||
}
|
||||
defaultRequest {
|
||||
url("https://timetableapi.ptv.vic.gov.au/v3/")
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
client.plugin(HttpSend).intercept { req ->
|
||||
req.parameter("devid", Constants.devid)
|
||||
val fullPath = req.url.build().encodedPathAndQuery
|
||||
val hash = fullPath.encodeUtf8().hmacSha1(Constants.key.encodeUtf8()).hex()
|
||||
req.parameter("signature", hash)
|
||||
log("ktor.intercept", req.url.build().encodedPathAndQuery)
|
||||
execute(req)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun routes(): List<Route> {
|
||||
val response: RouteResponse = client.get("routes").body()
|
||||
return response.routes
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
actual fun log(tag: String, msg: String) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
import platform.UIKit.UIDevice
|
||||
|
||||
class IOSPlatform: Platform {
|
||||
override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
|
||||
}
|
||||
|
||||
actual fun getPlatform(): Platform = IOSPlatform()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
actual fun log(tag: String, msg: String) {
|
||||
println("[$tag] $msg")
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package moe.lava.banksia
|
||||
|
||||
class JVMPlatform: Platform {
|
||||
override val name: String = "Java ${System.getProperty("java.version")}"
|
||||
}
|
||||
|
||||
actual fun getPlatform(): Platform = JVMPlatform()
|
||||
Loading…
Add table
Add a link
Reference in a new issue