Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetAuthToken to Client #30

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ import (
)

func main() {
storageClient := storage_go.NewClient("https://<project-reference-id>.supabase.co/storage/v1", "<project-secret-api-key>", nil)
storageClient := storage_go.NewClient("https://<project-reference-id>.supabase.co/storage/v1", nil)
}
```
### Set Auth Token for your client
```go
storageClient.SetAuthToken("<auth-token>")
```

### Handling resources

Expand Down Expand Up @@ -134,7 +138,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:
Expand Down
13 changes: 9 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var version = "v0.7.0"
type Client struct {
clientError error
session http.Client
clientTransport transport
clientTransport *transport
}

type transport struct {
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -63,6 +62,12 @@ func NewClient(rawUrl string, token string, headers map[string]string) *Client {
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) {
Expand Down
24 changes: 13 additions & 11 deletions test/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (

var (
rawUrl = "https://abc.supabase.co/storage/v1"
token = ""
apiKey = ""
)

func TestUpload(t *testing.T) {
file, err := os.Open("dummy.txt")
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)

Expand All @@ -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,
Expand All @@ -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.SetAuthToken(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.SetAuthToken(apiKey)
file, err := os.Open("dummy.txt")
if err != nil {
panic(err)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions test/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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"))
}