Skip to content

Commit 8e069b1

Browse files
nitishkumar71alexellis
authored andcommitted
add context in each method
Signed-off-by: Nitishkumar Singh <[email protected]> updated readme Signed-off-by: Nitishkumar Singh <[email protected]>
1 parent f9851ac commit 8e069b1

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ auth := &sdk.BasicAuth{
2828

2929
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
3030

31-
namespace, err := client.GetNamespaces()
31+
namespace, err := client.GetNamespaces(context.Background())
3232
```
3333

3434
### Authentication with IAM

client.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sdk
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -35,12 +36,12 @@ func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Clien
3536
}
3637

3738
// GetNamespaces get openfaas namespaces
38-
func (s *Client) GetNamespaces() ([]string, error) {
39+
func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
3940
u := s.GatewayURL
4041
namespaces := []string{}
4142
u.Path = "/system/namespaces"
4243

43-
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
44+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
4445
if err != nil {
4546
return namespaces, fmt.Errorf("unable to create request: %s, error: %w", u.String(), err)
4647
}
@@ -81,7 +82,7 @@ func (s *Client) GetNamespaces() ([]string, error) {
8182
}
8283

8384
// 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) {
8586
u := s.GatewayURL
8687

8788
u.Path = "/system/functions"
@@ -92,7 +93,7 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
9293
u.RawQuery = query.Encode()
9394
}
9495

95-
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
96+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
9697
if err != nil {
9798
return []types.FunctionStatus{}, fmt.Errorf("unable to create request for %s, error: %w", u.String(), err)
9899
}
@@ -123,12 +124,12 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
123124
return functions, nil
124125
}
125126

126-
func (s *Client) GetInfo() (SystemInfo, error) {
127+
func (s *Client) GetInfo(ctx context.Context) (SystemInfo, error) {
127128
u := s.GatewayURL
128129

129130
u.Path = "/system/info"
130131

131-
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
132+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
132133
if err != nil {
133134
return SystemInfo{}, fmt.Errorf("unable to create request for %s, error: %w", u.String(), err)
134135
}
@@ -160,7 +161,7 @@ func (s *Client) GetInfo() (SystemInfo, error) {
160161
}
161162

162163
// 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) {
164165
u := s.GatewayURL
165166

166167
u.Path = "/system/function/" + name
@@ -171,7 +172,7 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
171172
u.RawQuery = query.Encode()
172173
}
173174

174-
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
175+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
175176
if err != nil {
176177
return types.FunctionDeployment{}, fmt.Errorf("unable to create request for %s, error: %w", u.String(), err)
177178
}
@@ -202,16 +203,16 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
202203
return functions, nil
203204
}
204205

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)
207208

208209
}
209210

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)
212213
}
213214

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) {
215216

216217
bodyBytes, err := json.Marshal(spec)
217218
if err != nil {
@@ -223,7 +224,7 @@ func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, erro
223224
u := s.GatewayURL
224225
u.Path = "/system/functions"
225226

226-
req, err := http.NewRequest(method, u.String(), bodyReader)
227+
req, err := http.NewRequestWithContext(ctx, method, u.String(), bodyReader)
227228
if err != nil {
228229
return http.StatusBadGateway, err
229230
}
@@ -258,7 +259,7 @@ func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, erro
258259
}
259260

260261
// 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 {
262263

263264
scaleReq := types.ScaleServiceRequest{
264265
ServiceName: functionName,
@@ -277,7 +278,7 @@ func (s *Client) ScaleFunction(functionName, namespace string, replicas uint64)
277278

278279
u.Path = functionPath
279280

280-
req, err := http.NewRequest(http.MethodPost, u.String(), bodyReader)
281+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bodyReader)
281282
if err != nil {
282283
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s, error: %s", u.String(), err)
283284
}
@@ -320,7 +321,7 @@ func (s *Client) ScaleFunction(functionName, namespace string, replicas uint64)
320321
}
321322

322323
// 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 {
324325

325326
delReq := types.DeleteFunctionRequest{
326327
FunctionName: functionName,
@@ -335,7 +336,7 @@ func (s *Client) DeleteFunction(functionName, namespace string) error {
335336
u := s.GatewayURL
336337
u.Path = "/system/functions"
337338

338-
req, err := http.NewRequest(http.MethodDelete, u.String(), bodyReader)
339+
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), bodyReader)
339340
if err != nil {
340341
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s, error: %s", u.String(), err)
341342
}

client_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sdk
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"net/http"
@@ -20,7 +21,7 @@ func TestSdk_GetNamespaces_TwoNamespaces(t *testing.T) {
2021
sU, _ := url.Parse(s.URL)
2122

2223
client := NewClient(sU, nil, http.DefaultClient)
23-
ns, err := client.GetNamespaces()
24+
ns, err := client.GetNamespaces(context.Background())
2425
if err != nil {
2526
t.Fatalf("wanted no error, but got: %s", err)
2627
}
@@ -52,7 +53,7 @@ func TestSdk_GetNamespaces_NoNamespaces(t *testing.T) {
5253
sU, _ := url.Parse(s.URL)
5354

5455
client := NewClient(sU, nil, http.DefaultClient)
55-
ns, err := client.GetNamespaces()
56+
ns, err := client.GetNamespaces(context.Background())
5657
if err != nil {
5758
t.Fatalf("wanted no error, but got: %s", err)
5859
}
@@ -128,7 +129,8 @@ func TestSdk_DeployFunction(t *testing.T) {
128129
sU, _ := url.Parse(s.URL)
129130

130131
client := NewClient(sU, nil, http.DefaultClient)
131-
_, err := client.Deploy(types.FunctionDeployment{
132+
133+
_, err := client.Deploy(context.Background(), types.FunctionDeployment{
132134
Service: funcName,
133135
Image: fmt.Sprintf("docker.io/openfaas/%s:latest", funcName),
134136
Namespace: nsName,
@@ -195,7 +197,7 @@ func TestSdk_DeleteFunction(t *testing.T) {
195197
sU, _ := url.Parse(s.URL)
196198

197199
client := NewClient(sU, nil, http.DefaultClient)
198-
err := client.DeleteFunction(test.functionName, test.namespace)
200+
err := client.DeleteFunction(context.Background(), test.functionName, test.namespace)
199201

200202
if !errors.Is(err, test.err) && err.Error() != test.err.Error() {
201203
t.Fatalf("wanted %s, but got: %s", test.err, err)

0 commit comments

Comments
 (0)