From 1035e411abd22156ac3a6af4a6b78b3daf2642ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 20 Jan 2020 19:43:14 +0100 Subject: [PATCH] [API] Include device object in each request. --- .../edziennik/data/api/szkolny/SzkolnyApi.kt | 59 +++++++++++-------- .../data/api/szkolny/request/ApiRequest.kt | 21 +++++++ .../api/szkolny/request/AppUserRequest.kt | 7 ++- .../api/szkolny/request/ErrorReportRequest.kt | 8 ++- .../api/szkolny/request/EventShareRequest.kt | 5 +- .../api/szkolny/request/ServerSyncRequest.kt | 18 +----- .../api/szkolny/request/WebPushRequest.kt | 5 +- 7 files changed, 73 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ApiRequest.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt index 1a59e3ea..3997e27a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt @@ -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, notifications: List, blacklistedIds: List): List { 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) -> Unit)? = null): List { 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) -> Unit)? = null): List { 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 { 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): ApiResponse? { 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? { return api.appUser(AppUserRequest( deviceId = app.deviceId, + device = getDevice(), userCode = userCode )).execute().body() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ApiRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ApiRequest.kt new file mode 100644 index 00000000..2794a39a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ApiRequest.kt @@ -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 + ) +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/AppUserRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/AppUserRequest.kt index 30c95a13..0fde00b7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/AppUserRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/AppUserRequest.kt @@ -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) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ErrorReportRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ErrorReportRequest.kt index 3fcdd848..18a2cc3f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ErrorReportRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ErrorReportRequest.kt @@ -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 -) { +) : 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 ) -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt index 31c4a72e..cf7cd776 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt @@ -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) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ServerSyncRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ServerSyncRequest.kt index 3902153a..3624aece 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ServerSyncRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/ServerSyncRequest.kt @@ -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, val users: List? = null, val notifications: List? = 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, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/WebPushRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/WebPushRequest.kt index 209a63a8..9be53747 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/WebPushRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/WebPushRequest.kt @@ -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 -) \ No newline at end of file +) : ApiRequest(deviceId, device)