-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_app.go
159 lines (120 loc) · 2.58 KB
/
api_app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package raptor
import (
"encoding/json"
"errors"
"fmt"
"github.com/raptorbox/raptor-sdk-go/models"
)
//CreateApp instantiate a new API client
func CreateApp(r *Raptor) *App {
return &App{
Raptor: r,
}
}
//App API client
type App struct {
Raptor *Raptor
}
//NewApp return a new App instance
func (i *App) NewApp() *models.App {
userID := ""
if i.Raptor.Auth().GetUser() != nil {
userID = i.Raptor.Auth().GetUser().ID
}
app := models.NewApp()
app.UserID = userID
return app
}
//NewAppQuery return a new AppQuery instance
func (i *App) NewAppQuery() *models.AppQuery {
return models.NewAppQuery()
}
//GetConfig return the configuration
func (i *App) GetConfig() models.Config {
return i.Raptor.GetConfig()
}
//GetClient return a client instance
func (i *App) GetClient() models.Client {
return i.Raptor.GetClient()
}
//Load a app by ID
func (i *App) Load(ID string) (*models.App, error) {
raw, err := i.GetClient().Get(fmt.Sprintf(APP_GET, ID), nil)
if err != nil {
return nil, err
}
app := models.NewApp()
err = json.Unmarshal(raw, app)
if err != nil {
return nil, err
}
return app, nil
}
//List apps accessible by an user
func (i *App) List() (*models.AppPager, error) {
raw, err := i.GetClient().Get(APP_LIST, nil)
if err != nil {
return nil, err
}
return models.ParseAppPager(raw)
}
//Search for apps
func (i *App) Search(q *models.AppQuery) (*models.AppPager, error) {
if q == nil {
return nil, errors.New("Query is missing")
}
raw, err := i.GetClient().Post(APP_SEARCH, q, nil)
if err != nil {
return nil, err
}
return models.ParseAppPager(raw)
}
//Create a app
func (i *App) Create(app *models.App) error {
raw, err := i.GetClient().Post(APP_CREATE, app, nil)
if err != nil {
return err
}
res := &models.App{}
err = i.GetClient().FromJSON(raw, res)
if err != nil {
return err
}
err = app.Merge(*res)
if err != nil {
return err
}
return nil
}
//Update a app
func (i *App) Update(app *models.App) error {
if app.ID == "" {
return errors.New("App ID is missing")
}
raw, err := i.GetClient().Put(fmt.Sprintf(APP_UPDATE, app.ID), app, nil)
if err != nil {
return err
}
res := &models.App{}
err = i.GetClient().FromJSON(raw, res)
if err != nil {
return err
}
err = app.Merge(*res)
if err != nil {
return err
}
return nil
}
//DeleteByID a app by ID
func (i *App) DeleteByID(id string) error {
err := i.GetClient().Delete(fmt.Sprintf(APP_DELETE, id), nil)
return err
}
//Delete a app
func (i *App) Delete(app *models.App) error {
if app.ID == "" {
return errors.New("App ID is missing")
}
return i.DeleteByID(app.ID)
}