forked from dim13/unifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vouchers.go
129 lines (106 loc) · 2.59 KB
/
vouchers.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
// Copyright (c) 2014 The unifi Authors. All rights reserved.
// Use of this source code is governed by ISC-style license
// that can be found in the LICENSE file.
package unifi
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/url"
"reflect"
)
type VoucherMap map[string]Voucher
type Voucher struct {
u *Unifi
AdminName string `json:"admin_name"`
Code string
CreateTime int `json:"create_time"`
Duration int
ForHotspot bool `json:"for_hotspot"`
Note string
QosOverwrite bool `json:"qos_overwrite"`
Quota int
SiteID string `json:"site_id"`
Used int
}
type NewVoucher struct {
Cmd string `json:"cmd"`
Expire string `json:"expire"`
ExpireNumber string `json:"expire_number"`
ExpireUnit string `json:"expire_unit"`
N string `json:"n"`
Note string `json:"note"`
Quota string `json:"quota"`
}
//Value with parameters for create New Voucher
var Nv NewVoucher
func (u *Unifi) Voucher(site *Site) ([]Voucher, error) {
var response struct {
Data []Voucher
Meta meta
}
err := u.parse(site, "stat/voucher", nil, &response)
for i := range response.Data {
response.Data[i].u = u
}
return response.Data, err
}
func (u *Unifi) VoucherMap(site *Site) (VoucherMap, error) {
vouch, err := u.Voucher(site)
if err != nil {
return nil, err
}
m := make(VoucherMap)
for _, a := range vouch {
m[a.Code] = a
}
return m, nil
}
//Functions creating new Vouchers
func (u *Unifi) NewVoucher(site *Site, nv NewVoucher) ([]Voucher, error) {
var response struct {
Data []Voucher
Meta meta
}
Nv = nv
err := u.parseNewVoucher(site, "cmd/hotspot", &response)
return response.Data, err
}
func (u *Unifi) apicmdNewVoucher(site *Site, cmd string) ([]byte, error) {
jsonData := Nv
// Setup url
cmdurl := u.apiURL
cmdurl += fmt.Sprintf("s/%s/%s", site.Name, cmd)
data, err := json.Marshal(jsonData)
if err != nil {
return nil, err
}
val := url.Values{"json": {string(data)}}
resp, err := u.client.PostForm(cmdurl, val)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (u *Unifi) parseNewVoucher(site *Site, cmd string, v interface{}) error {
body, err := u.apicmdNewVoucher(site, cmd)
if err != nil {
return err
}
if err := json.Unmarshal(body, &v); err != nil {
log.Println(cmd)
log.Println(string(body))
return err
}
m := reflect.ValueOf(v).Elem().FieldByName("Meta").Interface().(meta)
if m.Rc != "ok" {
return fmt.Errorf("Bad request: %s", m.Rc)
}
return nil
}