-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
123 lines (103 loc) · 2.36 KB
/
client.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
package go_certcentral
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
)
const (
baseURL = "https://www.digicert.com/services/v2"
contentTypeJson = "application/json"
)
// Client is the client for the DigiCert cert-central API.
type Client struct {
*Options
httpClient *http.Client
}
func New(opts *Options) (*Client, error) {
if err := opts.validate(); err != nil {
return nil, err
}
tr := &http.Transport{
DisableCompression: false,
DisableKeepAlives: false,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
InsecureSkipVerify: false,
},
Proxy: http.ProxyFromEnvironment,
}
return &Client{
Options: opts,
httpClient: &http.Client{Transport: tr},
}, nil
}
func (c *Client) do(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", "sapcc/go-certcentral")
req.Header.Set("X-DC-DEVKEY", c.Token)
req.Header.Set("Accept", contentTypeJson)
req.Header.Set("Content-Type", contentTypeJson)
if c.IsDebug {
if reqDump, err := httputil.DumpRequest(req, true); err != nil {
fmt.Println("err: ", err)
} else {
fmt.Println("sending request: ", string(reqDump))
}
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if c.IsDebug {
if resDump, err := httputil.DumpResponse(res, true); err != nil {
fmt.Println("err: ", err)
} else {
fmt.Println("got response: ", string(resDump))
}
}
err = wrapErrorIfAny(res)
return res, err
}
func wrapErrorIfAny(res *http.Response) error {
if res.StatusCode >= 400 {
if res.Header.Get("Content-Type") == contentTypeJson {
return parseJsonError(res)
}
return &Error{
Code: res.StatusCode,
Status: res.Status,
Message: "unknown error",
}
}
return nil
}
func parseJsonError(res *http.Response) error {
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
defer res.Body.Close()
type errs struct {
Errors []Error `json:"errors"`
}
var e errs
if err := json.Unmarshal(resBody, &e); err != nil {
return err
}
resErr := &Error{
Code: res.StatusCode,
Status: res.Status,
Message: "unkown",
}
if len(e.Errors) > 0 {
resErr.Status = e.Errors[0].Status
resErr.Message = e.Errors[0].Message
}
return resErr
}