This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
webclient.go
206 lines (181 loc) · 4.81 KB
/
webclient.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cmd
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"syscall"
"unsafe"
)
// WebClient is a basic web client to be used with various utility functionality.
type WebClient struct {
Client *http.Client
Address string
Username string
Password string
token string
}
// GetInformation will ask for more information if not already known.
func (wc *WebClient) GetInformation() {
emptyline := false
if wc.Address == "" {
emptyline = true
wc.Address = readline("API address: ", false)
}
if wc.Username == "" {
emptyline = true
wc.Username = readline("Username: ", false)
}
if wc.Password == "" {
emptyline = true
wc.Password = readline("Password: ", true)
}
if emptyline {
fmt.Printf("\n\n")
}
}
func (wc *WebClient) preflight() {
if wc.token == "" {
wc.GetInformation()
wc.Authenticate()
}
}
// Authenticate against the RESTful service with basic authentication.
func (wc *WebClient) Authenticate() {
data := json.RawMessage(`{ "username": "` + wc.Username + `", "password": "` + wc.Password + `" }`)
req, err := http.NewRequest("POST", wc.Address+"/auth/basic", bytes.NewBuffer(data))
if err != nil {
return
}
req.Header.Add("Content-Type", "application/json")
resp, err := wc.Client.Do(req)
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
wc.token = strings.Trim(strings.TrimSpace(string(body)), "\"")
}
// Save a JSON structure to the RESTful service.
func (wc *WebClient) Save(payload json.RawMessage) {
wc.preflight()
req, err := http.NewRequest("POST", wc.Address+"/me/save", bytes.NewBuffer(payload))
if err != nil {
log.Println("Error creating request for saving payload.", err)
return
}
req.Header.Add("Authorization", "Bearer "+wc.token)
req.Header.Add("Content-Type", "application/json")
resp, err := wc.Client.Do(req)
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
log.Println("Error or bad response while saving payload.", err)
return
}
defer resp.Body.Close()
}
// Form returns the entire application as a JSON structure.
func (wc *WebClient) Form() json.RawMessage {
wc.preflight()
req, err := http.NewRequest("GET", wc.Address+"/me/form", nil)
if err != nil {
log.Println("Error creating request for submitting application.", err)
return []byte{}
}
req.Header.Add("Authorization", "Bearer "+wc.token)
resp, err := wc.Client.Do(req)
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
log.Println("Error or bad response while submitting application.", err)
return []byte{}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}
}
return body
}
// Submit will submit an application through the RESTful service.
func (wc *WebClient) Submit() {
wc.preflight()
req, err := http.NewRequest("GET", wc.Address+"/me/form/submit", nil)
if err != nil {
log.Println("Error creating request for submitting application.", err)
return
}
req.Header.Add("Authorization", "Bearer "+wc.token)
resp, err := wc.Client.Do(req)
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
log.Println("Error or bad response while submitting application.", err)
return
}
defer resp.Body.Close()
}
// Logout will end a session.
func (wc *WebClient) Logout() {
wc.preflight()
req, err := http.NewRequest("GET", wc.Address+"/me/logout", nil)
if err != nil {
log.Println("Error creating request for logging out.", err)
return
}
req.Header.Add("Authorization", "Bearer "+wc.token)
resp, err := wc.Client.Do(req)
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
log.Println("Error or bad response while logging out.", err)
return
}
defer resp.Body.Close()
}
// IsPiped returns whether the command was started with piped input.
func IsPiped() bool {
stat, _ := os.Stdin.Stat()
mode := stat.Mode()
return mode&os.ModeNamedPipe != 0
}
func readline(prefix string, secret bool) string {
var input string
var reader *bufio.Reader
if IsPiped() {
fi, err := os.Open("/dev/tty")
if err != nil {
return ""
}
defer fi.Close()
reader = bufio.NewReader(fi)
} else {
reader = bufio.NewReader(os.Stdin)
}
fmt.Print(prefix)
if secret {
echo(false)
}
input, _ = reader.ReadString('\n')
input = strings.TrimSpace(input)
if secret {
echo(true)
}
return input
}
func echo(visible bool) {
var termios = &syscall.Termios{}
var fd = os.Stdout.Fd()
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.TCGETS, uintptr(unsafe.Pointer(termios))); err != 0 {
return
}
if visible {
termios.Lflag |= syscall.ECHO
} else {
termios.Lflag &^= syscall.ECHO
}
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.TCSETS, uintptr(unsafe.Pointer(termios))); err != 0 {
return
}
}