[API] Include device object in each request.

This commit is contained in:
Kuba Szczodrzyński 2020-01-20 19:43:14 +01:00
parent d5ae4b7ec9
commit 1035e411ab
7 changed files with 73 additions and 50 deletions

View File

@ -55,32 +55,34 @@ class SzkolnyApi(val app: App) {
api = retrofit.create()
}
private fun getDevice() = run {
val config = app.config
val device = ApiRequest.Device(
osType = "Android",
osVersion = Build.VERSION.RELEASE,
hardware = "${Build.MANUFACTURER} ${Build.MODEL}",
pushToken = app.config.sync.tokenApp,
appVersion = BuildConfig.VERSION_NAME,
appType = BuildConfig.BUILD_TYPE,
appVersionCode = BuildConfig.VERSION_CODE,
syncInterval = app.config.sync.interval
)
device.toString().md5().let {
if (it == config.hash)
null
else {
config.hash = it
device
}
}
}
fun getEvents(profiles: List<Profile>, notifications: List<Notification>, blacklistedIds: List<Long>): List<EventFull> {
val teams = app.db.teamDao().allNow
val response = api.serverSync(ServerSyncRequest(
deviceId = app.deviceId,
device = run {
val config = app.config
val device = ServerSyncRequest.Device(
osType = "Android",
osVersion = Build.VERSION.RELEASE,
hardware = "${Build.MANUFACTURER} ${Build.MODEL}",
pushToken = app.config.sync.tokenApp,
appVersion = BuildConfig.VERSION_NAME,
appType = BuildConfig.BUILD_TYPE,
appVersionCode = BuildConfig.VERSION_CODE,
syncInterval = app.config.sync.interval
)
device.toString().md5().let {
if (it == config.hash)
null
else {
config.hash = it
device
}
}
},
device = getDevice(),
userCodes = profiles.map { it.userCode },
users = profiles.mapNotNull { profile ->
val config = app.config.getFor(profile.id)
@ -131,6 +133,7 @@ class SzkolnyApi(val app: App) {
return api.shareEvent(EventShareRequest(
deviceId = app.deviceId,
device = getDevice(),
sharedByName = event.sharedByName,
shareTeamCode = team.code,
event = event
@ -142,6 +145,7 @@ class SzkolnyApi(val app: App) {
return api.shareEvent(EventShareRequest(
deviceId = app.deviceId,
device = getDevice(),
sharedByName = event.sharedByName,
unshareTeamCode = team.code,
eventId = event.id
@ -154,8 +158,9 @@ class SzkolnyApi(val app: App) {
fun pairBrowser(browserId: String?, pairToken: String?, onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> {
val response = api.webPush(WebPushRequest(
action = "pairBrowser",
deviceId = app.deviceId,
device = getDevice(),
action = "pairBrowser",
browserId = browserId,
pairToken = pairToken
)).execute().body()
@ -170,8 +175,9 @@ class SzkolnyApi(val app: App) {
fun listBrowsers(onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> {
val response = api.webPush(WebPushRequest(
action = "listBrowsers",
deviceId = app.deviceId
deviceId = app.deviceId,
device = getDevice(),
action = "listBrowsers"
)).execute().body()
return response?.data?.browsers ?: emptyList()
@ -179,8 +185,9 @@ class SzkolnyApi(val app: App) {
fun unpairBrowser(browserId: String): List<WebPushResponse.Browser> {
val response = api.webPush(WebPushRequest(
action = "unpairBrowser",
deviceId = app.deviceId,
device = getDevice(),
action = "unpairBrowser",
browserId = browserId
)).execute().body()
@ -190,6 +197,7 @@ class SzkolnyApi(val app: App) {
fun errorReport(errors: List<ErrorReportRequest.Error>): ApiResponse<Nothing>? {
return api.errorReport(ErrorReportRequest(
deviceId = app.deviceId,
device = getDevice(),
appVersion = BuildConfig.VERSION_NAME,
errors = errors
)).execute().body()
@ -198,6 +206,7 @@ class SzkolnyApi(val app: App) {
fun unregisterAppUser(userCode: String): ApiResponse<Nothing>? {
return api.appUser(AppUserRequest(
deviceId = app.deviceId,
device = getDevice(),
userCode = userCode
)).execute().body()
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) Kuba Szczodrzyński 2020-1-20.
*/
package pl.szczodrzynski.edziennik.data.api.szkolny.request
open class ApiRequest(
open val deviceId: String,
open val device: Device? = null
) {
data class Device(
val osType: String,
val osVersion: String,
val hardware: String,
val pushToken: String?,
val appVersion: String,
val appType: String,
val appVersionCode: Int,
val syncInterval: Int
)
}

View File

@ -5,8 +5,9 @@
package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class AppUserRequest(
val action: String = "unregister",
override val deviceId: String,
override val device: Device? = null,
val deviceId: String,
val action: String = "unregister",
val userCode: String
)
) : ApiRequest(deviceId, device)

View File

@ -5,10 +5,12 @@
package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class ErrorReportRequest(
val deviceId: String,
override val deviceId: String,
override val device: Device? = null,
val appVersion: String,
val errors: List<Error>
) {
) : ApiRequest(deviceId, device) {
data class Error(
val id: Long,
val tag: String,
@ -21,4 +23,4 @@ data class ErrorReportRequest(
val apiResponse: String?,
val isCritical: Boolean
)
}
}

View File

@ -7,9 +7,10 @@ package pl.szczodrzynski.edziennik.data.api.szkolny.request
import pl.szczodrzynski.edziennik.data.db.full.EventFull
data class EventShareRequest (
override val deviceId: String,
override val device: Device? = null,
val action: String = "event",
val deviceId: String,
val sharedByName: String,
val shareTeamCode: String? = null,
@ -18,4 +19,4 @@ data class EventShareRequest (
val eventId: Long? = null,
val event: EventFull? = null
)
) : ApiRequest(deviceId, device)

View File

@ -5,26 +5,14 @@
package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class ServerSyncRequest(
val deviceId: String,
val device: Device? = null,
override val deviceId: String,
override val device: Device? = null,
val userCodes: List<String>,
val users: List<User>? = null,
val notifications: List<Notification>? = null
) {
data class Device(
val osType: String,
val osVersion: String,
val hardware: String,
val pushToken: String?,
val appVersion: String,
val appType: String,
val appVersionCode: Int,
val syncInterval: Int
)
) : ApiRequest(deviceId, device) {
data class User(
val userCode: String,
val studentName: String,

View File

@ -5,10 +5,11 @@
package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class WebPushRequest(
override val deviceId: String,
override val device: Device? = null,
val action: String,
val deviceId: String,
val browserId: String? = null,
val pairToken: String? = null
)
) : ApiRequest(deviceId, device)