Skip to content

Commit

Permalink
solve double slash problem in concatenated url using path.Join
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjiangshu committed Sep 5, 2024
1 parent b2a7903 commit 34e82e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
14 changes: 4 additions & 10 deletions sda-admin/dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"path"

"github.com/neicnordic/sensitive-data-archive/sda-admin/helpers"
)
Expand All @@ -17,47 +18,40 @@ 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,
DatasetID: datasetID,
}

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
}

Expand Down
13 changes: 4 additions & 9 deletions sda-admin/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"path"

"github.com/neicnordic/sensitive-data-archive/sda-admin/helpers"
)
Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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,
Expand All @@ -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
}

Expand Down
10 changes: 3 additions & 7 deletions sda-admin/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"fmt"
"net/url"
"path"

"github.com/neicnordic/sensitive-data-archive/sda-admin/helpers"
)
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 34e82e5

Please sign in to comment.