Skip to content

Commit

Permalink
fix(webdav): handle api error and use url.Values encoding (#283)
Browse files Browse the repository at this point in the history
- fix(webdav): handle api errors in patch/create from s3
- fix(webdav): utilize url.Values for query encoding
  • Loading branch information
dsonck92 authored Aug 17, 2024
1 parent 71c4f25 commit fbd2208
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions webdav/client/api_client/file_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"strings"

"github.com/kouprlabs/voltaserve/webdav/config"
Expand Down Expand Up @@ -145,20 +146,21 @@ func (cl *FileClient) CreateFromS3(opts FileCreateFromS3Options) (*File, error)
if err != nil {
return nil, err
}
args := url.Values{
"api_key": []string{cl.config.Security.APIKey},
"access_token": []string{cl.token.AccessToken},
"workspace_id": []string{opts.WorkspaceID},
"parent_id": []string{opts.ParentID},
"name": []string{opts.Name},
"s3_key": []string{opts.S3Reference.Key},
"s3_bucket": []string{opts.S3Reference.Bucket},
"snapshot_id": []string{opts.S3Reference.SnapshotID},
"content_type": []string{opts.S3Reference.ContentType},
"size": []string{strconv.FormatInt(opts.S3Reference.Size, 10)},
}
reqUrl := cl.config.APIURL + "/v2/files/create_from_s3?" + args.Encode()
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/v2/files/create_from_s3?api_key=%s&access_token=%s&workspace_id=%s&parent_id=%s&name=%s&s3_key=%s&s3_bucket=%s&snapshot_id=%s&content_type=%s&size=%d",
cl.config.APIURL,
cl.config.Security.APIKey,
cl.token.AccessToken,
opts.WorkspaceID,
opts.ParentID,
opts.Name,
opts.S3Reference.Key,
opts.S3Reference.Bucket,
opts.S3Reference.SnapshotID,
opts.S3Reference.ContentType,
opts.S3Reference.Size,
),
reqUrl,
bytes.NewBuffer(body))
if err != nil {
return nil, err
Expand All @@ -176,6 +178,12 @@ func (cl *FileClient) CreateFromS3(opts FileCreateFromS3Options) (*File, error)
return
}
}(res.Body)

body, err = cl.jsonResponseOrThrow(res)
if err != nil {
return nil, err
}

var file File
if err = json.Unmarshal(body, &file); err != nil {
return nil, err
Expand All @@ -194,19 +202,19 @@ func (cl *FileClient) PatchFromS3(opts FilePatchFromS3Options) (*File, error) {
if err != nil {
return nil, err
}
args := url.Values{
"api_key": []string{cl.config.Security.APIKey},
"access_token": []string{cl.token.AccessToken},
"name": []string{opts.Name},
"s3_key": []string{opts.S3Reference.Key},
"s3_bucket": []string{opts.S3Reference.Bucket},
"snapshot_id": []string{opts.S3Reference.SnapshotID},
"content_type": []string{opts.S3Reference.ContentType},
"size": []string{strconv.FormatInt(opts.S3Reference.Size, 10)},
}
reqUrl := cl.config.APIURL + "/v2/files/" + opts.ID + "/patch_from_s3?" + args.Encode()
req, err := http.NewRequest("PATCH",
fmt.Sprintf("%s/v2/files/%s/patch_from_s3?api_key=%s&access_token=%s&name=%s&s3_key=%s&s3_bucket=%s&snapshot_id=%s&content_type=%s&size=%d",
cl.config.APIURL,
opts.ID,
cl.config.Security.APIKey,
cl.token.AccessToken,
opts.Name,
opts.S3Reference.Key,
opts.S3Reference.Bucket,
opts.S3Reference.SnapshotID,
opts.S3Reference.ContentType,
opts.S3Reference.Size,
),
reqUrl,
bytes.NewBuffer(body))
if err != nil {
return nil, err
Expand All @@ -223,6 +231,12 @@ func (cl *FileClient) PatchFromS3(opts FilePatchFromS3Options) (*File, error) {
return
}
}(res.Body)

body, err = cl.jsonResponseOrThrow(res)
if err != nil {
return nil, err
}

var file File
if err = json.Unmarshal(body, &file); err != nil {
return nil, err
Expand Down

0 comments on commit fbd2208

Please sign in to comment.