From 2201a8dc034b7e7cba7614e8c355c6857aa4b300 Mon Sep 17 00:00:00 2001 From: Gustavo Guzman Date: Thu, 25 Apr 2024 15:12:06 +0200 Subject: [PATCH 1/3] feat: add set api key and auth token --- README.md | 14 ++++++++++++-- client.go | 19 +++++++++++++++---- test/fileupload_test.go | 24 +++++++++++++----------- test/storage_test.go | 12 ++++++------ 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fcb5341..59914e4 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,20 @@ import ( ) func main() { - storageClient := storage_go.NewClient("https://.supabase.co/storage/v1", "", nil) + storageClient := storage_go.NewClient("https://.supabase.co/storage/v1", nil) } ``` +### Set Api Key for your client +```go + storageClient.SetApiKey("") +``` + +### Set Auth Token for your client +```go + storageClient.SetAuthToken("") +``` + ### Handling resources #### Handling Storage Buckets @@ -134,7 +144,7 @@ func main() { - Retrieve URLs for assets in public buckets: ```go - result, err := storageClient.GetPublicUrl("test", "book.pdf") + result := storageClient.GetPublicUrl("test", "book.pdf") ``` - Create an signed URL and upload to signed URL: diff --git a/client.go b/client.go index bbd5537..c9f26b2 100644 --- a/client.go +++ b/client.go @@ -13,7 +13,7 @@ var version = "v0.7.0" type Client struct { clientError error session http.Client - clientTransport transport + clientTransport *transport } type transport struct { @@ -31,7 +31,7 @@ func (t transport) RoundTrip(request *http.Request) (*http.Response, error) { return http.DefaultTransport.RoundTrip(request) } -func NewClient(rawUrl string, token string, headers map[string]string) *Client { +func NewClient(rawUrl string, headers map[string]string) *Client { baseURL, err := url.Parse(rawUrl) if err != nil { return &Client{ @@ -46,14 +46,13 @@ func NewClient(rawUrl string, token string, headers map[string]string) *Client { c := Client{ session: http.Client{Transport: t}, - clientTransport: t, + clientTransport: &t, } // Set required headers c.clientTransport.header.Set("Accept", "application/json") c.clientTransport.header.Set("Content-Type", "application/json") c.clientTransport.header.Set("X-Client-Info", "storage-go/"+version) - c.clientTransport.header.Set("Authorization", "Bearer "+token) // Optional headers [if exists] for key, value := range headers { @@ -63,6 +62,18 @@ func NewClient(rawUrl string, token string, headers map[string]string) *Client { return &c } +// Sets api key header for subsequent requests +func (c *Client) SetApiKey(apiKey string) *Client { + c.clientTransport.header.Set("apiKey", apiKey) + return c +} + +// Sets authorization header for subsequent requests +func (c *Client) SetAuthToken(authToken string) *Client { + c.clientTransport.header.Set("Authorization", "Bearer "+authToken) + return c +} + // NewRequest will create new request with method, url and body // If body is not nil, it will be marshalled into json func (c *Client) NewRequest(method, url string, body ...interface{}) (*http.Request, error) { diff --git a/test/fileupload_test.go b/test/fileupload_test.go index c8bd272..680392b 100644 --- a/test/fileupload_test.go +++ b/test/fileupload_test.go @@ -10,7 +10,7 @@ import ( var ( rawUrl = "https://abc.supabase.co/storage/v1" - token = "" + apiKey = "" ) func TestUpload(t *testing.T) { @@ -18,7 +18,7 @@ func TestUpload(t *testing.T) { if err != nil { panic(err) } - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.UploadFile("test", "test.txt", file) fmt.Println(resp, err) @@ -31,42 +31,42 @@ func TestUpdate(t *testing.T) { if err != nil { panic(err) } - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.UpdateFile("test", "test.txt", file) fmt.Println(resp, err) } func TestMoveFile(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.MoveFile("test", "test.txt", "random/test.txt") fmt.Println(resp, err) } func TestSignedUrl(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.CreateSignedUrl("test", "file_example_MP4_480_1_5MG.mp4", 120) fmt.Println(resp, err) } func TestPublicUrl(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp := c.GetPublicUrl("shield", "book.pdf") fmt.Println(resp) } func TestDeleteFile(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.RemoveFile("shield", []string{"book.pdf"}) fmt.Println(resp, err) } func TestListFile(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.ListFiles("shield", "", storage_go.FileSearchOptions{ Limit: 10, Offset: 0, @@ -80,14 +80,16 @@ func TestListFile(t *testing.T) { } func TestCreateUploadSignedUrl(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{"apiKey": token}) + c := storage_go.NewClient(rawUrl, map[string]string{}) + c.SetApiKey(apiKey) resp, err := c.CreateSignedUploadUrl("your-bucket-id", "book.pdf") fmt.Println(resp, err) } func TestUploadToSignedUrl(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{"apiKey": token}) + c := storage_go.NewClient(rawUrl, map[string]string{}) + c.SetApiKey(apiKey) file, err := os.Open("dummy.txt") if err != nil { panic(err) @@ -98,7 +100,7 @@ func TestUploadToSignedUrl(t *testing.T) { } func TestDownloadFile(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.DownloadFile("test", "book.pdf") if err != nil { t.Fatalf("DownloadFile failed: %v", err) diff --git a/test/storage_test.go b/test/storage_test.go index fe58ea1..bd316ea 100644 --- a/test/storage_test.go +++ b/test/storage_test.go @@ -8,25 +8,25 @@ import ( ) func TestBucketListAll(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) resp, err := c.ListBuckets() fmt.Println(resp, err) } func TestBucketFetchById(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) fmt.Println(c.GetBucket("test")) } func TestBucketCreate(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) fmt.Println(c.CreateBucket("test", storage_go.BucketOptions{ Public: true, })) } func TestBucketUpdate(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) _, _ = c.UpdateBucket("test", storage_go.BucketOptions{ Public: false, }) @@ -39,11 +39,11 @@ func TestBucketUpdate(t *testing.T) { } func TestEmptyBucket(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) fmt.Println(c.EmptyBucket("test")) } func TestDeleteBucket(t *testing.T) { - c := storage_go.NewClient(rawUrl, token, map[string]string{}) + c := storage_go.NewClient(rawUrl, map[string]string{}) fmt.Println(c.DeleteBucket("test")) } From 0eea2a479e3072040553845ddb640d40f04acbfb Mon Sep 17 00:00:00 2001 From: Gustavo Guzman Date: Thu, 25 Apr 2024 18:32:36 +0200 Subject: [PATCH 2/3] fix: remove setApiKey method from client --- client.go | 6 ------ test/fileupload_test.go | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index c9f26b2..8729555 100644 --- a/client.go +++ b/client.go @@ -62,12 +62,6 @@ func NewClient(rawUrl string, headers map[string]string) *Client { return &c } -// Sets api key header for subsequent requests -func (c *Client) SetApiKey(apiKey string) *Client { - c.clientTransport.header.Set("apiKey", apiKey) - return c -} - // Sets authorization header for subsequent requests func (c *Client) SetAuthToken(authToken string) *Client { c.clientTransport.header.Set("Authorization", "Bearer "+authToken) diff --git a/test/fileupload_test.go b/test/fileupload_test.go index 680392b..afd2d54 100644 --- a/test/fileupload_test.go +++ b/test/fileupload_test.go @@ -81,7 +81,7 @@ func TestListFile(t *testing.T) { func TestCreateUploadSignedUrl(t *testing.T) { c := storage_go.NewClient(rawUrl, map[string]string{}) - c.SetApiKey(apiKey) + c.SetAuthToken(apiKey) resp, err := c.CreateSignedUploadUrl("your-bucket-id", "book.pdf") fmt.Println(resp, err) @@ -89,7 +89,7 @@ func TestCreateUploadSignedUrl(t *testing.T) { func TestUploadToSignedUrl(t *testing.T) { c := storage_go.NewClient(rawUrl, map[string]string{}) - c.SetApiKey(apiKey) + c.SetAuthToken(apiKey) file, err := os.Open("dummy.txt") if err != nil { panic(err) From 4f49f9d14178a3162f142037075e6541c18f06ba Mon Sep 17 00:00:00 2001 From: Gustavo Guzman Date: Thu, 25 Apr 2024 18:42:20 +0200 Subject: [PATCH 3/3] fix: remove unnecessary docs --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 59914e4..45ecc10 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,6 @@ func main() { storageClient := storage_go.NewClient("https://.supabase.co/storage/v1", nil) } ``` - -### Set Api Key for your client -```go - storageClient.SetApiKey("") -``` - ### Set Auth Token for your client ```go storageClient.SetAuthToken("")