-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.go
50 lines (43 loc) · 897 Bytes
/
common.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
package qbt_apiv2
import (
"fmt"
"io"
"net/http"
)
const (
ResponseBodyOK = "Ok."
ResponseBodyFAIL = "Fails."
)
// Optional parameters when sending HTTP requests
type Optional map[string]any
func (opt Optional) StringField() map[string]string {
m := make(map[string]string)
for k, v := range opt {
m[k] = fmt.Sprintf("%v", v)
}
return m
}
func RespOk(resp *http.Response, err error) error {
if err != nil {
return err
} else if resp.Status != "200 OK" { // check for correct status code
return fmt.Errorf("%w: %s", ErrBadResponse, resp.Status)
} else {
return nil
}
}
func RespBodyOk(body io.ReadCloser, bizErr error) error {
defer body.Close()
b, err := io.ReadAll(body)
if err != nil {
return err
}
if string(b) != ResponseBodyOK {
return bizErr
}
return nil
}
func ignrBody(body io.ReadCloser) error {
_, err := io.Copy(io.Discard, body)
return err
}