-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
204 lines (176 loc) · 4.45 KB
/
api.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package hasura
import (
"bytes"
"context"
"net/http"
"net/url"
"path"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// API -
type API struct {
baseURL string
secret string
client *http.Client
}
// New -
func New(baseURL, secret string) *API {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100
return &API{baseURL, secret, &http.Client{
Timeout: time.Minute,
Transport: t,
}}
}
func (api *API) buildURL(endpoint string, args map[string]string) (string, error) {
u, err := url.Parse(api.baseURL)
if err != nil {
return "", err
}
u.Path = path.Join(u.Path, endpoint)
values := u.Query()
for key, value := range args {
values.Add(key, value)
}
u.RawQuery = values.Encode()
return u.String(), nil
}
func (api *API) get(ctx context.Context, endpoint string, args map[string]string) (*http.Response, error) {
url, err := api.buildURL(endpoint, args)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return api.client.Do(req)
}
//nolint
func (api *API) post(ctx context.Context, endpoint string, args map[string]string, body interface{}, output interface{}) error {
url, err := api.buildURL(endpoint, args)
if err != nil {
return err
}
postBody, err := json.Marshal(body)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(postBody))
if err != nil {
return err
}
if api.secret != "" {
req.Header.Add("X-Hasura-Admin-Secret", api.secret)
}
resp, err := api.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var apiError APIError
if err := json.NewDecoder(resp.Body).Decode(&apiError); err != nil {
return errors.Wrap(err, "Hasura's response decoding error")
}
return apiError
}
if output == nil {
return nil
}
return json.NewDecoder(resp.Body).Decode(output)
}
// Health
func (api *API) Health(ctx context.Context) error {
resp, err := api.get(ctx, "/healthz", nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
return err
}
// ExportMetadata -
func (api *API) ExportMetadata(ctx context.Context, data *Metadata) (ExportMetadataResponse, error) {
req := request{
Type: "export_metadata",
Args: data,
}
var resp ExportMetadataResponse
err := api.post(ctx, "/v1/query", nil, req, &resp)
return resp, err
}
// ReplaceMetadata -
func (api *API) ReplaceMetadata(ctx context.Context, data *Metadata) error {
req := request{
Type: "replace_metadata",
Args: data,
}
var resp replaceMetadataResponse
if err := api.post(ctx, "/v1/query", nil, req, &resp); err != nil {
return err
}
if resp.Message == "success" {
return nil
}
return errors.Errorf("Can't replace hasura's metadata: %s", resp.Message)
}
// TrackTable -
func (api *API) TrackTable(ctx context.Context, schema, name string) error {
req := request{
Type: "track_table",
Args: map[string]string{
"schema": schema,
"name": name,
},
}
return api.post(ctx, "/v1/query", nil, req, nil)
}
// CreateSelectPermissions - A select permission is used to restrict access to only the specified columns and rows.
func (api *API) CreateSelectPermissions(ctx context.Context, table, role string, perm Permission) error {
req := request{
Type: "create_select_permission",
Args: map[string]interface{}{
"table": table,
"role": role,
"permission": perm,
},
}
return api.post(ctx, "/v1/query", nil, req, nil)
}
// DropSelectPermissions -
func (api *API) DropSelectPermissions(ctx context.Context, table, role string) error {
req := request{
Type: "drop_select_permission",
Args: map[string]interface{}{
"table": table,
"role": role,
},
}
return api.post(ctx, "/v1/query", nil, req, nil)
}
// CreateRestEndpoint -
func (api *API) CreateRestEndpoint(ctx context.Context, name, url, queryName, collectionName string) error {
req := request{
Type: "create_rest_endpoint",
Args: map[string]interface{}{
"name": name,
"url": url,
"methods": []string{"GET"},
"definition": map[string]interface{}{
"query": map[string]interface{}{
"query_name": queryName,
"collection_name": collectionName,
},
},
},
}
return api.post(ctx, "/v1/query", nil, req, nil)
}