-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
107 lines (94 loc) · 3.02 KB
/
blocks.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
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"
"net/url"
"strconv"
)
type Blocks struct {
Client *Client
}
func (s Blocks) basePath(stackId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/blocks", s.Client.Config.OrgName, stackId)
}
func (s Blocks) blockPath(stackId, blockId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/blocks/%d", s.Client.Config.OrgName, stackId, blockId)
}
// List - GET /orgs/:orgName/stacks/:stack_id/blocks
func (s Blocks) List(ctx context.Context, stackId int64) ([]types.Block, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(stackId), nil, nil, nil)
if err != nil {
return nil, err
}
var blocks []types.Block
if err := response.ReadJson(res, &blocks); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return blocks, nil
}
// Get - GET /orgs/:orgName/stacks/:stack_id/blocks/:id
func (s Blocks) Get(ctx context.Context, stackId, blockId int64, includeArchived bool) (*types.Block, error) {
q := url.Values{
"include_archived": []string{strconv.FormatBool(includeArchived)},
}
res, err := s.Client.Do(ctx, http.MethodGet, s.blockPath(stackId, blockId), q, nil, nil)
if err != nil {
return nil, err
}
var block types.Block
if err := response.ReadJson(res, &block); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &block, nil
}
// Create - POST /orgs/:orgName/stacks/:stack_id/blocks
func (s Blocks) Create(ctx context.Context, stackId int64, block *types.Block) (*types.Block, error) {
rawPayload, _ := json.Marshal(block)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(stackId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedBlock types.Block
if err := response.ReadJson(res, &updatedBlock); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedBlock, nil
}
// Update - PUT/PATCH /orgs/:orgName/stacks/:stack_id/blocks/:id
func (s Blocks) Update(ctx context.Context, stackId, blockId int64, block *types.Block) (*types.Block, error) {
rawPayload, _ := json.Marshal(block)
res, err := s.Client.Do(ctx, http.MethodPut, s.blockPath(stackId, blockId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedBlock types.Block
if err := response.ReadJson(res, &updatedBlock); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedBlock, nil
}
// Destroy - DELETE /orgs/:orgName/stacks/:stack_id/blocks/:id
func (s Blocks) Destroy(ctx context.Context, stackId, blockId int64) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.blockPath(stackId, blockId), nil, nil, nil)
if err != nil {
return false, err
}
if err := response.Verify(res); response.IsNotFoundError(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}