@@ -2,6 +2,7 @@ package sdk
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"encoding/json"
6
7
"fmt"
7
8
"io"
@@ -35,12 +36,12 @@ func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Clien
35
36
}
36
37
37
38
// GetNamespaces get openfaas namespaces
38
- func (s * Client ) GetNamespaces () ([]string , error ) {
39
+ func (s * Client ) GetNamespaces (ctx context. Context ) ([]string , error ) {
39
40
u := s .GatewayURL
40
41
namespaces := []string {}
41
42
u .Path = "/system/namespaces"
42
43
43
- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
44
+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
44
45
if err != nil {
45
46
return namespaces , fmt .Errorf ("unable to create request: %s, error: %w" , u .String (), err )
46
47
}
@@ -81,7 +82,7 @@ func (s *Client) GetNamespaces() ([]string, error) {
81
82
}
82
83
83
84
// GetFunctions lists all functions
84
- func (s * Client ) GetFunctions (namespace string ) ([]types.FunctionStatus , error ) {
85
+ func (s * Client ) GetFunctions (ctx context. Context , namespace string ) ([]types.FunctionStatus , error ) {
85
86
u := s .GatewayURL
86
87
87
88
u .Path = "/system/functions"
@@ -92,7 +93,7 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
92
93
u .RawQuery = query .Encode ()
93
94
}
94
95
95
- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
96
+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
96
97
if err != nil {
97
98
return []types.FunctionStatus {}, fmt .Errorf ("unable to create request for %s, error: %w" , u .String (), err )
98
99
}
@@ -123,12 +124,12 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
123
124
return functions , nil
124
125
}
125
126
126
- func (s * Client ) GetInfo () (SystemInfo , error ) {
127
+ func (s * Client ) GetInfo (ctx context. Context ) (SystemInfo , error ) {
127
128
u := s .GatewayURL
128
129
129
130
u .Path = "/system/info"
130
131
131
- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
132
+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
132
133
if err != nil {
133
134
return SystemInfo {}, fmt .Errorf ("unable to create request for %s, error: %w" , u .String (), err )
134
135
}
@@ -160,7 +161,7 @@ func (s *Client) GetInfo() (SystemInfo, error) {
160
161
}
161
162
162
163
// GetFunction gives a richer payload than GetFunctions, but for a specific function
163
- func (s * Client ) GetFunction (name , namespace string ) (types.FunctionDeployment , error ) {
164
+ func (s * Client ) GetFunction (ctx context. Context , name , namespace string ) (types.FunctionDeployment , error ) {
164
165
u := s .GatewayURL
165
166
166
167
u .Path = "/system/function/" + name
@@ -171,7 +172,7 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
171
172
u .RawQuery = query .Encode ()
172
173
}
173
174
174
- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
175
+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
175
176
if err != nil {
176
177
return types.FunctionDeployment {}, fmt .Errorf ("unable to create request for %s, error: %w" , u .String (), err )
177
178
}
@@ -202,16 +203,16 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
202
203
return functions , nil
203
204
}
204
205
205
- func (s * Client ) Deploy (spec types.FunctionDeployment ) (int , error ) {
206
- return s .deploy (http .MethodPost , spec )
206
+ func (s * Client ) Deploy (ctx context. Context , spec types.FunctionDeployment ) (int , error ) {
207
+ return s .deploy (ctx , http .MethodPost , spec )
207
208
208
209
}
209
210
210
- func (s * Client ) Update (spec types.FunctionDeployment ) (int , error ) {
211
- return s .deploy (http .MethodPut , spec )
211
+ func (s * Client ) Update (ctx context. Context , spec types.FunctionDeployment ) (int , error ) {
212
+ return s .deploy (ctx , http .MethodPut , spec )
212
213
}
213
214
214
- func (s * Client ) deploy (method string , spec types.FunctionDeployment ) (int , error ) {
215
+ func (s * Client ) deploy (ctx context. Context , method string , spec types.FunctionDeployment ) (int , error ) {
215
216
216
217
bodyBytes , err := json .Marshal (spec )
217
218
if err != nil {
@@ -223,7 +224,7 @@ func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, erro
223
224
u := s .GatewayURL
224
225
u .Path = "/system/functions"
225
226
226
- req , err := http .NewRequest ( method , u .String (), bodyReader )
227
+ req , err := http .NewRequestWithContext ( ctx , method , u .String (), bodyReader )
227
228
if err != nil {
228
229
return http .StatusBadGateway , err
229
230
}
@@ -258,7 +259,7 @@ func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, erro
258
259
}
259
260
260
261
// ScaleFunction scales a function to a number of replicas
261
- func (s * Client ) ScaleFunction (functionName , namespace string , replicas uint64 ) error {
262
+ func (s * Client ) ScaleFunction (ctx context. Context , functionName , namespace string , replicas uint64 ) error {
262
263
263
264
scaleReq := types.ScaleServiceRequest {
264
265
ServiceName : functionName ,
@@ -277,7 +278,7 @@ func (s *Client) ScaleFunction(functionName, namespace string, replicas uint64)
277
278
278
279
u .Path = functionPath
279
280
280
- req , err := http .NewRequest ( http .MethodPost , u .String (), bodyReader )
281
+ req , err := http .NewRequestWithContext ( ctx , http .MethodPost , u .String (), bodyReader )
281
282
if err != nil {
282
283
return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , u .String (), err )
283
284
}
@@ -320,7 +321,7 @@ func (s *Client) ScaleFunction(functionName, namespace string, replicas uint64)
320
321
}
321
322
322
323
// DeleteFunction deletes a function
323
- func (s * Client ) DeleteFunction (functionName , namespace string ) error {
324
+ func (s * Client ) DeleteFunction (ctx context. Context , functionName , namespace string ) error {
324
325
325
326
delReq := types.DeleteFunctionRequest {
326
327
FunctionName : functionName ,
@@ -335,7 +336,7 @@ func (s *Client) DeleteFunction(functionName, namespace string) error {
335
336
u := s .GatewayURL
336
337
u .Path = "/system/functions"
337
338
338
- req , err := http .NewRequest ( http .MethodDelete , u .String (), bodyReader )
339
+ req , err := http .NewRequestWithContext ( ctx , http .MethodDelete , u .String (), bodyReader )
339
340
if err != nil {
340
341
return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , u .String (), err )
341
342
}
0 commit comments