11package dev.zxilly.notify.sdk
22
3- import dev.zxilly.notify.sdk.entity.Message
4- import dev.zxilly.notify.sdk.entity.MessageItem
3+ import dev.zxilly.notify.sdk.entity.*
54import io.ktor.client.*
5+ import io.ktor.client.call.*
6+ import io.ktor.client.plugins.contentnegotiation.*
67import io.ktor.client.request.*
78import io.ktor.client.request.forms.*
89import io.ktor.client.statement.*
910import io.ktor.http.*
10- import kotlinx. serialization.builtins.ListSerializer
11+ import io.ktor. serialization.kotlinx.json.*
1112import kotlinx.serialization.json.Json
1213
1314@Suppress(" MemberVisibilityCanBePrivate" , " unused" )
1415class Client private constructor(
1516 private val userID : String ,
1617 private val endpoint : String
1718) {
18- private val client = HttpClient ()
19+ private val client = HttpClient () {
20+ install(ContentNegotiation ) {
21+ json(Json {
22+ ignoreUnknownKeys = true
23+ })
24+ }
25+ }
1926
20- private suspend fun check () {
27+ private suspend inline fun check () {
2128 val resp = client.get(" $endpoint /check" ) {
2229 parameter(" user_id" , userID)
2330 }
24- if (resp.bodyAsText() != " true" ) {
25- throw Error (" User ID not valid" )
31+ val ret: Response <Boolean > = resp.body()
32+ if (! ret.body) {
33+ throw Exception (" User ID $userID not valid" )
2634 }
2735 }
2836
29- suspend fun send (msg : Message ) = wrap {
37+ suspend fun send (msg : MessagePayload ) = wrap {
3038 if (msg.content.isBlank()) {
3139 throw emptyContentError
3240 }
@@ -39,28 +47,29 @@ class Client private constructor(
3947 }
4048 )
4149 if (! resp.ok()) {
42- throw Error (" Send failed\n ${resp.bodyAsText()} " )
50+ val err: ErrorResponse = resp.body()
51+ throw Exception (" Error code ${err.code} : ${err.body} " )
4352 }
44- Json .decodeFromString( MessageItem .serializer(), resp.bodyAsText())
53+ (resp.body() as Response < MessageItem >).body
4554 }
4655
4756 suspend fun send (content : String ) = wrap {
48- val msg = Message (content, null , null )
57+ val msg = MessagePayload (content, null , null )
4958 send(msg)
5059 }
5160
5261 suspend fun send (content : String , title : String ) = wrap {
53- val msg = Message (content, title, null )
62+ val msg = MessagePayload (content, title, null )
5463 send(msg)
5564 }
5665
5766 suspend fun send (content : String , title : String , long : String ) = wrap {
58- val msg = Message (content, title, long)
67+ val msg = MessagePayload (content, title, long)
5968 send(msg)
6069 }
6170
62- suspend fun send (block : Message .() -> Unit ) = wrap {
63- val msg = Message (" " , null , null ).apply (block)
71+ suspend fun send (block : MessagePayload .() -> Unit ) = wrap {
72+ val msg = MessagePayload (" " , null , null ).apply (block)
6473 send(msg)
6574 }
6675
@@ -71,22 +80,39 @@ class Client private constructor(
7180 }
7281 }
7382
74- suspend fun reportFCMToken (token : String ) = wrap {
75- val resp = client.put(" $endpoint /$userID /fcm/token" ) {
76- setBody(token)
83+ suspend fun register (channel : Channel , token : String , deviceID : String ) = wrap {
84+ if (! isUUID(deviceID)) {
85+ throw Error (" Device ID is not a valid UUID" )
86+ }
87+
88+ println (Parameters .build {
89+ append(" channel" , channel.value)
90+ append(" token" , token)
91+ }.formUrlEncode())
92+ val resp = client.put {
93+ url(" $endpoint /$userID /token/$deviceID " )
94+ setBody(
95+ Parameters .build {
96+ append(" channel" , channel.value)
97+ append(" token" , token)
98+ }.formUrlEncode()
99+ )
100+ header(" Content-Type" , " application/x-www-form-urlencoded" )
77101 }
78102 if (! resp.ok()) {
79- throw Error (" Report failed\n ${resp.bodyAsText()} " )
103+ val err: ErrorResponse = resp.body()
104+ throw Exception (" Error code ${err.code} : ${err.body} " )
80105 }
106+ (resp.body() as Response <Boolean >).body
81107 }
82108
83109 suspend fun <T > fetchMessage (postProcessor : List <MessageItem >.() -> List <T >) = wrap {
84110 val resp = client.get(" $endpoint /$userID /record" )
85111 if (! resp.ok()) {
86112 throw Error (" Fetch failed\n ${resp.bodyAsText()} " )
87113 }
88- val messages = Json .decodeFromString( ListSerializer ( MessageItem .serializer()), resp.bodyAsText())
89- postProcessor(messages)
114+ val messages = resp.body() as Response < List < MessageItem >>
115+ postProcessor(messages.body )
90116 }
91117
92118 companion object {
0 commit comments