Skip to content

Commit c4e1527

Browse files
authored
feat: implement storage api (nedpals#1)
1 parent a35db55 commit c4e1527

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

storage.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package supabase
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
// "errors"
8+
"fmt"
9+
"net/http"
10+
)
11+
12+
type Storage struct {
13+
client *Client
14+
// *bucket
15+
// *file
16+
}
17+
18+
type bucket struct {
19+
Name string `json:"name"`
20+
}
21+
type bucketResponse struct {
22+
Id string `json:"id"`
23+
Name string `json:"name"`
24+
Owner string `json:"owner"`
25+
Created_at string `json:"created_at"`
26+
Updated_at string `json:"updated_at"`
27+
}
28+
type bucketMessage struct {
29+
Message string `json:"message"`
30+
}
31+
// type file struct{}
32+
33+
type bucketOption struct {
34+
Id string `json:"id"`
35+
Name string `json:"name"`
36+
Public bool `json:"public"`
37+
}
38+
39+
40+
type storageError struct {
41+
Err string `json:"error"`
42+
Message string `json:"message"`
43+
}
44+
45+
// CreateBucket creates a new storage bucket
46+
// @param: option: a bucketOption with the name and id of the bucket you want to create
47+
// @returns: bucket: a response with the details of the bucket of the bucket created
48+
func (s *Storage) CreateBucket(ctx context.Context, option bucketOption) (*bucket, error) {
49+
reqBody, _ := json.Marshal(option)
50+
reqURL := fmt.Sprintf("%s/%s/bucket", s.client.BaseURL, StorageEndpoint)
51+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
req.Header.Set("Content-Type", "application/json")
57+
injectAuthorizationHeader(req, s.client.apiKey)
58+
res := bucket{}
59+
errRes := storageError{}
60+
if err := s.client.sendRequest(req, &res); err != nil {
61+
return nil, fmt.Errorf("%s\n%s", errRes.Err, errRes.Message)
62+
}
63+
64+
return &res, nil
65+
}
66+
67+
// GetBucket retrieves a bucket by its id
68+
// @param: id: the id of the bucket
69+
// @returns: bucketResponse: a response with the details of the bucket
70+
func (s *Storage) GetBucket(ctx context.Context, id string) (*bucketResponse, error) {
71+
// reqBody, _ := json.Marshal()
72+
reqURL := fmt.Sprintf("%s/%s/bucket/%s", s.client.BaseURL, StorageEndpoint, id)
73+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
req.Header.Set("Content-Type", "application/json")
79+
injectAuthorizationHeader(req, s.client.apiKey)
80+
res := bucketResponse{}
81+
errRes := storageError{}
82+
if err := s.client.sendRequest(req, &res); err != nil {
83+
return nil, fmt.Errorf("%s \n %s", errRes.Err, errRes.Message)
84+
}
85+
86+
return &res, nil
87+
}
88+
89+
// ListBucket retrieves all buckets ina supabase storage
90+
// @returns: []bucketResponse: a response with the details of all the bucket
91+
func (s *Storage) ListBuckets(ctx context.Context) (*[]bucketResponse, error) {
92+
// reqBody, _ := json.Marshal()
93+
reqURL := fmt.Sprintf("%s/%s/bucket/", s.client.BaseURL, StorageEndpoint)
94+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
req.Header.Set("Content-Type", "application/json")
100+
injectAuthorizationHeader(req, s.client.apiKey)
101+
res := []bucketResponse{}
102+
errRes := storageError{}
103+
if err := s.client.sendRequest(req, &res); err != nil {
104+
return nil, fmt.Errorf("%s \n %s", errRes.Err, errRes.Message)
105+
}
106+
107+
return &res, nil
108+
}
109+
110+
// EmptyBucket empties the object of a bucket by id
111+
// @param id: the id of the bucket
112+
// @returns bucketMessage: a successful response message or failed
113+
func (s *Storage) EmptyBucket(ctx context.Context, id string) (*bucketMessage, error) {
114+
// reqBody, _ := json.Marshal()
115+
reqURL := fmt.Sprintf("%s/%s/bucket/%s/empty", s.client.BaseURL, StorageEndpoint, id)
116+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, nil)
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
req.Header.Set("Content-Type", "application/json")
122+
injectAuthorizationHeader(req, s.client.apiKey)
123+
res := bucketMessage{}
124+
errRes := storageError{}
125+
if err := s.client.sendRequest(req, &res); err != nil {
126+
return nil, fmt.Errorf("%s \n %s", errRes.Err, errRes.Message)
127+
}
128+
129+
return &res, nil
130+
}
131+
132+
// UpdateBucket updates a bucket by its id
133+
// @param id: the id of the bucket
134+
// @param option: the options to be updated
135+
// @returns bucketMessage: a successful response message or failed
136+
func (s *Storage) UpdateBucket(ctx context.Context, id string, option bucketOption) (*bucketMessage, error) {
137+
reqBody, _ := json.Marshal(option)
138+
reqURL := fmt.Sprintf("%s/%s/bucket/%s", s.client.BaseURL, StorageEndpoint, id)
139+
req, err := http.NewRequestWithContext(ctx, http.MethodPut, reqURL, bytes.NewBuffer(reqBody))
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
req.Header.Set("Content-Type", "application/json")
145+
injectAuthorizationHeader(req, s.client.apiKey)
146+
res := bucketMessage{}
147+
errRes := storageError{}
148+
if err := s.client.sendRequest(req, &res); err != nil {
149+
return nil, fmt.Errorf("%s \n %s", errRes.Err, errRes.Message)
150+
}
151+
152+
return &res, nil
153+
}
154+
155+
// DeleteBucket deletes a bucket by its id, a bucket can't be deleted except emptied
156+
// @param id: the id of the bucket
157+
// @returns bucketMessage: a successful response message or failed
158+
func (s *Storage) DeleteBucket(ctx context.Context, id string) (*bucketResponse, error) {
159+
// reqBody, _ := json.Marshal()
160+
reqURL := fmt.Sprintf("%s/%s/bucket/%s", s.client.BaseURL, StorageEndpoint, id)
161+
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, reqURL, nil)
162+
if err != nil {
163+
return nil, err
164+
}
165+
166+
req.Header.Set("Content-Type", "application/json")
167+
injectAuthorizationHeader(req, s.client.apiKey)
168+
res := bucketResponse{}
169+
errRes := storageError{}
170+
if err := s.client.sendRequest(req, &res); err != nil {
171+
return nil, fmt.Errorf("%s\n%s", errRes.Err, errRes.Message)
172+
}
173+
174+
return &res, nil
175+
}
176+

supabase.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
const (
1414
AuthEndpoint = "auth/v1"
1515
RestEndpoint = "rest/v1"
16+
StorageEndpoint = "storage/v1"
1617
)
1718

1819
type Client struct {
@@ -21,6 +22,7 @@ type Client struct {
2122
apiKey string
2223
HTTPClient *http.Client
2324
Auth *Auth
25+
Storage *Storage
2426
DB *postgrest.Client
2527
}
2628

@@ -43,6 +45,7 @@ func CreateClient(baseURL string, supabaseKey string, debug ...bool) *Client {
4345
BaseURL: baseURL,
4446
apiKey: supabaseKey,
4547
Auth: &Auth{},
48+
Storage: &Storage{},
4649
HTTPClient: &http.Client{
4750
Timeout: time.Minute,
4851
},

0 commit comments

Comments
 (0)