This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomescriptArg.go
152 lines (140 loc) · 4.3 KB
/
homescriptArg.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package sdk
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
/* Base structs */
type HmsArgInputType string
// Datatypes which a Homescript argument can use
// Type conversion is handled by the target Homescript
// These types act as a hint for the user and
var (
String HmsArgInputType = "string"
Number HmsArgInputType = "number"
Boolean HmsArgInputType = "boolean"
)
type HmsArgDisplay string
var (
TypeDefault HmsArgDisplay = "type_default" // Uses a normal input field matching the specified data type
StringSwitches HmsArgDisplay = "string_switches" // Shows a list of switches from which the user can select one as a string
BooleanYesNo HmsArgDisplay = "boolean_yes_no" // Uses `yes` and `no` as substitutes for true and false
BooleanOnOff HmsArgDisplay = "boolean_on_off" // Uses `on` and `off` as substitutes for true and false
NumberHour HmsArgDisplay = "number_hour" // Displays a hour picker (0 <= h <= 24)
NumberMinute HmsArgDisplay = "number_minute" // Displays a minute picker (0 <= m <= 60)
)
// Represents a Homescript with its arguments
type HomescriptWithArguments struct {
Data Homescript `json:"data"`
Arguments []HomescriptArg `json:"arguments"`
}
type HomescriptArg struct {
Id uint `json:"id"` // The Id is automatically generated
Data HomescriptArgData `json:"data"` // The main data of the argument
}
type HomescriptArgData struct {
ArgKey string `json:"argKey"` // The unique key of the argument
HomescriptId string `json:"homescriptId"` // The Homescript to which the argument belongs to
Prompt string `json:"prompt"` // What the user will be prompted
InputType HmsArgInputType `json:"inputType"` // Which data type is expected
Display HmsArgDisplay `json:"display"` // How the prompt will look like
}
/* Specific API responses*/
// Is returned when a new Hms argument was created successfully
type AddedHomescriptArgResponse struct {
NewId uint `json:"id"`
Response GenericResponse `json:"response"`
}
/* Functions */
// Returns a slice of Homescripts arguments which belong to a given Homescript
/** Errors
- nil
- ErrNotInitialized
- ErrConnFailed
- ErrReadResponseBody
- ErrInvalidCredentials
- ErrPermissionDenied
- PrepareRequest errors
- Unknown
*/
func (c *Connection) ListHomescriptArgsOfHmsId(homescriptId string) ([]HomescriptArg, error) {
if !c.ready {
return nil, ErrNotInitialized
}
req, err := c.prepareRequest(fmt.Sprintf("/api/homescript/arg/list/of/%s", url.PathEscape(homescriptId)), Get, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, ErrConnFailed
}
defer res.Body.Close()
switch res.StatusCode {
case 200:
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, ErrReadResponseBody
}
var parsedBody []HomescriptArg
if err := json.Unmarshal(resBody, &parsedBody); err != nil {
return nil, ErrReadResponseBody
}
return parsedBody, nil
case 401:
return nil, ErrInvalidCredentials
case 422:
return nil, ErrUnprocessableEntity
case 403:
return nil, ErrPermissionDenied
}
return nil, fmt.Errorf("unknown response code: %s", res.Status)
}
// Returns a slice of Homescripts with their arguments
/** Errors
- nil
- ErrNotInitialized
- ErrConnFailed
- ErrReadResponseBody
- ErrInvalidCredentials
- ErrPermissionDenied
- PrepareRequest errors
- Unknown
*/
func (c *Connection) ListHomescriptWithArgs() ([]HomescriptWithArguments, error) {
if !c.ready {
return nil, ErrNotInitialized
}
req, err := c.prepareRequest("/api/homescript/list/personal/complete", Get, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, ErrConnFailed
}
defer res.Body.Close()
switch res.StatusCode {
case 200:
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, ErrReadResponseBody
}
var parsedBody []HomescriptWithArguments
if err := json.Unmarshal(resBody, &parsedBody); err != nil {
return nil, ErrReadResponseBody
}
return parsedBody, nil
case 401:
return nil, ErrInvalidCredentials
case 422:
return nil, ErrUnprocessableEntity
case 403:
return nil, ErrPermissionDenied
}
return nil, fmt.Errorf("unknown response code: %s", res.Status)
}