aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt
blob: 3f90c4c94a6983db4eaa88d1cbee7dce449852cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * Traccar
 * Traccar GPS tracking server API documentation. To use the API you need to have a server instance. For testing purposes you can use one of free [demo servers](https://www.traccar.org/demo-server/). For production use you can install your own server or get a [subscription service](https://www.traccar.org/product/tracking-server/).
 *
 * OpenAPI spec version: 4.14
 * Contact: support@traccar.org
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */
package mx.trackermap.TrackerMap.client.apis

import com.russhwolf.settings.Settings
import mx.trackermap.TrackerMap.client.models.User
import mx.trackermap.TrackerMap.client.infrastructure.*

const val SERVER_URL_KEY = "server_url"
const val ACCESS_TOKEN_KEY = "access_token"

class SessionApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {

    /**
     * Close the Session
     *
     * @return void
     */
    suspend fun sessionDelete() {
        val localVariableConfig = RequestConfig(
            RequestMethod.DELETE,
            "/session"
        )
        val response = request<Any?>(
            localVariableConfig
        )

        return when (response.responseType) {
            ResponseType.Success -> {
                val settings = Settings()
                settings.remove(ACCESS_TOKEN_KEY)
            }
            ResponseType.Informational -> TODO()
            ResponseType.Redirection -> TODO()
            ResponseType.ClientError -> throw ClientException(
                (response as ClientError<*>).body as? String ?: "Client error"
            )
            ResponseType.ServerError -> throw ServerException(
                (response as ServerError<*>).message ?: "Server error"
            )
        }
    }

    /**
     * Fetch Session information
     *
     * @param token  (optional)
     * @return User
     */
    @Suppress("UNCHECKED_CAST")
    suspend fun sessionGet(token: String? = null): User {
        val query: MutableMap<String, List<String>> = mutableMapOf()
        token?.let { query["userId"] = listOf(it) }
        val localVariableConfig = RequestConfig(
            RequestMethod.GET,
            "/session", query = query
        )
        val response = request<User>(
            localVariableConfig
        )

        return when (response.responseType) {
            ResponseType.Success -> (response as Success<*>).data as User
            ResponseType.Informational -> TODO()
            ResponseType.Redirection -> TODO()
            ResponseType.ClientError -> throw ClientException(
                (response as ClientError<*>).body as? String ?: "Client error"
            )
            ResponseType.ServerError -> throw ServerException(
                (response as ServerError<*>).message ?: "Server error"
            )
        }
    }

    /**
     * Create a new Session
     *
     * @param email
     * @param password
     * @return User
     */
    @Suppress("UNCHECKED_CAST")
    suspend fun sessionPost(email: String, password: String): User {
        val localVariableBody = mapOf("email" to email, "password" to password)
        val localVariableHeaders = mapOf("Content-Type" to "application/x-www-form-urlencoded")
        val localVariableConfig = RequestConfig(
            RequestMethod.POST,
            "/session", headers = localVariableHeaders
        )
        val response = request<User>(
            localVariableConfig, localVariableBody
        )

        return when (response.responseType) {
            ResponseType.Success -> {
                val cookie = response.headers
                    .values
                    .flatten()
                    .find { it.contains("JSESSIONID") }!!
                    .replace("; Path=/", "")
                this.token = cookie
                val settings = Settings()
                settings.putString(ACCESS_TOKEN_KEY, cookie)
                (response as Success<*>).data as User
            }
            ResponseType.Informational -> TODO()
            ResponseType.Redirection -> TODO()
            ResponseType.ClientError -> throw ClientException(
                (response as ClientError<*>).body as? String ?: "Client error"
            )
            ResponseType.ServerError -> throw ServerException(
                (response as ServerError<*>).message ?: "Server error"
            )
        }
    }
}