forked from dim13/unifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
104 lines (83 loc) · 1.94 KB
/
user.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
// 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 (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/url"
"reflect"
)
type User struct {
FirstSeen int `json:"first_seen"`
Hostname string
IsGuest bool `json:"is_guest"`
LastSeen int `json:"last_seen"`
Mac string
Oui string
}
// Request structs
type NewUser struct {
UserGroupID string `json:"usergroup_id"`
Mac string `json:"mac"`
Name string `json:"name"`
}
type Data struct {
Data NewUser `json:"data"`
}
type Objects struct {
Objects []Data `json:"objects"`
}
//Value with parameters for create New User
var Nu NewUser
//Functions creating new Users
func (u *Unifi) NewUser(site *Site, nu NewUser) ([]User, error) {
var response struct {
Data []User
Meta meta
}
Nu = nu
err := u.parseNewUser(site, "group/user", &response)
return response.Data, err
}
func (u *Unifi) apicmdNewUser(site *Site, cmd string) ([]byte, error) {
jsonData := Objects{[]Data{{Nu}}}
// 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
}
fmt.Println(bytes.NewBuffer(data))
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) parseNewUser(site *Site, cmd string, v interface{}) error {
body, err := u.apicmdNewUser(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
}