-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
supabase.go
113 lines (98 loc) · 2.5 KB
/
supabase.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
package supabase
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
postgrest "github.com/nedpals/postgrest-go/pkg"
)
const (
AuthEndpoint = "auth/v1"
AdminEndpoint = "auth/v1/admin"
RestEndpoint = "rest/v1"
StorageEndpoint = "storage/v1"
)
type Client struct {
BaseURL string
// apiKey can be a client API key or a service key
apiKey string
HTTPClient *http.Client
Admin *Admin
Auth *Auth
Storage *Storage
DB *postgrest.Client
}
type ErrorResponse struct {
Code int `json:"code"`
Message string `json:"msg"`
}
func (err *ErrorResponse) Error() string {
return err.Message
}
// CreateClient creates a new Supabase client
func CreateClient(baseURL string, supabaseKey string, debug ...bool) *Client {
parsedURL, err := url.Parse(fmt.Sprintf("%s/%s/", baseURL, RestEndpoint))
if err != nil {
panic(err)
}
client := &Client{
BaseURL: baseURL,
apiKey: supabaseKey,
Admin: &Admin{},
Auth: &Auth{},
Storage: &Storage{},
HTTPClient: &http.Client{
Timeout: time.Minute,
},
DB: postgrest.NewClient(
*parsedURL,
postgrest.WithTokenAuth(supabaseKey),
func(c *postgrest.Client) {
// debug parameter is only for postgrest-go for now
if len(debug) > 0 {
c.Debug = debug[0]
}
c.AddHeader("apikey", supabaseKey)
},
),
}
client.Admin.client = client
client.Admin.serviceKey = supabaseKey
client.Auth.client = client
client.Storage.client = client
return client
}
func injectAuthorizationHeader(req *http.Request, value string) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", value))
}
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
var errRes ErrorResponse
hasCustomError, err := c.sendCustomRequest(req, v, &errRes)
if err != nil {
return err
} else if hasCustomError {
return &errRes
}
return nil
}
func (c *Client) sendCustomRequest(req *http.Request, successValue interface{}, errorValue interface{}) (bool, error) {
req.Header.Set("apikey", c.apiKey)
res, err := c.HTTPClient.Do(req)
if err != nil {
return true, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= http.StatusOK && res.StatusCode < 300
if !statusOK {
if err = json.NewDecoder(res.Body).Decode(&errorValue); err == nil {
return true, nil
}
return false, fmt.Errorf("unknown, status code: %d", res.StatusCode)
} else if res.StatusCode != http.StatusNoContent {
if err = json.NewDecoder(res.Body).Decode(&successValue); err != nil {
return false, err
}
}
return false, nil
}