-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
93 lines (66 loc) · 1.45 KB
/
client.go
File metadata and controls
93 lines (66 loc) · 1.45 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
package camunda
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"github.com/dimchansky/utfbom"
)
type Client struct {
once sync.Once
cli http.Client
}
func (c *Client) init() {
}
func (c *Client) authorize(ctx context.Context) (*Token, error) {
return new(Token), nil
}
func (c *Client) send(ctx context.Context, url, method, ct string, payload io.Reader, out interface{}) error {
c.once.Do(c.init)
var content []byte
var err error
var req *http.Request
var resp *http.Response
req, err = http.NewRequestWithContext(ctx, method, url, payload)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", ct)
resp, err = c.cli.Do(req)
if err != nil {
return err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
goto ok
}
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()
content, err = ioutil.ReadAll(utfbom.SkipOnly(resp.Body))
if err != nil {
return err
}
err = new(Error)
if e := json.Unmarshal(content, err); e != nil {
goto unknown
}
if v, ok := err.(*Error); ok {
return v
}
unknown:
return fmt.Errorf("send failed, status code: %d", resp.StatusCode)
ok:
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()
if resp.StatusCode == 204 {
return nil
}
content, err = ioutil.ReadAll(utfbom.SkipOnly(resp.Body))
if err != nil {
return err
}
return json.Unmarshal(content, out)
}