-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
92 lines (75 loc) · 2.45 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
package test
import (
"net/http"
"github.com/alexfalkowski/go-service/client"
"github.com/alexfalkowski/go-service/crypto/tls"
"github.com/alexfalkowski/go-service/runtime"
"github.com/alexfalkowski/go-service/security/token"
"github.com/alexfalkowski/go-service/telemetry/tracer"
"github.com/alexfalkowski/go-service/transport"
g "github.com/alexfalkowski/go-service/transport/grpc"
h "github.com/alexfalkowski/go-service/transport/http"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
)
// Client for test.
type Client struct {
Lifecycle fx.Lifecycle
Logger *zap.Logger
Tracer *tracer.Config
Transport *transport.Config
TLS *tls.Config
RoundTripper http.RoundTripper
Meter metric.Meter
Generator token.Generator
Compression bool
H2C bool
}
// NewHTTP client for test.
func (c *Client) NewHTTP() *http.Client {
sec, err := h.WithClientTLS(c.TLS)
runtime.Must(err)
tracer, err := tracer.NewTracer(c.Lifecycle, Environment, Version, Name, c.Tracer, c.Logger)
runtime.Must(err)
opts := []h.ClientOption{
h.WithClientLogger(c.Logger),
h.WithClientRoundTripper(c.RoundTripper), h.WithClientBreaker(),
h.WithClientTracer(tracer), h.WithClientRetry(c.Transport.HTTP.Retry),
h.WithClientMetrics(c.Meter), h.WithClientUserAgent(UserAgent),
h.WithClientTokenGenerator(c.Generator), h.WithClientTimeout("1m"), sec,
}
if c.Compression {
opts = append(opts, h.WithClientCompression())
}
if c.H2C {
opts = append(opts, h.WithClientH2C())
}
client := h.NewClient(opts...)
return client
}
func (c *Client) NewGRPC() *grpc.ClientConn {
tracer, err := tracer.NewTracer(c.Lifecycle, Environment, Version, Name, c.Tracer, c.Logger)
runtime.Must(err)
sec, err := g.WithClientTLS(c.TLS)
runtime.Must(err)
cl := &client.Config{
Host: "localhost:" + c.Transport.GRPC.Port,
Retry: c.Transport.GRPC.Retry,
}
opts := []g.ClientOption{
g.WithClientUnaryInterceptors(), g.WithClientStreamInterceptors(),
g.WithClientLogger(c.Logger), g.WithClientTracer(tracer),
g.WithClientBreaker(), g.WithClientRetry(cl.Retry),
g.WithClientMetrics(c.Meter), g.WithClientUserAgent(UserAgent),
g.WithClientTimeout("5s"), g.WithClientTokenGenerator(c.Generator),
g.WithClientTimeout("1m"), g.WithClientDialOption(), sec,
}
if c.Compression {
opts = append(opts, g.WithClientCompression())
}
conn, err := g.NewClient(cl.Host, opts...)
runtime.Must(err)
return conn
}