Refactor HttpServerApi

This commit is contained in:
Jonas Lochmann 2023-02-06 01:00:00 +01:00
parent ca0e33e942
commit 538d062a38
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2023 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -26,6 +26,7 @@ import io.timelimit.android.sync.network.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody import okhttp3.RequestBody
import okhttp3.Response
import okio.BufferedSink import okio.BufferedSink
import okio.GzipSink import okio.GzipSink
import okio.buffer import okio.buffer
@ -109,21 +110,15 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun sendMailLoginCode(mail: String, locale: String, deviceAuthToken: String?): String { override suspend fun sendMailLoginCode(mail: String, locale: String, deviceAuthToken: String?): String {
httpClient.newCall( postJsonRequest(
Request.Builder() "auth/send-mail-login-code-v2"
.url("$endpointWithoutSlashAtEnd/auth/send-mail-login-code-v2") ) { writer ->
.post(createJsonRequestBody { writer.beginObject()
writer -> writer.name(MAIL).value(mail)
writer.name(LOCALE).value(locale)
writer.beginObject() if (deviceAuthToken != null) { writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) }
writer.name(MAIL).value(mail) writer.endObject()
writer.name(LOCALE).value(locale) }.use {
if (deviceAuthToken != null) { writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) }
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
try { try {
it.assertSuccess() it.assertSuccess()
} catch (ex: BadRequestHttpError) { } catch (ex: BadRequestHttpError) {
@ -174,20 +169,15 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun signInByMailCode(mailLoginToken: String, code: String): String { override suspend fun signInByMailCode(mailLoginToken: String, code: String): String {
httpClient.newCall( postJsonRequest(
Request.Builder() "auth/sign-in-by-mail-code"
.url("$endpointWithoutSlashAtEnd/auth/sign-in-by-mail-code") ) { writer ->
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.beginObject()
writer.name(MAIL_LOGIN_TOKEN).value(mailLoginToken) writer.name(MAIL_LOGIN_TOKEN).value(mailLoginToken)
writer.name(RECEIVED_CODE).value(code) writer.name(RECEIVED_CODE).value(code)
writer.endObject() writer.endObject()
}) }.use {
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -214,19 +204,11 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun getStatusByMailToken(mailAuthToken: String): StatusOfMailAddressResponse { override suspend fun getStatusByMailToken(mailAuthToken: String): StatusOfMailAddressResponse {
httpClient.newCall( postJsonRequest("parent/get-status-by-mail-address") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/get-status-by-mail-address") writer.name(MAIL_AUTH_TOKEN).value(mailAuthToken)
.post(createJsonRequestBody { writer.endObject()
writer -> }.use {
writer.beginObject()
writer.name(MAIL_AUTH_TOKEN).value(mailAuthToken)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -256,34 +238,26 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
mailToken: String, parentPassword: ParentPassword, parentDevice: NewDeviceInfo, mailToken: String, parentPassword: ParentPassword, parentDevice: NewDeviceInfo,
timeZone: String, parentName: String, deviceName: String, skipClientLevel: Boolean timeZone: String, parentName: String, deviceName: String, skipClientLevel: Boolean
): AddDeviceResponse { ): AddDeviceResponse {
return httpClient.newCall( return postJsonRequest("parent/create-family") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/create-family")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(MAIL_AUTH_TOKEN).value(mailToken)
writer.name(TIMEZONE).value(timeZone)
writer.name(PARENT_NAME).value(parentName)
writer.name(DEVICE_NAME).value(deviceName)
writer.name(MAIL_AUTH_TOKEN).value(mailToken) writer.name(PARENT_PASSWORD)
writer.name(TIMEZONE).value(timeZone) parentPassword.serialize(writer)
writer.name(PARENT_NAME).value(parentName)
writer.name(DEVICE_NAME).value(deviceName)
writer.name(PARENT_PASSWORD) writer.name(PARENT_DEVICE)
parentPassword.serialize(writer) parentDevice.serialize(writer)
writer.name(PARENT_DEVICE) if (!skipClientLevel) {
parentDevice.serialize(writer) writer.name(CLIENT_LEVEL).value(ClientDataStatus.CLIENT_LEVEL_VALUE)
}
if (!skipClientLevel) { writer.endObject()
writer.name(CLIENT_LEVEL).value(ClientDataStatus.CLIENT_LEVEL_VALUE) }.use {
}
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -311,29 +285,21 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
private suspend fun signInToFamilyByMailTokenInternal( private suspend fun signInToFamilyByMailTokenInternal(
mailToken: String, parentDevice: NewDeviceInfo, deviceName: String, skipClientLevel: Boolean mailToken: String, parentDevice: NewDeviceInfo, deviceName: String, skipClientLevel: Boolean
): AddDeviceResponse { ): AddDeviceResponse {
return httpClient.newCall( return postJsonRequest("parent/sign-in-into-family") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/sign-in-into-family")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(MAIL_AUTH_TOKEN).value(mailToken)
writer.name(DEVICE_NAME).value(deviceName)
writer.name(MAIL_AUTH_TOKEN).value(mailToken) writer.name(PARENT_DEVICE)
writer.name(DEVICE_NAME).value(deviceName) parentDevice.serialize(writer)
writer.name(PARENT_DEVICE) if (!skipClientLevel) {
parentDevice.serialize(writer) writer.name(CLIENT_LEVEL).value(ClientDataStatus.CLIENT_LEVEL_VALUE)
}
if (!skipClientLevel) { writer.endObject()
writer.name(CLIENT_LEVEL).value(ClientDataStatus.CLIENT_LEVEL_VALUE) }.use {
}
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -351,24 +317,16 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun recoverPasswordByMailToken(mailToken: String, parentPassword: ParentPassword) { override suspend fun recoverPasswordByMailToken(mailToken: String, parentPassword: ParentPassword) {
httpClient.newCall( postJsonRequest("parent/recover-parent-password") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/recover-parent-password")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(MAIL_AUTH_TOKEN).value(mailToken)
writer.name(MAIL_AUTH_TOKEN).value(mailToken) writer.name(PASSWORD)
parentPassword.serialize(writer)
writer.name(PASSWORD) writer.endObject()
parentPassword.serialize(writer) }.use {
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
} }
} }
@ -384,30 +342,22 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
private suspend fun registerChildDeviceInternal( private suspend fun registerChildDeviceInternal(
registerToken: String, childDeviceInfo: NewDeviceInfo, deviceName: String, skipClientLevel: Boolean registerToken: String, childDeviceInfo: NewDeviceInfo, deviceName: String, skipClientLevel: Boolean
): AddDeviceResponse { ): AddDeviceResponse {
return httpClient.newCall( return postJsonRequest("child/add-device") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/child/add-device")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(REGISTER_TOKEN).value(registerToken)
writer.name(REGISTER_TOKEN).value(registerToken) writer.name(CHILD_DEVICE)
childDeviceInfo.serialize(writer)
writer.name(CHILD_DEVICE) writer.name(DEVICE_NAME).value(deviceName)
childDeviceInfo.serialize(writer)
writer.name(DEVICE_NAME).value(deviceName) if (!skipClientLevel) {
writer.name(CLIENT_LEVEL).value(ClientDataStatus.CLIENT_LEVEL_VALUE)
}
if (!skipClientLevel) { writer.endObject()
writer.name(CLIENT_LEVEL).value(ClientDataStatus.CLIENT_LEVEL_VALUE) }.use {
}
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -425,13 +375,7 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun pushChanges(request: ActionUploadRequest): ActionUploadResponse { override suspend fun pushChanges(request: ActionUploadRequest): ActionUploadResponse {
httpClient.newCall( postJsonRequest("sync/push-actions") { request.serialize(it) }.use {
Request.Builder()
.url("$endpointWithoutSlashAtEnd/sync/push-actions")
.post(createJsonRequestBody{ request.serialize(it) })
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -443,24 +387,16 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun pullChanges(deviceAuthToken: String, status: ClientDataStatus): ServerDataStatus { override suspend fun pullChanges(deviceAuthToken: String, status: ClientDataStatus): ServerDataStatus {
httpClient.newCall( postJsonRequest("sync/pull-status") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/sync/pull-status")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) writer.name(STATUS)
status.serialize(writer)
writer.name(STATUS) writer.endObject()
status.serialize(writer) }.use {
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -472,23 +408,15 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun createAddDeviceToken(deviceAuthToken: String, parentUserId: String, parentPasswordSecondHash: String): CreateAddDeviceTokenResponse { override suspend fun createAddDeviceToken(deviceAuthToken: String, parentUserId: String, parentPasswordSecondHash: String): CreateAddDeviceTokenResponse {
httpClient.newCall( postJsonRequest("parent/create-add-device-token") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/create-add-device-token")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.name(PARENT_ID).value(parentUserId)
writer.name(PARENT_PASSWORD_SECOND_HASH).value(parentPasswordSecondHash)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) writer.endObject()
writer.name(PARENT_ID).value(parentUserId) }.use {
writer.name(PARENT_PASSWORD_SECOND_HASH).value(parentPasswordSecondHash)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
it.assertSuccess() it.assertSuccess()
val body = it.body!! val body = it.body!!
@ -500,24 +428,14 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun canDoPurchase(deviceAuthToken: String): CanDoPurchaseStatus { override suspend fun canDoPurchase(deviceAuthToken: String): CanDoPurchaseStatus {
httpClient.newCall( postJsonRequest("purchase/can-do-purchase") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/purchase/can-do-purchase")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.name("type").value("googleplay")
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.name("type").value("googleplay")
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response ->
writer.endObject()
}.use { response ->
response.assertSuccess() response.assertSuccess()
return Threads.network.executeAndWait { return Threads.network.executeAndWait {
@ -529,23 +447,15 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun finishPurchaseByGooglePlay(receipt: String, signature: String, deviceAuthToken: String) { override suspend fun finishPurchaseByGooglePlay(receipt: String, signature: String, deviceAuthToken: String) {
httpClient.newCall( postJsonRequest("purchase/finish-purchase-by-google-play") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/purchase/finish-purchase-by-google-play")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name("receipt").value(receipt)
writer.name("signature").value(signature)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.name("receipt").value(receipt) writer.endObject()
writer.name("signature").value(signature) }.use {
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response -> response ->
response.assertSuccess() response.assertSuccess()
@ -553,42 +463,24 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun linkParentMailAddress(mailAuthToken: String, deviceAuthToken: String, parentUserId: String, secondPasswordHash: String) { override suspend fun linkParentMailAddress(mailAuthToken: String, deviceAuthToken: String, parentUserId: String, secondPasswordHash: String) {
httpClient.newCall( postJsonRequest("parent/link-mail-address") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/link-mail-address")
.post(createJsonRequestBody {
writer ->
writer.beginObject() writer.name(MAIL_AUTH_TOKEN).value(mailAuthToken)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.name(MAIL_AUTH_TOKEN).value(mailAuthToken) writer.name(PARENT_USER_ID).value(parentUserId)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) writer.name(PARENT_PASSWORD_SECOND_HASH).value(secondPasswordHash)
writer.name(PARENT_USER_ID).value(parentUserId)
writer.name(PARENT_PASSWORD_SECOND_HASH).value(secondPasswordHash)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response ->
writer.endObject()
}.use { response ->
response.assertSuccess() response.assertSuccess()
} }
} }
override suspend fun updatePrimaryDevice(request: UpdatePrimaryDeviceRequest): UpdatePrimaryDeviceResponse { override suspend fun updatePrimaryDevice(request: UpdatePrimaryDeviceRequest): UpdatePrimaryDeviceResponse {
httpClient.newCall( postJsonRequest("child/update-primary-device") {
Request.Builder() request.serialize(it)
.url("$endpointWithoutSlashAtEnd/child/update-primary-device") }.use { response ->
.post(createJsonRequestBody { writer ->
request.serialize(writer)
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response ->
response.assertSuccess() response.assertSuccess()
return Threads.network.executeAndWait { return Threads.network.executeAndWait {
@ -600,19 +492,13 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun requestSignOutAtPrimaryDevice(deviceAuthToken: String) { override suspend fun requestSignOutAtPrimaryDevice(deviceAuthToken: String) {
httpClient.newCall( postJsonRequest("child/logout-at-primary-device") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/child/logout-at-primary-device")
.post(createJsonRequestBody { writer ->
writer.beginObject()
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.endObject() writer.endObject()
}) }.use { response ->
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use { response ->
response.assertSuccess() response.assertSuccess()
return Threads.network.executeAndWait { return Threads.network.executeAndWait {
@ -624,58 +510,34 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
override suspend fun reportDeviceRemoved(deviceAuthToken: String) { override suspend fun reportDeviceRemoved(deviceAuthToken: String) {
httpClient.newCall( postJsonRequest("sync/report-removed") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/sync/report-removed") writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
.post(createJsonRequestBody { writer -> writer.endObject()
writer.beginObject() }.use { response ->
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response ->
response.assertSuccess() response.assertSuccess()
} }
} }
override suspend fun removeDevice(deviceAuthToken: String, parentUserId: String, parentPasswordSecondHash: String, deviceId: String) { override suspend fun removeDevice(deviceAuthToken: String, parentUserId: String, parentPasswordSecondHash: String, deviceId: String) {
httpClient.newCall( postJsonRequest("parent/remove-device") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/remove-device") writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
.post(createJsonRequestBody { writer -> writer.name(PARENT_USER_ID).value(parentUserId)
writer.beginObject() writer.name(PARENT_PASSWORD_SECOND_HASH).value(parentPasswordSecondHash)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) writer.name(DEVICE_ID).value(deviceId)
writer.name(PARENT_USER_ID).value(parentUserId) writer.endObject()
writer.name(PARENT_PASSWORD_SECOND_HASH).value(parentPasswordSecondHash) }.use { response ->
writer.name(DEVICE_ID).value(deviceId)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response ->
response.assertSuccess() response.assertSuccess()
} }
} }
override suspend fun isDeviceRemoved(deviceAuthToken: String): Boolean { override suspend fun isDeviceRemoved(deviceAuthToken: String): Boolean {
httpClient.newCall( postJsonRequest("sync/is-device-removed") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/sync/is-device-removed") writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
.post(createJsonRequestBody { writer -> writer.endObject()
writer.beginObject() }.use { response ->
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use {
response ->
response.assertSuccess() response.assertSuccess()
return Threads.network.executeAndWait { return Threads.network.executeAndWait {
@ -701,20 +563,14 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
parentUserId: String, parentUserId: String,
parentPasswordSecondHash: String parentPasswordSecondHash: String
): String { ): String {
httpClient.newCall( postJsonRequest("parent/create-identity-token") { writer ->
Request.Builder() writer.beginObject()
.url("$endpointWithoutSlashAtEnd/parent/create-identity-token") writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken)
.post(createJsonRequestBody { writer -> writer.name(PARENT_USER_ID).value(parentUserId)
writer.beginObject() writer.name(PARENT_PASSWORD_SECOND_HASH).value(parentPasswordSecondHash)
writer.name(DEVICE_AUTH_TOKEN).value(deviceAuthToken) writer.name("purpose").value("purchase")
writer.name(PARENT_USER_ID).value(parentUserId) writer.endObject()
writer.name(PARENT_PASSWORD_SECOND_HASH).value(parentPasswordSecondHash) }.use { response ->
writer.name("purpose").value("purchase")
writer.endObject()
})
.header("Content-Encoding", "gzip")
.build()
).waitForResponse().use { response ->
response.assertSuccess() response.assertSuccess()
return Threads.network.executeAndWait { return Threads.network.executeAndWait {
@ -734,4 +590,17 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
} }
} }
private suspend fun postJsonRequest(
path: String,
requestBody: (writer: JsonWriter) -> Unit
): Response {
return httpClient.newCall(
Request.Builder()
.url("$endpointWithoutSlashAtEnd/$path")
.post(createJsonRequestBody(requestBody))
.header("Content-Encoding", "gzip")
.build()
).waitForResponse()
}
} }