From 5a3c8ed6e7ae891db1bca23420d42f8fe1973e26 Mon Sep 17 00:00:00 2001 From: Benjamin Ritter Date: Mon, 22 Jan 2024 19:28:29 +0100 Subject: [PATCH 1/2] feat: Add Describe function --- README.md | 6 +++--- client.go | 20 ++++++++++++++++++++ client_test.go | 23 +++++++++++++++++------ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f30cb99..b747cdc 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ This is a new Library for using the Bunny.net Object Storage Service in Go. It h I wrote this because of a open PR implementing Bunny.net as Object Storage in JuiceFS and the maintainers were a bit hesitant merging code with many homebrew dependencies. This is using just three widespread dependencies (resty, uuid, logrus) and is about 100 lines of library code, excluding tests. -The E2E test coverage is currently at about 70% - -This library features a simple API and allows you to focus on what`s important +- The E2E test coverage is currently at about 60% +- Simple to use +- undocumented `Describe` funtion which allows to retrieve metadata for single, non-directory files. ## 🦾 Getting Started diff --git a/client.go b/client.go index 74763af..fd3522c 100644 --- a/client.go +++ b/client.go @@ -127,3 +127,23 @@ func (c *Client) List(path string) ([]Object, error) { } return objectList, nil } + +// Describes an Object. EXPERIMENTAL. The official Java SDK uses it, but the DESCRIBE HTTP method used is not officially documented. +func (c *Client) Describe(path string) (Object, error) { + object := Object{} + + resp, err := c.R(). + SetHeader("Accept", "application/json"). + SetResult(object). + Execute("DESCRIBE", path) + c.logger.Debugf("Describe Request Response: %v", resp) + + if err != nil { + c.logger.Errorf("Describe Request Failed: %v", err) + return object, err + } + if resp.IsError() { + return object, errors.New(resp.Status()) + } + return object, nil +} diff --git a/client_test.go b/client_test.go index e5826eb..312c461 100644 --- a/client_test.go +++ b/client_test.go @@ -39,10 +39,21 @@ func TestMain(m *testing.M) { m.Run() } +func DescribeFile(t *testing.T) { + _, name := UploadRandomFile1MB(t) + obj, err := bunnyclient.Describe(name) + if err != nil { + t.Error(err) + } + empty_object := bunnystorage.Object{} + if obj == empty_object { + t.Errorf("Returned empty DESCRIBE API response") + } +} + func DeleteFileWithPathSetToTrue(t *testing.T) { t.Cleanup(func() { DeleteTestPath(t) }) - _ = UploadRandomFile1MB(t) - testpath := path.Join(testingDirectory, t.Name()) + _, testpath := UploadRandomFile1MB(t) err := bunnyclient.Delete(testpath, true) if err != nil { @@ -73,7 +84,7 @@ func TestDeleteNonexistentFile(t *testing.T) { func TestDownloadAfterUpload1M(t *testing.T) { t.Cleanup(func() { DeleteTestPath(t) }) - input := UploadRandomFile1MB(t) // 1MB file size + input, _ := UploadRandomFile1MB(t) // 1MB file size output := DownloadFile(t) list := ListFilesInTestDir(t) if len(list) != 1 { @@ -87,7 +98,7 @@ func TestDownloadAfterUpload1M(t *testing.T) { func TestListAfterUploadWithExtraTrailingSlash(t *testing.T) { t.Cleanup(func() { DeleteTestPath(t) }) - _ = UploadRandomFile1MB(t) // 1MB file size + _, _ = UploadRandomFile1MB(t) // 1MB file size list, err := bunnyclient.List(testingDirectory + "/") if err != nil { t.Error(err) @@ -126,7 +137,7 @@ func DownloadFile(t *testing.T) []byte { return body } -func UploadRandomFile1MB(t *testing.T) []byte { +func UploadRandomFile1MB(t *testing.T) ([]byte, string) { t.Helper() testpath := path.Join(testingDirectory, t.Name()) testcontent := make([]byte, 1048576) @@ -140,7 +151,7 @@ func UploadRandomFile1MB(t *testing.T) []byte { if err != nil { t.Error(err) } - return testcontent + return testcontent, testpath } func DeleteFile(t *testing.T) { From ace413e535fd21e1c95973203efe2743ae154147 Mon Sep 17 00:00:00 2001 From: Benjamin Ritter Date: Mon, 22 Jan 2024 19:33:02 +0100 Subject: [PATCH 2/2] fix: enable non-working test --- README.md | 4 ++-- client_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b747cdc..6daa767 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ This is a new Library for using the Bunny.net Object Storage Service in Go. It h I wrote this because of a open PR implementing Bunny.net as Object Storage in JuiceFS and the maintainers were a bit hesitant merging code with many homebrew dependencies. This is using just three widespread dependencies (resty, uuid, logrus) and is about 100 lines of library code, excluding tests. -- The E2E test coverage is currently at about 60% +- The E2E test coverage is currently at about 70% - Simple to use -- undocumented `Describe` funtion which allows to retrieve metadata for single, non-directory files. +- Implements Undocumented `Describe` funtion which allows to retrieve metadata for single, non-directory files. ## 🦾 Getting Started diff --git a/client_test.go b/client_test.go index 312c461..3f59f00 100644 --- a/client_test.go +++ b/client_test.go @@ -39,7 +39,7 @@ func TestMain(m *testing.M) { m.Run() } -func DescribeFile(t *testing.T) { +func TestDescribeFile(t *testing.T) { _, name := UploadRandomFile1MB(t) obj, err := bunnyclient.Describe(name) if err != nil {