forked from r--w/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 14
/
files.go
45 lines (37 loc) · 997 Bytes
/
files.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
package pocketbase
import (
"encoding/json"
"fmt"
)
type (
Files struct {
*Client
}
ResponseGetToken struct {
Token string `json:"token"`
}
)
// GetToken requests a new private file access token for the current auth model (admin or record).
func (f Files) GetToken() (string, error) {
if err := f.Authorize(); err != nil {
return "", err
}
request := f.client.R().
SetHeader("Content-Type", "application/json")
resp, err := request.Post(f.url + "/api/files/token")
if err != nil {
return "", fmt.Errorf("[files] can't send token request to pocketbase, err %w", err)
}
if resp.IsError() {
return "", fmt.Errorf("[files] pocketbase returned status at getting a new token: %d, msg: %s, err %w",
resp.StatusCode(),
resp.String(),
ErrInvalidResponse,
)
}
response := ResponseGetToken{}
if err := json.Unmarshal(resp.Body(), &response); err != nil {
return "", fmt.Errorf("[files] can't unmarshal response, err %w", err)
}
return response.Token, nil
}