From 34e82e5b1d2f36f38563e554741c631c0896a33f Mon Sep 17 00:00:00 2001 From: Nanjiang Shu Date: Thu, 5 Sep 2024 13:38:45 +0000 Subject: [PATCH] solve double slash problem in concatenated url using path.Join --- sda-admin/dataset/dataset.go | 14 ++++---------- sda-admin/file/file.go | 13 ++++--------- sda-admin/list/list.go | 10 +++------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/sda-admin/dataset/dataset.go b/sda-admin/dataset/dataset.go index af70c97ec..acdf291b9 100644 --- a/sda-admin/dataset/dataset.go +++ b/sda-admin/dataset/dataset.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/url" + "path" "github.com/neicnordic/sensitive-data-archive/sda-admin/helpers" ) @@ -17,10 +18,9 @@ type RequestBodyDataset struct { func Create(apiURI, token, datasetID string, accessionIDs []string) error { parsedURL, err := url.Parse(apiURI) if err != nil { - return err } - parsedURL.Path = fmt.Sprintf("%s/dataset/create", parsedURL.Path) + parsedURL.Path = path.Join(parsedURL.Path, "dataset/create") requestBody := RequestBodyDataset{ AccessionIDs: accessionIDs, @@ -28,36 +28,30 @@ func Create(apiURI, token, datasetID string, accessionIDs []string) error { } jsonBody, err := json.Marshal(requestBody) - if err != nil { - return fmt.Errorf("failed to marshal JSON, reason: %v", err) } _, err = helpers.PostRequest(parsedURL.String(), token, jsonBody) - if err != nil { - return err } return nil } -// DatasetRelease releases a dataset for downloading +// Release releases a dataset for downloading func Release(apiURI, token, datasetID string) error { parsedURL, err := url.Parse(apiURI) if err != nil { - return err } - parsedURL.Path = fmt.Sprintf("%s/dataset/release/%s", parsedURL.Path, datasetID) + parsedURL.Path = path.Join(parsedURL.Path, "dataset/release", datasetID) jsonBody := []byte("{}") _, err = helpers.PostRequest(parsedURL.String(), token, jsonBody) if err != nil { - return err } diff --git a/sda-admin/file/file.go b/sda-admin/file/file.go index 367dd5830..7d7f06da9 100644 --- a/sda-admin/file/file.go +++ b/sda-admin/file/file.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/url" + "path" "github.com/neicnordic/sensitive-data-archive/sda-admin/helpers" ) @@ -23,10 +24,9 @@ type RequestBodyFileAccession struct { func Ingest(apiURI, token, username, filepath string) error { parsedURL, err := url.Parse(apiURI) if err != nil { - return err } - parsedURL.Path = fmt.Sprintf("%s/file/ingest", parsedURL.Path) + parsedURL.Path = path.Join(parsedURL.Path, "file/ingest") requestBody := RequestBodyFileIngest{ Filepath: filepath, @@ -35,12 +35,11 @@ func Ingest(apiURI, token, username, filepath string) error { jsonBody, err := json.Marshal(requestBody) if err != nil { - return fmt.Errorf("failed to marshal JSON, reason: %v", err) } + _, err = helpers.PostRequest(parsedURL.String(), token, jsonBody) if err != nil { - return err } @@ -51,10 +50,9 @@ func Ingest(apiURI, token, username, filepath string) error { func Accession(apiURI, token, username, filepath, accessionID string) error { parsedURL, err := url.Parse(apiURI) if err != nil { - return err } - parsedURL.Path = fmt.Sprintf("%s/file/accession", parsedURL.Path) + parsedURL.Path = path.Join(parsedURL.Path, "file/accession") requestBody := RequestBodyFileAccession{ AccessionID: accessionID, @@ -64,14 +62,11 @@ func Accession(apiURI, token, username, filepath, accessionID string) error { jsonBody, err := json.Marshal(requestBody) if err != nil { - return fmt.Errorf("failed to marshal JSON, reason: %v", err) } _, err = helpers.PostRequest(parsedURL.String(), token, jsonBody) - if err != nil { - return err } diff --git a/sda-admin/list/list.go b/sda-admin/list/list.go index 35487da47..20e3c665d 100644 --- a/sda-admin/list/list.go +++ b/sda-admin/list/list.go @@ -3,6 +3,7 @@ package list import ( "fmt" "net/url" + "path" "github.com/neicnordic/sensitive-data-archive/sda-admin/helpers" ) @@ -11,15 +12,12 @@ import ( func Users(apiURI, token string) error { parsedURL, err := url.Parse(apiURI) if err != nil { - return err } - parsedURL.Path = fmt.Sprintf("%s/users", parsedURL.Path) - + parsedURL.Path = path.Join(parsedURL.Path, "users") response, err := helpers.GetResponseBody(parsedURL.String(), token) if err != nil { - return err } @@ -32,14 +30,12 @@ func Users(apiURI, token string) error { func Files(apiURI, token, username string) error { parsedURL, err := url.Parse(apiURI) if err != nil { - return err } - parsedURL.Path = fmt.Sprintf("%s/users/%s/files", parsedURL.Path, username) + parsedURL.Path = path.Join(parsedURL.Path, "users", username, "files") response, err := helpers.GetResponseBody(parsedURL.String(), token) if err != nil { - return err }