@@ -5,11 +5,13 @@ import (
5
5
"crypto/tls"
6
6
"encoding/json"
7
7
"fmt"
8
+ "io"
8
9
"io/ioutil"
9
10
"net/http"
10
11
11
12
"github.com/clems4ever/go-graphkb/internal/knowledge"
12
13
"github.com/clems4ever/go-graphkb/internal/schema"
14
+ "github.com/clems4ever/go-graphkb/internal/utils"
13
15
)
14
16
15
17
// ErrTooManyRequests error representing too many requests to the API
@@ -37,11 +39,18 @@ func NewGraphClient(URL, authToken string, skipVerify bool) *GraphClient {
37
39
}
38
40
}
39
41
42
+ func (gc * GraphClient ) newRequest (method , path string , body io.Reader ) (* http.Request , error ) {
43
+ req , err := http .NewRequest (method , fmt .Sprintf ("%s%s" , gc .url , path ), body )
44
+ if err != nil {
45
+ return nil , err
46
+ }
47
+ req .Header .Add (utils .XAuthTokenHeader , gc .authToken )
48
+ return req , nil
49
+ }
50
+
40
51
// ReadCurrentGraph read the current graph stored in graph kb
41
52
func (gc * GraphClient ) ReadCurrentGraph () (* knowledge.Graph , error ) {
42
- url := fmt .Sprintf ("%s/api/graph/read?token=%s" , gc .url , gc .authToken )
43
-
44
- req , err := http .NewRequest ("GET" , url , nil )
53
+ req , err := gc .newRequest ("GET" , "/api/graph/read" , nil )
45
54
if err != nil {
46
55
return nil , err
47
56
}
@@ -81,8 +90,7 @@ func (gc *GraphClient) UpdateSchema(sg schema.SchemaGraph) error {
81
90
return fmt .Errorf ("Unable to marshall request body" )
82
91
}
83
92
84
- url := fmt .Sprintf ("%s/api/graph/schema?token=%s" , gc .url , gc .authToken )
85
- req , err := http .NewRequest ("PUT" , url , bytes .NewBuffer (b ))
93
+ req , err := gc .newRequest ("PUT" , "/api/graph/schema" , bytes .NewBuffer (b ))
86
94
if err != nil {
87
95
return err
88
96
}
@@ -113,8 +121,7 @@ func (gc *GraphClient) InsertAssets(assets []knowledge.Asset) error {
113
121
return fmt .Errorf ("Unable to marshall request body" )
114
122
}
115
123
116
- url := fmt .Sprintf ("%s/api/graph/asset?token=%s" , gc .url , gc .authToken )
117
- req , err := http .NewRequest ("PUT" , url , bytes .NewBuffer (b ))
124
+ req , err := gc .newRequest ("PUT" , "/api/graph/assets" , bytes .NewBuffer (b ))
118
125
if err != nil {
119
126
return err
120
127
}
@@ -145,8 +152,7 @@ func (gc *GraphClient) DeleteAssets(assets []knowledge.Asset) error {
145
152
return fmt .Errorf ("Unable to marshall request body" )
146
153
}
147
154
148
- url := fmt .Sprintf ("%s/api/graph/asset?token=%s" , gc .url , gc .authToken )
149
- req , err := http .NewRequest ("DELETE" , url , bytes .NewBuffer (b ))
155
+ req , err := gc .newRequest ("DELETE" , "/api/graph/assets" , bytes .NewBuffer (b ))
150
156
if err != nil {
151
157
return err
152
158
}
@@ -177,8 +183,7 @@ func (gc *GraphClient) InsertRelations(relations []knowledge.Relation) error {
177
183
return fmt .Errorf ("Unable to marshall request body" )
178
184
}
179
185
180
- url := fmt .Sprintf ("%s/api/graph/relation?token=%s" , gc .url , gc .authToken )
181
- req , err := http .NewRequest ("PUT" , url , bytes .NewBuffer (b ))
186
+ req , err := gc .newRequest ("PUT" , "/api/graph/relations" , bytes .NewBuffer (b ))
182
187
if err != nil {
183
188
return err
184
189
}
@@ -209,8 +214,7 @@ func (gc *GraphClient) DeleteRelations(relations []knowledge.Relation) error {
209
214
return fmt .Errorf ("Unable to marshall request body" )
210
215
}
211
216
212
- url := fmt .Sprintf ("%s/api/graph/relation?token=%s" , gc .url , gc .authToken )
213
- req , err := http .NewRequest ("DELETE" , url , bytes .NewBuffer (b ))
217
+ req , err := gc .newRequest ("DELETE" , "/api/graph/relations" , bytes .NewBuffer (b ))
214
218
if err != nil {
215
219
return err
216
220
}
0 commit comments