-
Notifications
You must be signed in to change notification settings - Fork 0
/
UcpClient.go
89 lines (79 loc) · 1.62 KB
/
UcpClient.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
package UcpClient
import (
"fmt"
"net/http"
"encoding/json"
"bytes"
"io/ioutil"
)
type Client struct {
BaseURL string
Username string
Password string
// httpClient *http.Client
}
type userOrg struct {
fullName string
id string
isActive bool
isAdmin bool
isImported bool
isOrg bool
membersCount int
name string
}
func NewBasicAuthClient(baseurl, username, password string) *Client {
return &Client{
BaseURL: baseurl,
Username: username,
Password: password,
}
}
func (s *Client) AddUserOrg(UserOrgInst userOrg) error {
url := fmt.Sprintf(s.BaseURL+"/accounts/", s.Username)
fmt.Println(url)
j, err := json.Marshal(UserOrgInst)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(j))
if err != nil {
return err
}
_, err = s.doRequest(req)
return err
}
func (s *Client) doRequest(req *http.Request) ([]byte, error) {
req.SetBasicAuth(s.Username, s.Password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if 200 != resp.StatusCode {
return nil, fmt.Errorf("ERROR %s", body)
}
return body, nil
}
func (s *Client) GetUserOrg(id int, whom string) (*userOrg, error) {
url := fmt.Sprintf(s.BaseURL+"/accounts/"+whom, s.Username, id)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
bytes, err := s.doRequest(req)
if err != nil {
return nil, err
}
var data userOrg
err = json.Unmarshal(bytes, &data)
if err != nil {
return nil, err
}
return &data, nil
}