forked from kevholditch/gokong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificates.go
115 lines (87 loc) · 3.23 KB
/
certificates.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
package gokong
import (
"encoding/json"
"fmt"
"github.com/parnurzeal/gorequest"
)
type CertificateClient struct {
config *Config
}
type CertificateRequest struct {
Cert string `json:"cert,omitempty"`
Key string `json:"key,omitempty"`
}
type Certificate struct {
Id string `json:"id,omitempty"`
Cert string `json:"cert,omitempty"`
Key string `json:"key,omitempty"`
}
type Certificates struct {
Results []*Certificate `json:"data,omitempty"`
Total int `json:"total,omitempty"`
}
const CertificatesPath = "/certificates/"
func (certificateClient *CertificateClient) GetById(id string) (*Certificate, error) {
_, body, errs := gorequest.New().Get(certificateClient.config.HostAddress + CertificatesPath + id).End()
if errs != nil {
return nil, fmt.Errorf("could not get certificate, error: %v", errs)
}
certificate := &Certificate{}
err := json.Unmarshal([]byte(body), certificate)
if err != nil {
return nil, fmt.Errorf("could not parse certificate get response, error: %v", err)
}
if certificate.Id == "" {
return nil, nil
}
return certificate, nil
}
func (certificateClient *CertificateClient) Create(certificateRequest *CertificateRequest) (*Certificate, error) {
_, body, errs := gorequest.New().Post(certificateClient.config.HostAddress + CertificatesPath).Send(certificateRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not create new certificate, error: %v", errs)
}
createdCertificate := &Certificate{}
err := json.Unmarshal([]byte(body), createdCertificate)
if err != nil {
return nil, fmt.Errorf("could not parse certificate creation response, error: %v", err)
}
if createdCertificate.Id == "" {
return nil, fmt.Errorf("could not create certificate, error: %v", body)
}
return createdCertificate, nil
}
func (certificateClient *CertificateClient) DeleteById(id string) error {
res, _, errs := gorequest.New().Delete(certificateClient.config.HostAddress + CertificatesPath + id).End()
if errs != nil {
return fmt.Errorf("could not delete certificate, result: %v error: %v", res, errs)
}
return nil
}
func (certificateClient *CertificateClient) List() (*Certificates, error) {
_, body, errs := gorequest.New().Get(certificateClient.config.HostAddress + CertificatesPath).End()
if errs != nil {
return nil, fmt.Errorf("could not get certificates, error: %v", errs)
}
certificates := &Certificates{}
err := json.Unmarshal([]byte(body), certificates)
if err != nil {
return nil, fmt.Errorf("could not parse certificates list response, error: %v", err)
}
return certificates, nil
}
func (certificateClient *CertificateClient) UpdateById(id string, certificateRequest *CertificateRequest) (*Certificate, error) {
_, body, errs := gorequest.New().Patch(certificateClient.config.HostAddress + CertificatesPath + id).Send(certificateRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not update certificate, error: %v", errs)
}
updatedCertificate := &Certificate{}
err := json.Unmarshal([]byte(body), updatedCertificate)
if err != nil {
return nil, fmt.Errorf("could not parse certificate update response, error: %v", err)
}
if updatedCertificate.Id == "" {
return nil, fmt.Errorf("could not update certificate, error: %v", body)
}
return updatedCertificate, nil
}