forked from frain-dev/convoy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convoy.go
136 lines (114 loc) · 2.75 KB
/
convoy.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
package convoy_go
import (
"encoding/json"
"net/http"
"net/url"
"os"
"reflect"
"time"
"github.com/google/go-querystring/query"
)
type APIResponse struct {
Status bool `json:"status"`
Message string `json:"message"`
Data *json.RawMessage `json:"data,omitempty"`
}
// Pagination type used in responses.
type Pagination struct {
PerPage int `json:"per_page"`
HasNextPage bool `json:"has_next_page"`
HasPrevPage bool `json:"has_prev_page"`
PrevPageCursor string `json:"prev_page_cursor"`
NextPageCursor string `json:"next_page_cursor"`
}
// ListParams is used in requests for filtering lists
type ListParams struct {
PerPage int `url:"per_page"`
PrevPageCursor string `url:"prev_page_cursor"`
NextPageCursor string `url:"next_page_cursor"`
}
type Client struct {
client *http.Client
baseURL string
apiKey string
projectID string
log iLogger
kafkaOpts *KafkaOptions
sqsOpts *SQSOptions
Projects *Project
Endpoints *Endpoint
Events *Event
EventDeliveries *EventDelivery
DeliveryAttempts *DeliveryAttempt
Sources *Source
Subscriptions *Subscription
Kafka *Kafka
SQS *SQS
}
type Option func(*Client)
func OptionKafkaOptions(ko *KafkaOptions) func(c *Client) {
return func(c *Client) {
c.kafkaOpts = ko
}
}
func OptionSQSOptions(so *SQSOptions) func(c *Client) {
return func(c *Client) {
c.sqsOpts = so
}
}
func OptionLogger(logger iLogger) func(c *Client) {
return func(c *Client) {
c.log = logger
}
}
func OptionHTTPClient(client *http.Client) func(c *Client) {
return func(c *Client) {
c.client = client
}
}
func New(baseURL, apiKey, projectID string, options ...Option) *Client {
c := &Client{
client: &http.Client{
Timeout: 5 * time.Second,
},
log: NewLogger(os.Stdout, ErrorLevel),
apiKey: apiKey,
projectID: projectID,
baseURL: baseURL,
}
for _, opt := range options {
opt(c)
}
c.Projects = newProject(c)
c.Endpoints = newEndpoint(c)
c.Events = newEvent(c)
c.EventDeliveries = newEventDelivery(c)
c.DeliveryAttempts = newDeliveryAttempt(c)
c.Sources = newSource(c)
c.Subscriptions = newSubscription(c)
if c.kafkaOpts != nil {
c.Kafka = newKafka(c)
}
if c.sqsOpts != nil {
c.SQS = newSQS(c)
}
return c
}
// addOptions adds the parameters in opts as URL query parameters to s. opts
// must be a struct whose fields may contain "url" tags.
func addOptions(s string, opts interface{}) (string, error) {
v := reflect.ValueOf(opts)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qs, err := query.Values(opts)
if err != nil {
return s, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}