-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathDeviceApi.kt
More file actions
91 lines (78 loc) · 3.06 KB
/
DeviceApi.kt
File metadata and controls
91 lines (78 loc) · 3.06 KB
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
package ca.cgagnier.wlednativeandroid.service.api
import ca.cgagnier.wlednativeandroid.model.Device
import ca.cgagnier.wlednativeandroid.model.wledapi.Info
import ca.cgagnier.wlednativeandroid.model.wledapi.JsonPost
import ca.cgagnier.wlednativeandroid.model.wledapi.Preset
import ca.cgagnier.wlednativeandroid.model.wledapi.State
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import java.util.concurrent.TimeUnit
interface DeviceApi {
@GET("json/info")
suspend fun getInfo(): Response<Info>
@GET("presets.json")
suspend fun getPresets(): Response<Map<String, Preset>>
@GET("json/state")
suspend fun getState(): Response<State>
@POST("json/state")
suspend fun postState(@Body state: State): Response<State>
@POST("json/state")
suspend fun postJson(@Body jsonPost: JsonPost): Response<State>
@Multipart
@POST("update")
suspend fun updateDevice(@Part binaryFile: MultipartBody.Part): Response<ResponseBody>
}
/**
* Factory for creating instances of DeviceApi.
*
* Since the base URL is dynamic per device, we can't provide a singleton Retrofit instance.
* Instead, we provide this factory to create a new DeviceApi on-demand.
*
* @param client The OkHttpClient to use for the API calls.
*/
class DeviceApiFactory(private val client: OkHttpClient) {
/**
* Create a new DeviceApi instance from a device address.
*
* @param address The address of a device to create the API for.
*/
fun create(address: String): DeviceApi {
// Normalize the address to ensure it's a valid base URL
val baseUrl = if (!address.startsWith("http://") && !address.startsWith("https://")) {
"http://$address/"
} else {
address
}
return createForDeviceAndClient(baseUrl, client)
}
/**
* Create a new DeviceApi instance for a device.
*
* @param device The device to create the API for.
*/
fun create(device: Device): DeviceApi = createForDeviceAndClient(device.getDeviceUrl(), client)
/**
* Create a new DeviceApi instance with a custom timeout.
*
* @param device The device to create the API for.
* @param timeout The custom timeout in seconds.
*/
fun create(device: Device, timeout: Long): DeviceApi {
val customClient = client.newBuilder().connectTimeout(timeout, TimeUnit.SECONDS)
.readTimeout(timeout, TimeUnit.SECONDS).writeTimeout(timeout, TimeUnit.SECONDS).build()
return createForDeviceAndClient(device.getDeviceUrl(), customClient)
}
private fun createForDeviceAndClient(address: String, client: OkHttpClient): DeviceApi =
Retrofit.Builder().baseUrl(address).client(client)
.addConverterFactory(MoshiConverterFactory.create()).build()
.create(DeviceApi::class.java)
}