Skip to content

Commit a2411ad

Browse files
authored
Merge pull request #85 from ShimmerGlass/client-query-ctx
chore(client): pass the context during query http call
2 parents 0fa95d9 + e30c0f8 commit a2411ad

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

internal/client/graph_client.go

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

33
import (
44
"bytes"
5+
"context"
56
"crypto/tls"
67
"encoding/json"
78
"fmt"
@@ -44,11 +45,13 @@ func NewGraphClient(URL, authToken, basicAuthUser, basicAuthPass string, skipVer
4445
}
4546
}
4647

47-
func (gc *GraphClient) newRequest(method, path string, body io.Reader) (*http.Request, error) {
48+
func (gc *GraphClient) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
4849
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", gc.url, path), body)
4950
if err != nil {
5051
return nil, err
5152
}
53+
req = req.WithContext(ctx)
54+
5255
if gc.authToken != "" {
5356
req.Header.Add(utils.XAuthTokenHeader, gc.authToken)
5457
}
@@ -60,7 +63,7 @@ func (gc *GraphClient) newRequest(method, path string, body io.Reader) (*http.Re
6063

6164
// ReadCurrentGraph read the current graph stored in graph kb
6265
func (gc *GraphClient) ReadCurrentGraph() (*knowledge.Graph, error) {
63-
req, err := gc.newRequest("GET", "/api/graph/read", nil)
66+
req, err := gc.newRequest(context.Background(), "GET", "/api/graph/read", nil)
6467
if err != nil {
6568
return nil, err
6669
}
@@ -96,7 +99,7 @@ func (gc *GraphClient) UpdateSchema(sg schema.SchemaGraph) error {
9699
return fmt.Errorf("Unable to marshall request body")
97100
}
98101

99-
req, err := gc.newRequest("PUT", "/api/graph/schema", bytes.NewBuffer(b))
102+
req, err := gc.newRequest(context.Background(), "PUT", "/api/graph/schema", bytes.NewBuffer(b))
100103
if err != nil {
101104
return err
102105
}
@@ -127,7 +130,7 @@ func (gc *GraphClient) InsertAssets(assets []knowledge.Asset) error {
127130
return fmt.Errorf("Unable to marshall request body")
128131
}
129132

130-
req, err := gc.newRequest("PUT", "/api/graph/assets", bytes.NewBuffer(b))
133+
req, err := gc.newRequest(context.Background(), "PUT", "/api/graph/assets", bytes.NewBuffer(b))
131134
if err != nil {
132135
return err
133136
}
@@ -158,7 +161,7 @@ func (gc *GraphClient) DeleteAssets(assets []knowledge.Asset) error {
158161
return fmt.Errorf("Unable to marshall request body")
159162
}
160163

161-
req, err := gc.newRequest("DELETE", "/api/graph/assets", bytes.NewBuffer(b))
164+
req, err := gc.newRequest(context.Background(), "DELETE", "/api/graph/assets", bytes.NewBuffer(b))
162165
if err != nil {
163166
return err
164167
}
@@ -189,7 +192,7 @@ func (gc *GraphClient) InsertRelations(relations []knowledge.Relation) error {
189192
return fmt.Errorf("Unable to marshall request body")
190193
}
191194

192-
req, err := gc.newRequest("PUT", "/api/graph/relations", bytes.NewBuffer(b))
195+
req, err := gc.newRequest(context.Background(), "PUT", "/api/graph/relations", bytes.NewBuffer(b))
193196
if err != nil {
194197
return err
195198
}
@@ -220,7 +223,7 @@ func (gc *GraphClient) DeleteRelations(relations []knowledge.Relation) error {
220223
return fmt.Errorf("Unable to marshall request body")
221224
}
222225

223-
req, err := gc.newRequest("DELETE", "/api/graph/relations", bytes.NewBuffer(b))
226+
req, err := gc.newRequest(context.Background(), "DELETE", "/api/graph/relations", bytes.NewBuffer(b))
224227
if err != nil {
225228
return err
226229
}

internal/client/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (gapi *GraphAPI) Query(ctx context.Context, q string, includeSources bool)
1717
return nil, fmt.Errorf("Unable to marshall request body")
1818
}
1919

20-
req, err := gapi.client.newRequest("POST", "/api/query", bytes.NewBuffer(b))
20+
req, err := gapi.client.newRequest(ctx, "POST", "/api/query", bytes.NewBuffer(b))
2121
if err != nil {
2222
return nil, err
2323
}

0 commit comments

Comments
 (0)