diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-13 20:22:36 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-13 20:22:36 -0600 |
commit | 24bbcbb4a244f2e41c7ca3773ad1378ee422f55c (patch) | |
tree | fdf5444004231bf276054e9d513007d67f9ece77 /shared/src/commonMain/kotlin/mx/trackermap | |
parent | 2417f926d9fb58bf99d5f4f7e6221390709e4899 (diff) | |
download | etbsa-trackermap-mobile-24bbcbb4a244f2e41c7ca3773ad1378ee422f55c.tar.gz etbsa-trackermap-mobile-24bbcbb4a244f2e41c7ca3773ad1378ee422f55c.tar.bz2 etbsa-trackermap-mobile-24bbcbb4a244f2e41c7ca3773ad1378ee422f55c.zip |
Implemented base for exporting XLSX reports
Diffstat (limited to 'shared/src/commonMain/kotlin/mx/trackermap')
-rw-r--r-- | shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt | 164 |
1 files changed, 126 insertions, 38 deletions
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<Event> */ - @Suppress("UNCHECKED_CAST") - suspend fun reportsEventsGet( + private suspend fun reportsEventsGetBase( from: String, to: String, - deviceId: Int - ): kotlin.Array<Event> { + 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<String, String>() + if (xlsx) { + localVariableHeaders["Accept"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } val localVariableConfig = RequestConfig( RequestMethod.GET, - "/reports/events", query = localVariableQuery - ) - val response = request<kotlin.Array<Event>>( - localVariableConfig + "/reports/events", query = localVariableQuery, + headers = localVariableHeaders ) + val response = if (xlsx) { + request<ByteArray>( + localVariableConfig + ) + } else { + request<Array<Event>>( + localVariableConfig + ) + } return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Event> + 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<Event> { + return reportsEventsGetBase(from, to, deviceId, false) as Array<Event> + } + + @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<Position> */ - @Suppress("UNCHECKED_CAST") - suspend fun reportsRouteGet( + private suspend fun reportsRouteGetBase( from: String, to: String, - deviceId: Int - ): kotlin.Array<Position> { + 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<String, String>() + if (xlsx) { + localVariableHeaders["Accept"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } val localVariableConfig = RequestConfig( RequestMethod.GET, - "/reports/route", query = localVariableQuery - ) - val response = request<kotlin.Array<Position>>( - localVariableConfig + "/reports/route", query = localVariableQuery, + headers = localVariableHeaders ) + val response = if (xlsx) { + request<ByteArray>( + localVariableConfig + ) + } else { + request<Array<Position>>( + localVariableConfig + ) + } return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Position> + 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<Position> { + return reportsRouteGetBase(from, to, deviceId, false) as Array<Position> + } + + @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<ReportStops> */ - @Suppress("UNCHECKED_CAST") - suspend fun reportsStopsGet( + private suspend fun reportsStopsGetBase( from: String, to: String, - deviceId: Int - ): kotlin.Array<Stop> { + 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<String, String>() + if (xlsx) { + localVariableHeaders["Accept"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } val localVariableConfig = RequestConfig( RequestMethod.GET, - "/reports/stops", query = localVariableQuery - ) - val response = request<kotlin.Array<Stop>>( - localVariableConfig + "/reports/stops", query = localVariableQuery, + headers = localVariableHeaders ) + val response = if (xlsx) { + request<ByteArray>( + localVariableConfig + ) + } else { + request<Array<Stop>>( + localVariableConfig + ) + } return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Stop> + 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<Stop> { + return reportsStopsGetBase(from, to, deviceId, false) as Array<Stop> + } + + @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<kotlin.Int>? = null, - groupId: kotlin.Array<kotlin.Int>? = null - ): kotlin.Array<ReportSummary> { + deviceId: Array<Int>? = null, + groupId: Array<Int>? = null + ): Array<ReportSummary> { 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<kotlin.Array<ReportSummary>>( + val response = request<Array<ReportSummary>>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<ReportSummary> + ResponseType.Success -> (response as Success<*>).data as Array<ReportSummary> 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<kotlin.Int>? = null, - groupId: kotlin.Array<kotlin.Int>? = null - ): kotlin.Array<ReportTrips> { + deviceId: Array<Int>? = null, + groupId: Array<Int>? = null + ): Array<ReportTrips> { 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<kotlin.Array<ReportTrips>>( + val response = request<Array<ReportTrips>>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<ReportTrips> + ResponseType.Success -> (response as Success<*>).data as Array<ReportTrips> ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException( |