feat: initial api support
This commit is contained in:
parent
ad50e700d4
commit
4dd63b7d1d
20 changed files with 156 additions and 62 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue