-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponses.go
79 lines (66 loc) · 2.09 KB
/
responses.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
package UnifiVoucherGenerator
import "encoding/json"
//todo: add logging
type Meta struct {
Rc string `json:"rc"` // Maps the "rc" field to check if it's "ok"
}
type LoginResponse struct {
Meta Meta `json:"meta"` // Maps the "meta" field
Data []interface{} `json:"data"` // Use []interface{} for arbitrary data; adjust as needed
}
type RequestNewVoucherResponse struct {
Meta struct {
Rc string `json:"rc"`
} `json:"meta"`
Data []struct {
CreateTime int `json:"create_time"`
} `json:"data"`
}
// UnifiVoucher Define struct for each item in the data array
type UnifiVoucher struct {
Duration int `json:"duration"`
QosOverwrite bool `json:"qos_overwrite"`
Note string `json:"note"`
Code string `json:"code"`
ForHotspot bool `json:"for_hotspot"`
CreateTime int64 `json:"create_time"`
Quota int `json:"quota"`
SiteID string `json:"site_id"`
ID string `json:"_id"`
AdminName string `json:"admin_name"`
Used int `json:"used"`
Status string `json:"status"`
StatusExpires int `json:"status_expires"`
}
type UnifiVouchers []UnifiVoucher
func processResponse[T any](body string) (*T, error) {
var response T
err := json.Unmarshal([]byte(body), &response)
if err != nil {
return nil, err
}
return &response, nil
}
// ProcessLoginResponse converts the JSON response from the login endpoint into a struct
func processLoginResponse(body string) (*LoginResponse, error) {
return processResponse[LoginResponse](body)
}
func processNewVoucherRequestResponse(body string) (RequestNewVoucherResponse, error) {
t, err := processResponse[RequestNewVoucherResponse](body)
if err != nil {
return RequestNewVoucherResponse{}, err
}
return *t, nil
}
func processVoucherListResponse(body string) (UnifiVouchers, error) {
t, err := processResponse[VoucherListResponse](body)
if err != nil {
return UnifiVouchers{}, err
}
return t.Data, nil
}
// VoucherListResponse Define struct for the top-level JSON object
type VoucherListResponse struct {
Meta Meta `json:"meta"`
Data UnifiVouchers `json:"data"`
}