-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrations.go
109 lines (98 loc) · 3.25 KB
/
integrations.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package api
import (
"context"
"encoding/json"
"fmt"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"net/http"
)
type Integrations struct {
Client *Client
}
func (s Integrations) basePath() string {
return fmt.Sprintf("/orgs/%s/integrations", s.Client.Config.OrgName)
}
func (s Integrations) integrationPath(id int64) string {
return fmt.Sprintf("/orgs/%s/integrations/%d", s.Client.Config.OrgName, id)
}
func (s Integrations) oauthPath(id int64) string {
return fmt.Sprintf("/orgs/%s/integrations/%d/oauth", s.Client.Config.OrgName, id)
}
func (s Integrations) statusPath(id int64) string {
return fmt.Sprintf("/orgs/%s/integrations/%d/status", s.Client.Config.OrgName, id)
}
// List - GET /orgs/:orgName/integrations
func (s Integrations) List(ctx context.Context) ([]types.Integration, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Integration](res)
}
// Create - POST /orgs/:orgName/integrations
func (s Integrations) Create(ctx context.Context, integration types.Integration) (*types.Integration, error) {
rawPayload, _ := json.Marshal(integration)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Integration](res)
}
// Get - GET /orgs/:orgName/integrations/:integrationId
func (s Integrations) Get(ctx context.Context, id int64) (*types.Integration, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.integrationPath(id), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Integration](res)
}
// Delete - DELETE /orgs/:orgName/integrations/:integrationId
func (s Integrations) Delete(ctx context.Context, id int64) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.integrationPath(id), nil, nil, nil)
if err != nil {
return false, err
}
err = response.Verify(res)
if err != nil {
if response.IsNotFoundError(err) {
return false, nil
}
return false, err
}
return true, nil
}
type GetOauthResponse struct {
Url string `json:"url"`
}
// GetOauth - GET /orgs/:orgName/integrations/:integrationId/oauth
func (s Integrations) GetOauth(ctx context.Context, id int64) (*GetOauthResponse, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.oauthPath(id), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[GetOauthResponse](res)
}
// DeleteOauth - DELETE /orgs/:orgName/integrations/:integrationId/oauth
func (s Integrations) DeleteOauth(ctx context.Context, id int64) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.oauthPath(id), nil, nil, nil)
if err != nil {
return false, err
}
err = response.Verify(res)
if err != nil {
if response.IsNotFoundError(err) {
return false, nil
}
return false, err
}
return true, nil
}
// GetStatus - GET /orgs/:orgName/integrations/:integrationId/status
func (s Integrations) GetStatus(ctx context.Context, id int64) (*types.IntegrationStatus, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.statusPath(id), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.IntegrationStatus](res)
}