Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
232 lines (201 loc) · 8.08 KB
/
Copy pathapi.go
File metadata and controls
232 lines (201 loc) · 8.08 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package api
import (
"context"
"fmt"
"github.com/andygello555/gotils/v2/slices"
"github.com/machinebox/graphql"
"net/http"
"reflect"
"sync"
"time"
)
// HTTPRequest is a wrapper for http.Request that implements the Request interface.
type HTTPRequest struct {
*http.Request
}
func (req HTTPRequest) Header() *http.Header {
return &req.Request.Header
}
// GraphQLRequest is a wrapper for graphql.Request that implements the Request interface.
type GraphQLRequest struct {
*graphql.Request
}
func (req GraphQLRequest) Header() *http.Header {
return &req.Request.Header
}
// Request is the request instance that is constructed by the Binding.Request method.
type Request interface {
// Header returns a reference to the http.Header for the underlying http.Request that can be modified in any way
// necessary by Binding.Execute.
Header() *http.Header
}
// Client is the API client that will execute the Binding.
type Client interface {
// Run should execute the given Request and unmarshal the response into the given response interface. It is usually
// called from Binding.Execute to execute a Binding, hence why we also pass in the name of the Binding (from
// Binding.Name).
Run(ctx context.Context, bindingName string, attrs map[string]any, req Request, res any) error
}
type RateLimitType int
const (
// RequestRateLimit means that the RateLimit is limited by the number of HTTP requests that can be made in a certain
// timespan.
RequestRateLimit RateLimitType = iota
// ResourceRateLimit means that the RateLimit is limited by the number of resources that can be fetched in a certain
// timespan.
ResourceRateLimit
)
// RateLimit represents a RateLimit for a binding.
type RateLimit interface {
// Reset returns the time at which the RateLimit resets.
Reset() time.Time
// Remaining returns the number of requests remaining/resources that can be fetched for this RateLimit.
Remaining() int
// Used returns the number of requests used/resources fetched so far for this RateLimit.
Used() int
// Type is the type of the RateLimit. See RateLimitType for documentation.
Type() RateLimitType
}
// RateLimitedClient is an API Client that has a RateLimit for each Binding it has authority over.
type RateLimitedClient interface {
// Client should implement a Client.Run method that sets an internal sync.Map of RateLimit(s).
Client
// RateLimits returns the sync.Map of Binding names to RateLimit instances.
RateLimits() *sync.Map
// AddRateLimit should add a RateLimit to the internal sync.Map within the Client. It should check if the Binding of
// the given name already has a RateLimit, and whether the RateLimit.Reset lies after the currently set RateLimit
// for that Binding.
AddRateLimit(bindingName string, rateLimit RateLimit)
// LatestRateLimit should return the latest RateLimit for the Binding of the given name. If multiple Binding(s)
// share the same RateLimit(s) then this can also be encoded into this method.
LatestRateLimit(bindingName string) RateLimit
// Log should be implemented for debugging purposes. If you do not require it then you can define it but have it do
// nothing.
Log(string)
}
// BindingWrapper wraps a Binding value with its name. This is used within the Schema map so that we don't have to use
// type parameters everywhere.
type BindingWrapper struct {
name string
responseType reflect.Type
returnType reflect.Type
binding reflect.Value
}
func (bw BindingWrapper) String() string {
return fmt.Sprintf("%s/%v", bw.name, bw.binding.Type())
}
// Name returns the name of the underlying Binding.
func (bw BindingWrapper) Name() string { return bw.name }
func (bw BindingWrapper) bindingName() string {
return bw.binding.MethodByName("Name").Call([]reflect.Value{})[0].Interface().(string)
}
// Paginated calls the Binding.Paginated method for the underlying Binding in the BindingWrapper.
func (bw BindingWrapper) Paginated() bool {
return bw.binding.MethodByName("Paginated").Call([]reflect.Value{})[0].Bool()
}
// Paginator returns an un-typed Paginator for the underlying Binding of the BindingWrapper.
func (bw BindingWrapper) Paginator(client Client, waitTime time.Duration, args ...any) (paginator Paginator[any, any], err error) {
return NewPaginator(client, waitTime, bw, args...)
}
// ArgsFromStrings calls the Binding.ArgsFromStrings method for the underlying Binding in the BindingWrapper.
func (bw BindingWrapper) ArgsFromStrings(args ...string) (parsedArgs []any, err error) {
values := bw.binding.MethodByName("ArgsFromStrings").Call(slices.Comprehension(args, func(idx int, value string, arr []string) reflect.Value {
return reflect.ValueOf(value)
}))
parsedArgs = values[0].Interface().([]any)
err = nil
if !values[1].IsNil() {
err = values[1].Interface().(error)
}
return
}
// Params calls the Binding.Params method for the underlying Binding in the BindingWrapper.
func (bw BindingWrapper) Params() []BindingParam {
return bw.binding.MethodByName("Params").Call([]reflect.Value{})[0].Interface().([]BindingParam)
}
// Execute calls the Binding.Execute method for the underlying Binding in the BindingWrapper.
func (bw BindingWrapper) Execute(client Client, args ...any) (val any, err error) {
arguments := []any{client}
arguments = append(arguments, args...)
values := bw.binding.MethodByName("Execute").Call(slices.Comprehension(arguments, func(idx int, value any, arr []any) reflect.Value {
return reflect.ValueOf(value)
}))
val = values[0].Interface()
err = nil
if !values[1].IsNil() {
err = values[1].Interface().(error)
}
return
}
func (bw BindingWrapper) setName(name string) {
fmt.Println("setName", bw.binding.Type())
bw.binding.MethodByName("SetName").Call([]reflect.Value{reflect.ValueOf(name)})
}
// WrapBinding will return the BindingWrapper for the given Binding. The name of the BindingWrapper will be fetched from
// Binding.Name, so make sure to override this before using the Binding.
func WrapBinding[ResT any, RetT any](binding Binding[ResT, RetT]) BindingWrapper {
var (
resT ResT
retT RetT
)
return BindingWrapper{
name: binding.Name(),
responseType: reflect.TypeOf(resT),
returnType: reflect.TypeOf(retT),
binding: reflect.ValueOf(&binding).Elem(),
}
}
// Schema is a mapping of names to BindingWrapper(s).
type Schema map[string]BindingWrapper
// API represents a connection to an API with multiple different available Binding(s).
type API struct {
Client Client
schema Schema
}
// NewAPI constructs a new API instance for the given Client and Schema combination.
func NewAPI(client Client, schema Schema) *API {
for bindingName, bindingWrapper := range schema {
bindingWrapper.name = bindingName
}
return &API{
Client: client,
schema: schema,
}
}
// Binding returns the BindingWrapper with the given name in the Schema for this API. The second return value is an "ok"
// flag.
func (api *API) Binding(name string) (BindingWrapper, bool) {
binding, ok := api.schema[name]
return binding, ok
}
func (api *API) checkBindingExists(name string) (binding BindingWrapper, err error) {
var ok bool
if binding, ok = api.Binding(name); !ok {
err = fmt.Errorf("could not find Binding for action %q", name)
}
return
}
// ArgsFromStrings will execute the Binding.ArgsFromStrings method for the Binding of the given name within the API.
func (api *API) ArgsFromStrings(name string, args ...string) (parsedArgs []any, err error) {
var binding BindingWrapper
if binding, err = api.checkBindingExists(name); err != nil {
return
}
return binding.ArgsFromStrings(args...)
}
// Execute will execute the Binding of the given name within the API.
func (api *API) Execute(name string, args ...any) (val any, err error) {
var binding BindingWrapper
if binding, err = api.checkBindingExists(name); err != nil {
return
}
return binding.Execute(api.Client, args...)
}
// Paginator returns a Paginator for the Binding of the given name within the API.
func (api *API) Paginator(name string, waitTime time.Duration, args ...any) (paginator Paginator[any, any], err error) {
var binding BindingWrapper
if binding, err = api.checkBindingExists(name); err != nil {
return
}
return NewPaginator(api.Client, waitTime, binding, args...)
}