forked from frain-dev/convoy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
167 lines (136 loc) · 3.8 KB
/
source.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package convoy_go
import (
"context"
"errors"
"fmt"
"time"
)
var (
ErrNotListSourceResponse = errors.New("invalid list source response")
ErrNotSourceResponse = errors.New("invalid source response")
)
type Source struct {
client *Client
}
type CreateSourceRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Provider string `json:"provider"`
IsDisabled bool `json:"is_disabled"`
Verifier VerifierConfig `json:"verifier"`
}
type SourceResponse struct {
UID string `json:"uid"`
GroupID string `json:"group_id"`
MaskID string `json:"mask_id"`
Name string `json:"name"`
Type string `json:"type"`
Provider string `json:"provider"`
IsDisabled bool `json:"is_disabled"`
Verifier *VerifierConfig `json:"verifier"`
ProviderConfig *ProviderConfig `json:"provider_config"`
ForwardHeaders []string `json:"forward_headers"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
type ListSourceResponse struct {
Content []SourceResponse `json:"content"`
Pagination Pagination `json:"pagination"`
}
type SourceParams struct {
ListParams
Type string `url:"type"`
}
type ProviderConfig struct {
Twitter *TwitterProviderConfig `json:"twitter" bson:"twitter"`
}
type TwitterProviderConfig struct {
CrcVerifiedAt time.Time `json:"crc_verified_at"`
}
type VerifierConfig struct {
Type string `json:"type,omitempty"`
HMac *HMac `json:"hmac"`
BasicAuth *BasicAuth `json:"basic_auth"`
ApiKey *ApiKey `json:"api_key"`
}
type HMac struct {
Header string `json:"header"`
Hash string `json:"hash"`
Secret string `json:"secret"`
Encoding string `json:"encoding"`
}
type BasicAuth struct {
UserName string `json:"username"`
Password string `json:"password"`
}
type ApiKey struct {
HeaderValue string `json:"header_value"`
HeaderName string `json:"header_name"`
}
func newSource(client *Client) *Source {
return &Source{
client: client,
}
}
func (s *Source) All(ctx context.Context, query *SourceParams) (*ListSourceResponse, error) {
url, err := addOptions(s.generateUrl(), query)
if err != nil {
return nil, err
}
respPtr := &ListSourceResponse{}
err = getResource(ctx, s.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Source) Create(ctx context.Context, body *CreateSourceRequest) (*SourceResponse, error) {
url, err := addOptions(s.generateUrl(), nil)
if err != nil {
return nil, err
}
respPtr := &SourceResponse{}
err = postJSON(ctx, s.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Source) Find(ctx context.Context, sourceId string) (*SourceResponse, error) {
url, err := addOptions(s.generateUrl()+"/"+sourceId, nil)
if err != nil {
return nil, err
}
respPtr := &SourceResponse{}
err = getResource(ctx, s.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Source) Update(ctx context.Context, sourceId string, body *CreateSourceRequest) (*SourceResponse, error) {
url, err := addOptions(s.generateUrl()+"/"+sourceId, nil)
if err != nil {
return nil, err
}
respPtr := &SourceResponse{}
err = postJSON(ctx, s.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Source) Delete(ctx context.Context, sourceId string) error {
url, err := addOptions(s.generateUrl()+"/"+sourceId, nil)
if err != nil {
return err
}
err = deleteResource(ctx, s.client, url, nil)
if err != nil {
return err
}
return nil
}
func (s *Source) generateUrl() string {
return fmt.Sprintf("%s/projects/%s/sources", s.client.baseURL, s.client.projectID)
}