-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgraph.go
66 lines (50 loc) · 1.37 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package orchestrate
import (
"encoding/json"
"fmt"
"log"
"net/url"
)
type GraphResults struct {
Count uint64 `json:"count"`
Results []GraphResult `json:"results"`
}
type GraphResult struct {
Collection string `json:"collection"`
Key string `json:"key"`
Ref string `json:"ref"`
Value map[string]interface{} `json:"value"`
}
func (client Client) GetRelations(collection string, key string, hops []string) (*GraphResults, error) {
queryVariables := url.Values{
"hop": hops,
}
resp, err := client.doRequest("GET", collection+"/"+key+"/relations?"+queryVariables.Encode(), nil)
if err != nil {
log.Fatal(err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, newError(resp)
}
decoder := json.NewDecoder(resp.Body)
result := new(GraphResults)
err = decoder.Decode(result)
if err != nil {
log.Fatal(err)
}
return result, err
}
func (client Client) PutRelation(sourceCollection string, sourceKey string, kind string, sinkCollection string, sinkKey string) error {
resp, err := client.doRequest("PUT", fmt.Sprintf("%v/%v/relations/%v/%v/%v", sourceCollection, sourceKey, kind, sinkCollection, sinkKey), nil)
if err != nil {
log.Fatal(err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != 204 {
err = newError(resp)
}
return err
}