forked from agntcy/dir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
70 lines (57 loc) · 1.53 KB
/
Copy pathclient.go
File metadata and controls
70 lines (57 loc) · 1.53 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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package client
import (
"fmt"
routingv1 "github.com/agntcy/dir/api/routing/v1"
searchv1 "github.com/agntcy/dir/api/search/v1"
storev1 "github.com/agntcy/dir/api/store/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type Client struct {
storev1.StoreServiceClient
routingv1.RoutingServiceClient
searchv1.SearchServiceClient
storev1.SyncServiceClient
}
type options struct {
config *Config
}
type Option func(*options) error
func WithEnvConfig() Option {
return func(opts *options) error {
var err error
opts.config, err = LoadConfig()
return err
}
}
func WithConfig(config *Config) Option {
return func(opts *options) error {
opts.config = config
return nil
}
}
func New(opts ...Option) (*Client, error) {
// Load options
options := &options{}
for _, opt := range opts {
if err := opt(options); err != nil {
return nil, fmt.Errorf("failed to load options: %w", err)
}
}
// Create client
client, err := grpc.NewClient(
options.config.ServerAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, fmt.Errorf("failed to create gRPC client: %w", err)
}
return &Client{
StoreServiceClient: storev1.NewStoreServiceClient(client),
RoutingServiceClient: routingv1.NewRoutingServiceClient(client),
SearchServiceClient: searchv1.NewSearchServiceClient(client),
SyncServiceClient: storev1.NewSyncServiceClient(client),
}, nil
}