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

Fix FilesService.Upload #5

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 9 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const (
)

const (
defaultUserAgent = "go-putio"
defaultMediaType = "application/json"
defaultBaseURL = "https://api.put.io"
defaultUploadURL = "https://upload.put.io"
defaultTusURL = "https://upload.put.io/files/"
defaultUserAgent = "go-putio"
defaultMediaType = "application/json"
defaultBaseURL = "https://api.put.io"
defaultUploadHost = "upload.put.io"
defaultTusURL = "https://upload.put.io/files/"
)

// Client manages communication with Put.io v2 API.
Expand Down Expand Up @@ -109,17 +109,16 @@ func (c *Client) NewRequest(ctx context.Context, method, relURL string, body io.
return nil, fmt.Errorf("%w", err)
}

u := c.BaseURL.ResolveReference(rel)

// Workaround for upload endpoints. Upload server is different than API server.
var u *url.URL
switch {
case relURL == "$upload$":
u, _ = url.Parse(defaultUploadURL)
case relURL == "/v2/files/upload":
u.Host = defaultUploadHost
case relURL == "$upload-tus$":
u, _ = url.Parse(defaultTusURL)
case strings.HasPrefix(relURL, "http://") || strings.HasPrefix(relURL, "https://"):
u = rel
default:
u = c.BaseURL.ResolveReference(rel)
}

req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
Expand Down
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ func TestNewRequest_customUserAgent(t *testing.T) {
t.Errorf("got: %v, want: %v", got, userAgent)
}
}

func TestNewRequest_resolveAbsoluteUrl(t *testing.T) {
cl := NewClient(nil)
fullUrl := "https://api.put.io/v2/files/list/continue"

req, _ := cl.NewRequest(context.Background(), http.MethodGet, "/v2/files/list/continue", nil)
if got := req.URL.String(); got != fullUrl {
t.Errorf("got: %v, want: %v", got, fullUrl)
}
}

func TestNewRequest_overrideBaseUrl(t *testing.T) {
cl := NewClient(nil)
fullUrl := "https://upload.put.io/v2/files/upload"

req, _ := cl.NewRequest(context.Background(), http.MethodGet, "/v2/files/upload", nil)
if got := req.URL.String(); got != fullUrl {
t.Errorf("got: %v, want: %v", got, fullUrl)
}
}

func TestNewRequest_overrideWithAbsoluteUrl(t *testing.T) {
cl := NewClient(nil)
fullUrl := "https://my-custom-url.com/endpoint?query=param"

req, _ := cl.NewRequest(context.Background(), http.MethodGet, "https://my-custom-url.com/endpoint?query=param", nil)
if got := req.URL.String(); got != fullUrl {
t.Errorf("got: %v, want: %v", got, fullUrl)
}
}
2 changes: 1 addition & 1 deletion files.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (f *FilesService) Upload(ctx context.Context, r io.Reader, filename string,
return Upload{}, fmt.Errorf("%w", err)
}

req, err := f.client.NewRequest(ctx, http.MethodPost, "$upload$", &buf)
req, err := f.client.NewRequest(ctx, http.MethodPost, "/v2/files/upload", &buf)
if err != nil {
return Upload{}, fmt.Errorf("%w", err)
}
Expand Down
Loading