-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.go
102 lines (89 loc) · 2.84 KB
/
providers.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
package api
import (
"context"
"encoding/json"
"fmt"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"net/http"
)
type Providers struct {
Client *Client
}
func (s Providers) basePath() string {
return fmt.Sprintf("orgs/%s/providers", s.Client.Config.OrgName)
}
func (s Providers) providerPath(providerName string) string {
return fmt.Sprintf("orgs/%s/providers/%s", s.Client.Config.OrgName, providerName)
}
// List - GET /orgs/:orgName/providers
func (s Providers) List(ctx context.Context) ([]*types.Provider, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(), nil, nil, nil)
if err != nil {
return nil, err
}
var providers []*types.Provider
if err := response.ReadJson(res, &providers); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return providers, nil
}
// Get - GET /orgs/:orgName/providers/:name
func (s Providers) Get(ctx context.Context, providerName string) (*types.Provider, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.providerPath(providerName), nil, nil, nil)
if err != nil {
return nil, err
}
var provider types.Provider
if err := response.ReadJson(res, &provider); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &provider, nil
}
// Create - POST /orgs/:orgName/providers
func (s Providers) Create(ctx context.Context, provider *types.Provider) (*types.Provider, error) {
rawPayload, _ := json.Marshal(provider)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedProvider types.Provider
if err := response.ReadJson(res, &updatedProvider); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedProvider, nil
}
// Update - PUT/PATCH /orgs/:orgName/providers/:name
func (s Providers) Update(ctx context.Context, providerName string, provider *types.Provider) (*types.Provider, error) {
rawPayload, _ := json.Marshal(provider)
res, err := s.Client.Do(ctx, http.MethodPut, s.providerPath(providerName), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedProvider types.Provider
if err := response.ReadJson(res, &updatedProvider); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedProvider, nil
}
// Destroy - DELETE /orgs/:orgName/providers/:name
func (s Providers) Destroy(ctx context.Context, providerName string) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.providerPath(providerName), nil, nil, nil)
if err != nil {
return false, err
}
if err := response.Verify(res); response.IsNotFoundError(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}