[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() 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> { fun getEvents(profiles: List<Profile>, notifications: List<Notification>, blacklistedIds: List<Long>): List<EventFull> {
val teams = app.db.teamDao().allNow val teams = app.db.teamDao().allNow
val response = api.serverSync(ServerSyncRequest( val response = api.serverSync(ServerSyncRequest(
deviceId = app.deviceId, deviceId = app.deviceId,
device = run { device = getDevice(),
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
}
}
},
userCodes = profiles.map { it.userCode }, userCodes = profiles.map { it.userCode },
users = profiles.mapNotNull { profile -> users = profiles.mapNotNull { profile ->
val config = app.config.getFor(profile.id) val config = app.config.getFor(profile.id)
@ -131,6 +133,7 @@ class SzkolnyApi(val app: App) {
return api.shareEvent(EventShareRequest( return api.shareEvent(EventShareRequest(
deviceId = app.deviceId, deviceId = app.deviceId,
device = getDevice(),
sharedByName = event.sharedByName, sharedByName = event.sharedByName,
shareTeamCode = team.code, shareTeamCode = team.code,
event = event event = event
@ -142,6 +145,7 @@ class SzkolnyApi(val app: App) {
return api.shareEvent(EventShareRequest( return api.shareEvent(EventShareRequest(
deviceId = app.deviceId, deviceId = app.deviceId,
device = getDevice(),
sharedByName = event.sharedByName, sharedByName = event.sharedByName,
unshareTeamCode = team.code, unshareTeamCode = team.code,
eventId = event.id 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> { fun pairBrowser(browserId: String?, pairToken: String?, onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> {
val response = api.webPush(WebPushRequest( val response = api.webPush(WebPushRequest(
action = "pairBrowser",
deviceId = app.deviceId, deviceId = app.deviceId,
device = getDevice(),
action = "pairBrowser",
browserId = browserId, browserId = browserId,
pairToken = pairToken pairToken = pairToken
)).execute().body() )).execute().body()
@ -170,8 +175,9 @@ class SzkolnyApi(val app: App) {
fun listBrowsers(onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> { fun listBrowsers(onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> {
val response = api.webPush(WebPushRequest( val response = api.webPush(WebPushRequest(
action = "listBrowsers", deviceId = app.deviceId,
deviceId = app.deviceId device = getDevice(),
action = "listBrowsers"
)).execute().body() )).execute().body()
return response?.data?.browsers ?: emptyList() return response?.data?.browsers ?: emptyList()
@ -179,8 +185,9 @@ class SzkolnyApi(val app: App) {
fun unpairBrowser(browserId: String): List<WebPushResponse.Browser> { fun unpairBrowser(browserId: String): List<WebPushResponse.Browser> {
val response = api.webPush(WebPushRequest( val response = api.webPush(WebPushRequest(
action = "unpairBrowser",
deviceId = app.deviceId, deviceId = app.deviceId,
device = getDevice(),
action = "unpairBrowser",
browserId = browserId browserId = browserId
)).execute().body() )).execute().body()
@ -190,6 +197,7 @@ class SzkolnyApi(val app: App) {
fun errorReport(errors: List<ErrorReportRequest.Error>): ApiResponse<Nothing>? { fun errorReport(errors: List<ErrorReportRequest.Error>): ApiResponse<Nothing>? {
return api.errorReport(ErrorReportRequest( return api.errorReport(ErrorReportRequest(
deviceId = app.deviceId, deviceId = app.deviceId,
device = getDevice(),
appVersion = BuildConfig.VERSION_NAME, appVersion = BuildConfig.VERSION_NAME,
errors = errors errors = errors
)).execute().body() )).execute().body()
@ -198,6 +206,7 @@ class SzkolnyApi(val app: App) {
fun unregisterAppUser(userCode: String): ApiResponse<Nothing>? { fun unregisterAppUser(userCode: String): ApiResponse<Nothing>? {
return api.appUser(AppUserRequest( return api.appUser(AppUserRequest(
deviceId = app.deviceId, deviceId = app.deviceId,
device = getDevice(),
userCode = userCode userCode = userCode
)).execute().body() )).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 package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class AppUserRequest( 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 val userCode: String
) ) : ApiRequest(deviceId, device)

View File

@ -5,10 +5,12 @@
package pl.szczodrzynski.edziennik.data.api.szkolny.request package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class ErrorReportRequest( data class ErrorReportRequest(
val deviceId: String, override val deviceId: String,
override val device: Device? = null,
val appVersion: String, val appVersion: String,
val errors: List<Error> val errors: List<Error>
) { ) : ApiRequest(deviceId, device) {
data class Error( data class Error(
val id: Long, val id: Long,
val tag: String, val tag: String,
@ -21,4 +23,4 @@ data class ErrorReportRequest(
val apiResponse: String?, val apiResponse: String?,
val isCritical: Boolean 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 import pl.szczodrzynski.edziennik.data.db.full.EventFull
data class EventShareRequest ( data class EventShareRequest (
override val deviceId: String,
override val device: Device? = null,
val action: String = "event", val action: String = "event",
val deviceId: String,
val sharedByName: String, val sharedByName: String,
val shareTeamCode: String? = null, val shareTeamCode: String? = null,
@ -18,4 +19,4 @@ data class EventShareRequest (
val eventId: Long? = null, val eventId: Long? = null,
val event: EventFull? = null val event: EventFull? = null
) ) : ApiRequest(deviceId, device)

View File

@ -5,26 +5,14 @@
package pl.szczodrzynski.edziennik.data.api.szkolny.request package pl.szczodrzynski.edziennik.data.api.szkolny.request
data class ServerSyncRequest( data class ServerSyncRequest(
override val deviceId: String,
val deviceId: String, override val device: Device? = null,
val device: Device? = null,
val userCodes: List<String>, val userCodes: List<String>,
val users: List<User>? = null, val users: List<User>? = null,
val notifications: List<Notification>? = null val notifications: List<Notification>? = null
) { ) : ApiRequest(deviceId, device) {
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
)
data class User( data class User(
val userCode: String, val userCode: String,
val studentName: String, val studentName: String,

View File

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