1
9
10 package strapi
11
12 import (
13 "bytes"
14 "fmt"
15 "mime/multipart"
16 "net/http"
17 "strconv"
18
19 "gitlab.hexacode.org/go-libs/hctypes"
20 )
21
22 type StrapiClassic struct {
23 url string
24 token string
25 }
26
27
28
29
30
31
32
33
34 func NewStrapiWithToken(url, token string) *StrapiClassic {
35 strapi := StrapiClassic{
36 url: url,
37 token: token,
38 }
39
40 return &strapi
41 }
42
43
44
45
46
47
48
49
50
51
52 func NewStrapiWithLogin(url, identifier, password string) (*StrapiClassic, error) {
53 strapi := &StrapiClassic{
54 url: url,
55 }
56
57 status, data, err := strapi.Login(identifier, password)
58 if err != nil {
59 return nil, err
60 }
61
62 if status != http.StatusOK {
63 if data.ContainsKey("error") {
64 err2 := data["error"].(hctypes.Dict)
65 if err2.ContainsKey("message") {
66 return nil, fmt.Errorf(string(err2["message"].(hctypes.String)))
67 }
68 }
69
70 return nil, fmt.Errorf("Internal server error")
71 }
72
73 if !data.ContainsKey("jwt") {
74 return nil, fmt.Errorf("Internal server error")
75 }
76
77 strapi.token = string(data["jwt"].(hctypes.String))
78
79 return strapi, nil
80 }
81
82 func (strapi *StrapiClassic) GetToken() string {
83 return strapi.token
84 }
85
86
87
88
89
90 func (strapi *StrapiClassic) FindOne(path string, id int64) (int, hctypes.Dict, error) {
91 return strapi.base("GET", strapi.urlParam(path, id, nil), bytes.NewReader([]byte{}))
92 }
93
94
95
96
97
98
99 func (strapi *StrapiClassic) Find(path string, params hctypes.Dict) (int, hctypes.Dict, error) {
100 m_params := hctypes.Dict{}
101 if params != nil {
102 m_params = params.Copy()
103 }
104
105 return strapi.base("GET", strapi.urlParam(path, 0, m_params), bytes.NewReader([]byte{}))
106 }
107
108
109
110
111
112 func (strapi *StrapiClassic) Create(path string, data hctypes.Dict) (int, hctypes.Dict, error) {
113 m_data := hctypes.Dict{}
114 if data != nil {
115 m_data = data.Copy()
116 }
117
118 return strapi.base("POST", strapi.urlParam(path, 0, nil), bytes.NewReader(([]byte)(hctypes.Dict{
119 "data": m_data,
120 }.Copy().ToJson())))
121 }
122
123
124
125
126
127
128
129
130
131
132
133 func (strapi *StrapiClassic) CreateBase(path string, data hctypes.Dict) (int, hctypes.Dict, error) {
134 m_data := hctypes.Dict{}
135 if data != nil {
136 m_data = data.Copy()
137 }
138
139 return strapi.base("POST", strapi.urlParam(path, 0, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson())))
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 func (strapi *StrapiClassic) Update(path string, id int64, data hctypes.Dict) (int, hctypes.Dict, error) {
156 m_data := hctypes.Dict{}
157 if data != nil {
158 m_data = data.Copy()
159 }
160
161 return strapi.base("PUT", strapi.urlParam(path, id, nil), bytes.NewReader(([]byte)(hctypes.Dict{
162 "data": m_data,
163 }.Copy().ToJson())))
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 func (strapi *StrapiClassic) UpdateBase(path string, id int64, data hctypes.Dict) (int, hctypes.Dict, error) {
178 m_data := hctypes.Dict{}
179 if data != nil {
180 m_data = data.Copy()
181 }
182
183 return strapi.base("PUT", strapi.urlParam(path, id, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson())))
184 }
185
186
187
188
189
190
191 func (strapi *StrapiClassic) Delete(path string, id int64) (int, hctypes.Dict, error) {
192 return strapi.base("DELETE", strapi.urlParam(path, id, nil), bytes.NewReader([]byte{}))
193 }
194
195
196
197
198
199
200
201
202
203
204 func (strapi *StrapiClassic) Login(identifier, password string) (int, hctypes.Dict, error) {
205 return strapi.basePublic("POST", fmt.Sprintf("%s%s", strapi.url, "api/auth/local"), bytes.NewReader(([]byte)(hctypes.Dict{
206 "identifier": identifier,
207 "password": password,
208 }.Copy().ToJson())))
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 func (strapi *StrapiClassic) UsersCreate(username string, email string, password string, confirmed bool, blocked bool, role int64) (int, hctypes.Dict, error) {
225 return strapi.base("POST", strapi.urlParam("users", 0, nil), bytes.NewReader(([]byte)(hctypes.Dict{
226 "username": username,
227 "email": email,
228 "password": password,
229 "confirmed": confirmed,
230 "blocked": blocked,
231 "role": role,
232 }.Copy().ToJson())))
233 }
234
235
236
237
238
239
240
241
242
243
244 func (strapi *StrapiClassic) UsersFindOne(id int64) (int, hctypes.Dict, error) {
245 return strapi.base("GET", strapi.urlParam("users", id, hctypes.Dict{}), bytes.NewReader([]byte{}))
246 }
247
248
249
250
251 func (strapi *StrapiClassic) UsersFind() (int, hctypes.List, error) {
252 return strapi.baseList("GET", strapi.urlParam("users", 0, hctypes.Dict{}), bytes.NewReader([]byte{}))
253 }
254
255
256
257
258
259
260
261 func (strapi *StrapiClassic) UsersCount() (int, int64, error) {
262 status, buf, err := strapi.baseBuffer("GET", strapi.urlParam("users/count", 0, hctypes.Dict{}), bytes.NewReader([]byte{}))
263
264 if err != nil {
265 return status, -1, err
266 }
267
268 if status != 200 {
269 return status, -1, err
270 }
271
272 count, err := strconv.Atoi(string(buf))
273 if err != nil {
274 return status, -1, err
275 }
276
277 return status, int64(count), nil
278 }
279
280
281
282
283
284 func (strapi *StrapiClassic) UsersMe() (int, hctypes.Dict, error) {
285 return strapi.base("GET", strapi.urlParam("users/me", 0, hctypes.Dict{}), bytes.NewReader([]byte{}))
286 }
287
288
289
290
291
292
293
294
295
296
297
298 func (strapi *StrapiClassic) UsersUpdate(id int64, data hctypes.Dict) (int, hctypes.Dict, error) {
299 m_data := hctypes.Dict{}
300 if data != nil {
301 m_data = data.Copy()
302 }
303
304 return strapi.base("PUT", strapi.urlParam("users", id, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson())))
305 }
306
307
308
309
310
311
312
313
314
315
316 func (strapi *StrapiClassic) UsersDelete(id int64) (int, hctypes.Dict, error) {
317 return strapi.base("DELETE", strapi.urlParam("users", id, nil), bytes.NewReader([]byte{}))
318 }
319
320 func (strapi *StrapiClassic) Upload(file_name string, buffer hctypes.Buffer) (int, hctypes.List, error) {
321 data := bytes.NewBuffer([]byte{})
322 mp := multipart.NewWriter(data)
323
324 file, err := mp.CreateFormFile("files", file_name)
325 if err != nil {
326 return 500, hctypes.List{
327 hctypes.Dict{
328 "error": hctypes.Dict{
329 "message": "Internal server error",
330 "status": 500,
331 },
332 },
333 }, err
334 }
335
336 file.Write(buffer)
337 mp.Close()
338
339 header_content_type := mp.FormDataContentType()
340
341 return strapi.baseCustomHeaderList("POST", strapi.urlParam("upload", 0, nil), hctypes.Dict{
342 "Content-Type": header_content_type,
343 }.Copy(), data)
344 }
345
View as plain text