-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.go
More file actions
109 lines (94 loc) · 3.12 KB
/
apps.go
File metadata and controls
109 lines (94 loc) · 3.12 KB
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 Apps struct {
Client *Client
}
func (a Apps) globalPath() string {
return fmt.Sprintf("orgs/%s/apps", a.Client.Config.OrgName)
}
func (a Apps) basePath(stackId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/apps", a.Client.Config.OrgName, stackId)
}
func (a Apps) appPath(stackId, appId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/apps/%d", a.Client.Config.OrgName, stackId, appId)
}
// GlobalList - GET /orgs/:orgName/apps
func (a Apps) GlobalList(ctx context.Context) ([]types.Application, error) {
res, err := a.Client.Do(ctx, http.MethodGet, a.globalPath(), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Application](res)
}
// List - GET /orgs/:orgName/stacks/:stackId/apps
func (a Apps) List(ctx context.Context, stackId int64) ([]types.Application, error) {
res, err := a.Client.Do(ctx, http.MethodGet, a.basePath(stackId), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Application](res)
}
// Get - GET /orgs/:orgName/stacks/:stackId/apps/:id
func (a Apps) Get(ctx context.Context, stackId, appId int64) (*types.Application, error) {
res, err := a.Client.Do(ctx, http.MethodGet, a.appPath(stackId, appId), nil, nil, nil)
if err != nil {
return nil, err
}
var app types.Application
if err := response.ReadJson(res, &app); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &app, nil
}
// Create - POST /orgs/:orgName/stacks/:stackId/apps
func (a Apps) Create(ctx context.Context, stackId int64, app *types.Application) (*types.Application, error) {
rawPayload, _ := json.Marshal(app)
res, err := a.Client.Do(ctx, http.MethodPost, a.basePath(stackId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedApp types.Application
if err := response.ReadJson(res, &updatedApp); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedApp, nil
}
// Update - PUT/PATCH /orgs/:orgName/stacks/:stackId/apps/:id
func (a Apps) Update(ctx context.Context, stackId, appId int64, app *types.Application) (*types.Application, error) {
rawPayload, _ := json.Marshal(app)
res, err := a.Client.Do(ctx, http.MethodPut, a.appPath(stackId, appId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedApp types.Application
if err := response.ReadJson(res, &updatedApp); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedApp, nil
}
// Destroy - DELETE /orgs/:orgName/stacks/:stackId/apps/:id
func (a Apps) Destroy(ctx context.Context, stackId, appId int64) (bool, error) {
res, err := a.Client.Do(ctx, http.MethodDelete, a.appPath(stackId, appId), 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
}