-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
105 lines (86 loc) · 2.96 KB
/
client.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
package bqfake
// TODO: Implement context expiration checking.
import (
"context"
"io"
"net/http"
"strings"
"sync/atomic"
"cloud.google.com/go/bigquery"
"github.com/googleapis/google-cloud-go-testing/bigquery/bqiface"
"google.golang.org/api/option"
)
// *******************************************************************
// DryRunClient, that just returns status ok and empty body
// *******************************************************************
// CountingTransport counts calls, and returns OK and empty body.
// `count` field should only be accessed using atomic.Foobar
type CountingTransport struct {
count int32
reqs []*http.Request
}
// Count returns the client call count.
func (ct *CountingTransport) Count() int32 {
return atomic.LoadInt32(&ct.count)
}
// Requests returns the entire req from the last request
func (ct *CountingTransport) Requests() []*http.Request {
return ct.reqs
}
// RoundTrip implements the RoundTripper interface, logging the
// request, and the response body, (which may be json).
func (ct *CountingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&ct.count, 1)
// Create an empty response with StatusOK
resp := &http.Response{}
resp.StatusCode = http.StatusOK
resp.Body = &nopCloser{strings.NewReader("")}
// Save the request for testing.
ct.reqs = append(ct.reqs, req)
return resp, nil
}
type nopCloser struct {
io.Reader
}
func (nc *nopCloser) Close() error { return nil }
// DryRunClient returns a client that just counts calls.
func DryRunClient() (*http.Client, *CountingTransport) {
client := &http.Client{}
tp := &CountingTransport{}
client.Transport = tp
return client, tp
}
// Client implements a fake client.
type Client[Row any] struct {
bqiface.Client
ctx context.Context // Just for checking expiration/cancelation
config ClientConfig[Row]
}
// NewClient creates a new Client implementing bqiface.Client, with a dry run HTTPClient.
func NewClient(ctx context.Context, project string, opts ...option.ClientOption) (*Client[map[string]bigquery.Value], error) {
dryRun, _ := DryRunClient()
opts = append(opts, option.WithHTTPClient(dryRun))
c, err := bigquery.NewClient(ctx, project, opts...)
if err != nil {
return nil, err
}
return &Client[map[string]bigquery.Value]{Client: bqiface.AdaptClient(c), ctx: ctx}, nil
}
// Dataset creates a Dataset.
// TODO - understand how bqiface adapters/structs work, and make this return a Dataset
// that satisfies bqiface.Dataset interface?
func (client Client[Row]) Dataset(ds string) bqiface.Dataset {
return Dataset{Dataset: client.Client.Dataset(ds), tables: make(map[string]*Table)}
}
func (client Client[Row]) Query(string) bqiface.Query {
return Query[Row]{
config: client.config.QueryConfig,
}
}
func NewQueryReadClient[Row any](qc QueryConfig[Row]) *Client[Row] {
// NOTE: if all needed functions are implemented by the fake, then a real
// client is unnecessary.
return &Client[Row]{
config: ClientConfig[Row]{QueryConfig: qc},
}
}