@@ -2,6 +2,7 @@ package hasura
22
33import (
44 "bytes"
5+ "context"
56 "net/http"
67 "net/url"
78 "path"
@@ -41,12 +42,12 @@ func (api *API) buildURL(endpoint string, args map[string]string) (string, error
4142 return u .String (), nil
4243}
4344
44- func (api * API ) get (endpoint string , args map [string ]string ) (* http.Response , error ) {
45+ func (api * API ) get (ctx context. Context , endpoint string , args map [string ]string ) (* http.Response , error ) {
4546 url , err := api .buildURL (endpoint , args )
4647 if err != nil {
4748 return nil , err
4849 }
49- req , err := http .NewRequest ( http .MethodGet , url , nil )
50+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , url , nil )
5051 if err != nil {
5152 return nil , err
5253 }
@@ -55,7 +56,7 @@ func (api *API) get(endpoint string, args map[string]string) (*http.Response, er
5556}
5657
5758//nolint
58- func (api * API ) post (endpoint string , args map [string ]string , body interface {}, output interface {}) error {
59+ func (api * API ) post (ctx context. Context , endpoint string , args map [string ]string , body interface {}, output interface {}) error {
5960 url , err := api .buildURL (endpoint , args )
6061 if err != nil {
6162 return err
@@ -66,7 +67,7 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
6667 return err
6768 }
6869
69- req , err := http .NewRequest ( http .MethodPost , url , bytes .NewBuffer (postBody ))
70+ req , err := http .NewRequestWithContext ( ctx , http .MethodPost , url , bytes .NewBuffer (postBody ))
7071 if err != nil {
7172 return err
7273 }
@@ -97,8 +98,8 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
9798}
9899
99100// Health
100- func (api * API ) Health () error {
101- resp , err := api .get ("/healthz" , nil )
101+ func (api * API ) Health (ctx context. Context ) error {
102+ resp , err := api .get (ctx , "/healthz" , nil )
102103 if err != nil {
103104 return err
104105 }
@@ -111,24 +112,24 @@ func (api *API) Health() error {
111112}
112113
113114// ExportMetadata -
114- func (api * API ) ExportMetadata (data * Metadata ) (ExportMetadataResponse , error ) {
115+ func (api * API ) ExportMetadata (ctx context. Context , data * Metadata ) (ExportMetadataResponse , error ) {
115116 req := request {
116117 Type : "export_metadata" ,
117118 Args : data ,
118119 }
119120 var resp ExportMetadataResponse
120- err := api .post ("/v1/query" , nil , req , & resp )
121+ err := api .post (ctx , "/v1/query" , nil , req , & resp )
121122 return resp , err
122123}
123124
124125// ReplaceMetadata -
125- func (api * API ) ReplaceMetadata (data * Metadata ) error {
126+ func (api * API ) ReplaceMetadata (ctx context. Context , data * Metadata ) error {
126127 req := request {
127128 Type : "replace_metadata" ,
128129 Args : data ,
129130 }
130131 var resp replaceMetadataResponse
131- if err := api .post ("/v1/query" , nil , req , & resp ); err != nil {
132+ if err := api .post (ctx , "/v1/query" , nil , req , & resp ); err != nil {
132133 return err
133134 }
134135 if resp .Message == "success" {
@@ -138,19 +139,19 @@ func (api *API) ReplaceMetadata(data *Metadata) error {
138139}
139140
140141// TrackTable -
141- func (api * API ) TrackTable (schema , name string ) error {
142+ func (api * API ) TrackTable (ctx context. Context , schema , name string ) error {
142143 req := request {
143144 Type : "track_table" ,
144145 Args : map [string ]string {
145146 "schema" : schema ,
146147 "name" : name ,
147148 },
148149 }
149- return api .post ("/v1/query" , nil , req , nil )
150+ return api .post (ctx , "/v1/query" , nil , req , nil )
150151}
151152
152153// CreateSelectPermissions - A select permission is used to restrict access to only the specified columns and rows.
153- func (api * API ) CreateSelectPermissions (table , role string , perm Permission ) error {
154+ func (api * API ) CreateSelectPermissions (ctx context. Context , table , role string , perm Permission ) error {
154155 req := request {
155156 Type : "create_select_permission" ,
156157 Args : map [string ]interface {}{
@@ -159,23 +160,23 @@ func (api *API) CreateSelectPermissions(table, role string, perm Permission) err
159160 "permission" : perm ,
160161 },
161162 }
162- return api .post ("/v1/query" , nil , req , nil )
163+ return api .post (ctx , "/v1/query" , nil , req , nil )
163164}
164165
165166// DropSelectPermissions -
166- func (api * API ) DropSelectPermissions (table , role string ) error {
167+ func (api * API ) DropSelectPermissions (ctx context. Context , table , role string ) error {
167168 req := request {
168169 Type : "drop_select_permission" ,
169170 Args : map [string ]interface {}{
170171 "table" : table ,
171172 "role" : role ,
172173 },
173174 }
174- return api .post ("/v1/query" , nil , req , nil )
175+ return api .post (ctx , "/v1/query" , nil , req , nil )
175176}
176177
177178// CreateRestEndpoint -
178- func (api * API ) CreateRestEndpoint (name , url , queryName , collectionName string ) error {
179+ func (api * API ) CreateRestEndpoint (ctx context. Context , name , url , queryName , collectionName string ) error {
179180 req := request {
180181 Type : "create_rest_endpoint" ,
181182 Args : map [string ]interface {}{
@@ -190,5 +191,5 @@ func (api *API) CreateRestEndpoint(name, url, queryName, collectionName string)
190191 },
191192 },
192193 }
193- return api .post ("/v1/query" , nil , req , nil )
194+ return api .post (ctx , "/v1/query" , nil , req , nil )
194195}
0 commit comments