-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve how suspending API calls are exported to javascript as Promise
- Loading branch information
Showing
8 changed files
with
76 additions
and
74 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
common/src/commonMain/kotlin/de/fumix/sipgator/common/api/CommonSipgateApiV2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.fumix.sipgator.common.api | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.resources.get | ||
import io.ktor.client.request.accept | ||
import io.ktor.client.request.basicAuth | ||
import io.ktor.client.request.headers | ||
import io.ktor.client.request.host | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.isSuccess | ||
import de.fumix.sipgator.common.api.dto.BasicList | ||
import de.fumix.sipgator.common.api.dto.Contact | ||
import de.fumix.sipgator.common.api.dto.Device | ||
import de.fumix.sipgator.common.api.dto.HistoryEntry | ||
import de.fumix.sipgator.common.api.dto.PagedList | ||
import de.fumix.sipgator.common.api.dto.UserInfo | ||
import de.fumix.sipgator.common.model.SipgateApiCredentials | ||
|
||
data class CommonSipgateApiV2(private val credential: SipgateApiCredentials) { | ||
private val client: HttpClient = httpClient() | ||
|
||
val contacts = basicGet<SipgateApiV2.Contacts, PagedList<Contact>, Array<Contact>>(SipgateApiV2.Contacts()) { it.items.toTypedArray() } | ||
val history = basicGet<SipgateApiV2.History, PagedList<HistoryEntry>, Array<HistoryEntry>>(SipgateApiV2.History()) { it.items.toTypedArray() } | ||
fun userDevices(userId: String) = basicGet<SipgateApiV2.UserId.Devices, BasicList<Device>, Array<Device>>(SipgateApiV2.UserId.Devices(SipgateApiV2.UserId(userId = userId))) { it.items.toTypedArray() } | ||
val userInfo: suspend ((UserInfo) -> Unit, (Throwable) -> Unit) -> Unit = basicGet(SipgateApiV2.Authorization.UserInfo()) | ||
|
||
private inline fun <reified R: Any, reified T: Any> basicGet(resource: R): suspend ((T) -> Unit, (Throwable) -> Unit) -> Unit = basicGet<R, T, T>(resource) { it } | ||
|
||
private inline fun <reified R: Any, reified S: Any, T: Any> basicGet(resource: R, crossinline f: (S) -> T): suspend ((T) -> Unit, (Throwable) -> Unit) -> Unit { | ||
return { resolve, reject -> | ||
val response = client.get(resource) { | ||
host = "api.sipgate.com" | ||
url { | ||
protocol = URLProtocol.HTTPS | ||
} | ||
headers { | ||
accept(ContentType.Application.Json) | ||
basicAuth(credential.tokenName, credential.token) | ||
} | ||
} | ||
|
||
if (response.status.isSuccess()) { | ||
try { | ||
resolve(f(response.body<S>())) | ||
} catch (e: Exception) { | ||
println("A") | ||
reject(SipgateApiException.Decode(response.status, "Failed to decode API response! ${e}")) | ||
} | ||
} else { | ||
println("B") | ||
reject(SipgateApiException.Http(response.status, "Request failed for resource ${resource::class.simpleName}!")) | ||
} | ||
} | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
common/src/commonMain/kotlin/de/fumix/sipgator/common/api/SipgateApiV2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 10 additions & 65 deletions
75
common/src/jsMain/kotlin/de/fumix/sipgator/common/api/JsSipgateApiV2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,29 @@ | ||
package de.fumix.sipgator.common.api | ||
|
||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.resources.get | ||
import io.ktor.client.request.accept | ||
import io.ktor.client.request.basicAuth | ||
import io.ktor.client.request.host | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.headers | ||
import io.ktor.http.isSuccess | ||
import kotlin.js.ExperimentalJsExport | ||
import kotlin.js.JsExport | ||
import kotlin.js.Promise | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import de.fumix.sipgator.common.api.dto.BasicList | ||
import de.fumix.sipgator.common.api.dto.Contact | ||
import de.fumix.sipgator.common.api.dto.Device | ||
import de.fumix.sipgator.common.api.dto.HistoryEntry | ||
import de.fumix.sipgator.common.api.dto.PagedList | ||
import de.fumix.sipgator.common.api.dto.UserInfo | ||
import de.fumix.sipgator.common.model.SipgateApiCredentials | ||
|
||
@OptIn(ExperimentalJsExport::class) | ||
@JsExport | ||
object JsSipgateApiV2 { | ||
private val client = httpClient() | ||
class JsSipgateApiV2(credentials: SipgateApiCredentials) { | ||
private val api = CommonSipgateApiV2(credentials) | ||
|
||
fun userInfo( | ||
credentials: SipgateApiCredentials | ||
): Promise<UserInfo> = normalGetPromise( | ||
credentials, | ||
SipgateApiV2.Authorization.UserInfo() | ||
) | ||
fun contacts(): Promise<Array<Contact>> = asPromise(api.contacts) | ||
fun history(): Promise<Array<HistoryEntry>> = asPromise(api.history) | ||
fun userDevices(userId: String): Promise<Array<Device>> = asPromise(api.userDevices(userId)) | ||
fun userInfo(): Promise<UserInfo> = asPromise(api.userInfo) | ||
|
||
fun contacts( | ||
credentials: SipgateApiCredentials | ||
): Promise<Array<Contact>> = normalGetPromise<SipgateApiV2.Contacts, PagedList<Contact>, Array<Contact>>( | ||
credentials, | ||
SipgateApiV2.Contacts() | ||
) { it.items.toTypedArray() } | ||
|
||
fun history( | ||
credentials: SipgateApiCredentials | ||
): Promise<Array<HistoryEntry>> = normalGetPromise<SipgateApiV2.History, PagedList<HistoryEntry>, Array<HistoryEntry>>( | ||
credentials, | ||
SipgateApiV2.History(limit = 50) | ||
) { it.items.toTypedArray() } | ||
|
||
fun userDevices( | ||
credentials: SipgateApiCredentials, | ||
userId: String | ||
): Promise<Array<Device>> = normalGetPromise<SipgateApiV2.UserId.Devices, BasicList<Device>, Array<Device>>( | ||
credentials, | ||
SipgateApiV2.UserId.Devices(SipgateApiV2.UserId(userId = userId)) | ||
) { it.items.toTypedArray() } | ||
|
||
private inline fun <reified R: Any, reified T: Any> normalGetPromise(credential: SipgateApiCredentials, resource: R): Promise<T> = normalGetPromise<R, T, T>(credential, resource) { it } | ||
|
||
private inline fun <reified R: Any, reified S: Any, reified T: Any> normalGetPromise(credential: SipgateApiCredentials, resource: R, crossinline f: (S) -> T): Promise<T> = Promise { resolve, reject -> | ||
private fun <T: Any> asPromise( | ||
f: suspend ((T) -> Unit, (Throwable) -> Unit) -> Unit | ||
): Promise<T> = Promise { resolve, reject -> | ||
GlobalScope.launch(Dispatchers.Default) { | ||
val response = client.get(resource) { | ||
host = "api.sipgate.com" | ||
url { protocol = URLProtocol.HTTPS } | ||
headers { | ||
accept(ContentType.Application.Json) | ||
basicAuth(credential.tokenName, credential.token) | ||
} | ||
} | ||
if (response.status.isSuccess()) { | ||
try { | ||
resolve(f(response.body<S>())) | ||
} catch (e: Exception) { | ||
reject(SipgateApiException.Decode(response.status, "Failed to decode API response! " + e.message)) | ||
} | ||
} else { | ||
reject(SipgateApiException.Http(response.status, "Request failed for resource ${resource::class.simpleName}!")) | ||
} | ||
f(resolve, reject) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters