-
Notifications
You must be signed in to change notification settings - Fork 12
/
integration.go
83 lines (73 loc) · 1.87 KB
/
integration.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package opslevel
import (
"fmt"
"github.com/gosimple/slug"
"github.com/shurcooL/graphql"
)
type Integration struct {
Id graphql.ID `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
type IntegrationConnection struct {
Nodes []Integration
PageInfo PageInfo
TotalCount graphql.Int
}
func (self *Integration) Alias() string {
return fmt.Sprintf("%s-%s", slug.Make(self.Type), slug.Make(self.Name))
}
func (conn *IntegrationConnection) Hydrate(client *Client) error {
var q struct {
Account struct {
Integrations IntegrationConnection `graphql:"integrations(after: $after, first: $first)"`
}
}
v := PayloadVariables{
"first": client.pageSize,
}
q.Account.Integrations.PageInfo = conn.PageInfo
for q.Account.Integrations.PageInfo.HasNextPage {
v["after"] = q.Account.Integrations.PageInfo.End
if err := client.Query(&q, v); err != nil {
return err
}
for _, item := range q.Account.Integrations.Nodes {
conn.Nodes = append(conn.Nodes, item)
}
}
return nil
}
//#region Retrieve
func (client *Client) GetIntegration(id graphql.ID) (*Integration, error) {
var q struct {
Account struct {
Integration Integration `graphql:"integration(id: $id)"`
}
}
v := PayloadVariables{
"id": id,
}
if err := client.Query(&q, v); err != nil {
return nil, err
}
if q.Account.Integration.Id == nil {
return nil, fmt.Errorf("Integration with ID '%s' not found!", id)
}
return &q.Account.Integration, nil
}
func (client *Client) ListIntegrations() ([]Integration, error) {
var q struct {
Account struct {
Integrations IntegrationConnection `graphql:"integrations"`
}
}
if err := client.Query(&q, nil); err != nil {
return q.Account.Integrations.Nodes, err
}
if err := q.Account.Integrations.Hydrate(client); err != nil {
return q.Account.Integrations.Nodes, err
}
return q.Account.Integrations.Nodes, nil
}
//#endregion