Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
127 lines (107 loc) · 3.04 KB
/
Copy pathclient.go
File metadata and controls
127 lines (107 loc) · 3.04 KB
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
package nxrm
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/hashicorp/go-retryablehttp"
)
type ClientConfig struct {
Username, Password, BaseURL string
}
func NewClient(config *ClientConfig) (*Client, error) {
client := retryablehttp.NewClient()
return &Client{
username: config.Username,
password: config.Password,
baseURL: config.BaseURL,
client: client,
}, nil
}
type Client struct {
username, password, baseURL string
client *retryablehttp.Client
}
type User struct {
UserId string `json:"userId"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
EmailAddress string `json:"emailAddress"`
Status string `json:"status"`
Password string `json:"password"` // Passwords must be at least 6 characters long.
Roles []string `json:"roles"`
}
func (c *Client) GetUser(ctx context.Context, name string) (map[string]interface{}, error) {
endpoint := "/service/rest/beta/security/users"
method := http.MethodGet
req, err := http.NewRequest(method, c.baseURL+endpoint, nil)
if err != nil {
return nil, err
}
var users []map[string]interface{}
if err := c.do(ctx, req, &users); err != nil {
return nil, err
}
return users[0], nil
}
func (c *Client) CreateUser(ctx context.Context, name string, user *User) error {
endpoint := "/service/rest/beta/security/users"
method := http.MethodPost
userJson, err := json.Marshal(user)
if err != nil {
return err
}
req, err := http.NewRequest(method, c.baseURL+endpoint, bytes.NewReader(userJson))
if err != nil {
return err
}
return c.do(ctx, req, nil)
}
func (c *Client) ChangePassword(ctx context.Context, name, newPassword string) error {
endpoint := "/service/rest/beta/security/users/" + name + "/change-password"
method := http.MethodPut
req, err := http.NewRequest(method, c.baseURL+endpoint, strings.NewReader(newPassword))
if err != nil {
return err
}
return c.do(ctx, req, nil)
}
func (c *Client) DeleteUser(ctx context.Context, name string) error {
endpoint := "/service/rest/beta/security/users/" + name
method := http.MethodDelete
req, err := http.NewRequest(method, c.baseURL+endpoint, nil)
if err != nil {
return err
}
return c.do(ctx, req, nil)
}
func (c *Client) do(ctx context.Context, req *http.Request, ret interface{}) error {
retryableReq, err := retryablehttp.NewRequest(req.Method, req.URL.String(), req.Body)
if err != nil {
return err
}
retryableReq.SetBasicAuth(c.username, c.password)
retryableReq.Header.Add("Content-Type", "application/json")
resp, err := c.client.Do(retryableReq.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if ret == nil {
return nil
}
if err := json.Unmarshal(body, ret); err != nil {
return fmt.Errorf("%s; %d: %s", err, resp.StatusCode, body)
}
return nil
}
if resp.StatusCode == 404 {
return nil
}
return fmt.Errorf("%d: %s", resp.StatusCode, body)
}