-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasura.go
125 lines (100 loc) · 2.67 KB
/
hasura.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package hasura_api
import (
"time"
"github.com/go-resty/resty/v2"
"github.com/gookit/config/v2"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type HasuraClient struct {
Metadata *MetadataClient
client *resty.Client
Config *HasuraConfig
Debug bool
adminSecret string
}
type MetadataClient struct {
HasuraClient
}
type HasuraClientOptions struct {
configFilepaths []string
envFilepaths []string
debug bool
timeout time.Duration
literals struct {
endpoint string
adminSecret string
}
}
type HasuraClientOption func(*HasuraClientOptions)
func WithConfigFilepath(filepath ...string) HasuraClientOption {
return func(options *HasuraClientOptions) {
options.configFilepaths = filepath
}
}
func WithEnvFilepath(filepath ...string) HasuraClientOption {
return func(options *HasuraClientOptions) {
options.envFilepaths = filepath
}
}
func WithLiterals(hasuraEndpoint, hasuraAdminSecret string) HasuraClientOption {
return func(options *HasuraClientOptions) {
options.literals.endpoint = hasuraEndpoint
options.literals.adminSecret = hasuraAdminSecret
}
}
func WithDebug(debug bool) HasuraClientOption {
return func(options *HasuraClientOptions) {
options.debug = debug
}
}
func WithTimeout(timeout time.Duration) HasuraClientOption {
return func(options *HasuraClientOptions) {
options.timeout = timeout
}
}
func newHasuraClientWithLiterals(hasuraEndpoint, hasuraAdminSecret string) (*HasuraClient, error) {
client := &HasuraClient{
client: resty.New(),
adminSecret: hasuraAdminSecret,
Config: &HasuraConfig{
Endpoint: hasuraEndpoint,
},
}
client.Metadata = &MetadataClient{
HasuraClient: *client,
}
return client, nil
}
func NewHasuraClient(options ...HasuraClientOption) (*HasuraClient, error) {
opts := new(HasuraClientOptions)
for _, opt := range options {
opt(opts)
}
if opts.literals.endpoint != "" && opts.literals.adminSecret != "" {
return newHasuraClientWithLiterals(opts.literals.endpoint, opts.literals.adminSecret)
}
if err := godotenv.Load(opts.envFilepaths...); err != nil {
logrus.Warn("Error loading .env file", err)
}
conf, err := ConfigFromFile(opts.configFilepaths...)
if err != nil {
return nil, errors.WithStack(err)
}
adminSecret := config.Getenv("HASURA_GRAPHQL_ADMIN_SECRET", "")
client := &HasuraClient{
client: resty.New(),
Debug: opts.debug,
adminSecret: adminSecret,
Config: conf,
}
client.client.SetTimeout(10 * time.Minute)
if opts.timeout != time.Duration(0) {
client.client.SetTimeout(opts.timeout)
}
client.Metadata = &MetadataClient{
HasuraClient: *client,
}
return client, nil
}