From 5bd8ab38cd1549b267ff93fd1ea93f1c0ca9d8ab Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 13 Jan 2022 20:22:36 -0600 Subject: Implemented base for exporting XLSX reports --- .../TrackerMap/client/apis/ReportsApi.kt | 164 ++++++++++++++++----- 1 file changed, 126 insertions(+), 38 deletions(-) (limited to 'shared/src/commonMain') diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt index 43feaa6..b4d6f74 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt @@ -11,6 +11,7 @@ */ package mx.trackermap.TrackerMap.client.apis +import io.ktor.content.* import mx.trackermap.TrackerMap.client.models.Event import mx.trackermap.TrackerMap.client.models.Position import mx.trackermap.TrackerMap.client.models.Stop @@ -19,7 +20,7 @@ import mx.trackermap.TrackerMap.client.models.ReportTrips import mx.trackermap.TrackerMap.client.infrastructure.* -class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiClient(basePath) { +class ReportsApi(basePath: String = "https://demo.traccar.org/api") : ApiClient(basePath) { /** * Fetch a list of Events within the time period for the Devices or Groups @@ -31,27 +32,38 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api * @param type % can be used to return events of all types (optional) * @return kotlin.Array */ - @Suppress("UNCHECKED_CAST") - suspend fun reportsEventsGet( + private suspend fun reportsEventsGetBase( from: String, to: String, - deviceId: Int - ): kotlin.Array { + deviceId: Int, + xlsx: Boolean = false + ): Any { val localVariableQuery: MultiValueMap = mapOf( "deviceId" to toMultiValue(listOf(deviceId), "multi"), "from" to listOf(from), "to" to listOf(to) ) + val localVariableHeaders = mutableMapOf() + if (xlsx) { + localVariableHeaders["Accept"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } val localVariableConfig = RequestConfig( RequestMethod.GET, - "/reports/events", query = localVariableQuery - ) - val response = request>( - localVariableConfig + "/reports/events", query = localVariableQuery, + headers = localVariableHeaders ) + val response = if (xlsx) { + request( + localVariableConfig + ) + } else { + request>( + localVariableConfig + ) + } return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array + ResponseType.Success -> (response as Success<*>).data!! ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException( @@ -63,6 +75,24 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api } } + @Suppress("UNCHECKED_CAST") + suspend fun reportsEventsGet( + from: String, + to: String, + deviceId: Int, + ): Array { + return reportsEventsGetBase(from, to, deviceId, false) as Array + } + + @Suppress("UNCHECKED_CAST") + suspend fun reportsEventsGetXlsx( + from: String, + to: String, + deviceId: Int, + ): ByteArray { + return reportsEventsGetBase(from, to, deviceId, true) as ByteArray + } + /** * Fetch a list of Positions within the time period for the Devices or Groups * At least one _deviceId_ or one _groupId_ must be passed @@ -72,27 +102,38 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api * @param groupId (optional) * @return kotlin.Array */ - @Suppress("UNCHECKED_CAST") - suspend fun reportsRouteGet( + private suspend fun reportsRouteGetBase( from: String, to: String, - deviceId: Int - ): kotlin.Array { + deviceId: Int, + xlsx: Boolean = false + ): Any { val localVariableQuery: MultiValueMap = mapOf( "deviceId" to toMultiValue(listOf(deviceId), "multi"), "from" to listOf(from), "to" to listOf(to) ) + val localVariableHeaders = mutableMapOf() + if (xlsx) { + localVariableHeaders["Accept"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } val localVariableConfig = RequestConfig( RequestMethod.GET, - "/reports/route", query = localVariableQuery - ) - val response = request>( - localVariableConfig + "/reports/route", query = localVariableQuery, + headers = localVariableHeaders ) + val response = if (xlsx) { + request( + localVariableConfig + ) + } else { + request>( + localVariableConfig + ) + } return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array + ResponseType.Success -> (response as Success<*>).data!! ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException( @@ -104,6 +145,24 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api } } + @Suppress("UNCHECKED_CAST") + suspend fun reportsRouteGet( + from: String, + to: String, + deviceId: Int, + ): Array { + return reportsRouteGetBase(from, to, deviceId, false) as Array + } + + @Suppress("UNCHECKED_CAST") + suspend fun reportsRouteGetXlsx( + from: String, + to: String, + deviceId: Int, + ): ByteArray { + return reportsRouteGetBase(from, to, deviceId, true) as ByteArray + } + /** * Fetch a list of ReportStops within the time period for the Devices or Groups * At least one _deviceId_ or one _groupId_ must be passed @@ -113,27 +172,38 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api * @param groupId (optional) * @return kotlin.Array */ - @Suppress("UNCHECKED_CAST") - suspend fun reportsStopsGet( + private suspend fun reportsStopsGetBase( from: String, to: String, - deviceId: Int - ): kotlin.Array { + deviceId: Int, + xlsx: Boolean = false + ): Any { val localVariableQuery: MultiValueMap = mapOf( "deviceId" to toMultiValue(listOf(deviceId), "multi"), "from" to listOf(from), "to" to listOf(to) ) + val localVariableHeaders = mutableMapOf() + if (xlsx) { + localVariableHeaders["Accept"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } val localVariableConfig = RequestConfig( RequestMethod.GET, - "/reports/stops", query = localVariableQuery - ) - val response = request>( - localVariableConfig + "/reports/stops", query = localVariableQuery, + headers = localVariableHeaders ) + val response = if (xlsx) { + request( + localVariableConfig + ) + } else { + request>( + localVariableConfig + ) + } return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array + ResponseType.Success -> (response as Success<*>).data!! ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException( @@ -145,6 +215,24 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api } } + @Suppress("UNCHECKED_CAST") + suspend fun reportsStopsGet( + from: String, + to: String, + deviceId: Int, + ): Array { + return reportsStopsGetBase(from, to, deviceId, false) as Array + } + + @Suppress("UNCHECKED_CAST") + suspend fun reportsStopsGetXlsx( + from: String, + to: String, + deviceId: Int, + ): ByteArray { + return reportsStopsGetBase(from, to, deviceId, true) as ByteArray + } + /** * Fetch a list of ReportSummary within the time period for the Devices or Groups * At least one _deviceId_ or one _groupId_ must be passed @@ -158,9 +246,9 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api suspend fun reportsSummaryGet( from: java.time.LocalDateTime, to: java.time.LocalDateTime, - deviceId: kotlin.Array? = null, - groupId: kotlin.Array? = null - ): kotlin.Array { + deviceId: Array? = null, + groupId: Array? = null + ): Array { val localVariableQuery: MultiValueMap = mapOf( "deviceId" to toMultiValue(deviceId!!.toList(), "multi"), "groupId" to toMultiValue(groupId!!.toList(), "multi"), @@ -171,12 +259,12 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api RequestMethod.GET, "/reports/summary", query = localVariableQuery ) - val response = request>( + val response = request>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array + ResponseType.Success -> (response as Success<*>).data as Array ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException( @@ -201,9 +289,9 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api suspend fun reportsTripsGet( from: java.time.LocalDateTime, to: java.time.LocalDateTime, - deviceId: kotlin.Array? = null, - groupId: kotlin.Array? = null - ): kotlin.Array { + deviceId: Array? = null, + groupId: Array? = null + ): Array { val localVariableQuery: MultiValueMap = mapOf( "deviceId" to toMultiValue(deviceId!!.toList(), "multi"), "groupId" to toMultiValue(groupId!!.toList(), "multi"), @@ -214,12 +302,12 @@ class ReportsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api RequestMethod.GET, "/reports/trips", query = localVariableQuery ) - val response = request>( + val response = request>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array + ResponseType.Success -> (response as Success<*>).data as Array ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException( -- cgit v1.2.3