-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcw.go
111 lines (92 loc) · 2.39 KB
/
gcw.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
package gcw
import (
"context"
"errors"
"google.golang.org/api/iterator"
"google.golang.org/protobuf/proto"
)
type ApiResponseHandler interface {
HandleResponse(proto.Message)
}
type ClientInterface interface {
Close() error
}
type ClientWrapper[T ClientInterface] struct {
new func() (T, error)
Ctx context.Context
}
func (c *ClientWrapper[T]) New() (T, error) {
return c.new()
}
func NewClientWrapper[T ClientInterface](ctx context.Context, new func() (T, error)) ClientWrapper[T] {
return ClientWrapper[T]{
new: new,
Ctx: ctx,
}
}
type IteratorInterface[T proto.Message] interface {
PageInfo() *iterator.PageInfo
Next() (T, error)
}
type RequestFunc[T ClientInterface, V proto.Message, R proto.Message] func(T, R, context.Context) (V, error)
type RequestWrapper[T ClientInterface, V proto.Message, R proto.Message] struct {
Req R
requestFunc RequestFunc[T, V, R]
}
func NewRequest[T ClientInterface, V proto.Message, R proto.Message](req R, fn RequestFunc[T, V, R]) *RequestWrapper[T, V, R] {
return &RequestWrapper[T, V, R]{
Req: req,
requestFunc: fn,
}
}
func (rw *RequestWrapper[T, V, R]) MakeRequest(c ClientWrapper[T], h ApiResponseHandler) error {
client, err := c.New()
if err != nil {
return err
}
defer func(client T) {
err := client.Close()
if err != nil {
}
}(client)
r, err := rw.requestFunc(client, rw.Req, c.Ctx)
if err != nil {
return err
}
h.HandleResponse(r)
return nil
}
type CreateIteratorFunc[T ClientInterface, V proto.Message, R proto.Message] func(T, R, context.Context) IteratorInterface[V]
type IteratorRequestWrapper[T ClientInterface, V proto.Message, R proto.Message] struct {
Req R
createIterator CreateIteratorFunc[T, V, R]
}
func NewIteratorRequest[T ClientInterface, V proto.Message, R proto.Message](req R, fn CreateIteratorFunc[T, V, R]) *IteratorRequestWrapper[T, V, R] {
return &IteratorRequestWrapper[T, V, R]{
Req: req,
createIterator: fn,
}
}
func (irw *IteratorRequestWrapper[T, V, R]) MakeRequest(c ClientWrapper[T], h ApiResponseHandler) error {
client, err := c.New()
if err != nil {
return err
}
defer func(client T) {
err := client.Close()
if err != nil {
}
}(client)
it := irw.createIterator(client, irw.Req, c.Ctx)
for {
resp, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return err
}
h.HandleResponse(resp)
}
return nil
}