Skip to content
Merged
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
2 changes: 1 addition & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func New(server, token string, timeout time.Duration) (*API, error) {
server = strings.TrimRight(server, "/")
parsed, err := url.Parse(server)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return nil, clierr.Validation("invalid API server URL", "Use a full URL such as https://api.nexrender.com/api/v2.")
return nil, clierr.Validation("invalid API server URL", "Use the API address, such as https://api.nexrender.com.")
}
httpClient := &http.Client{Timeout: timeout}
editor := func(_ context.Context, req *http.Request) error {
Expand Down
16 changes: 15 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -108,6 +109,19 @@ func ValidateProfileName(name string) error {
return nil
}

// NormalizeServer trims a server URL and, when it is only the API's address
// (https://api.nexrender.com) or ends in /api, adds the /api/v2 base the client needs.
// Other paths are kept as given, for gateways and proxies that mount the API elsewhere.
func NormalizeServer(server string) string {
return strings.TrimRight(strings.TrimSpace(server), "/")
server = strings.TrimRight(strings.TrimSpace(server), "/")
parsed, err := url.Parse(server)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return server
}
switch parsed.Path {
case "", "/api":
parsed.Path = "/api/v2"
return parsed.String()
}
return server
}
18 changes: 18 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ func TestValidateProfileName(t *testing.T) {
}
}
}

func TestNormalizeServerDerivesTheAPIBase(t *testing.T) {
cases := map[string]string{
"https://api.nexrender.com": "https://api.nexrender.com/api/v2",
" https://staging.api.nexrender.com/ ": "https://staging.api.nexrender.com/api/v2",
"https://staging.api.nexrender.com/api": "https://staging.api.nexrender.com/api/v2",
"https://staging.api.nexrender.com/api/v2": "https://staging.api.nexrender.com/api/v2",
"http://127.0.0.1:3000": "http://127.0.0.1:3000/api/v2",
"https://gateway.example.test/nexrender/v2": "https://gateway.example.test/nexrender/v2",
"": "",
"not a url": "not a url",
}
for input, want := range cases {
if got := NormalizeServer(input); got != want {
t.Errorf("NormalizeServer(%q) = %q, want %q", input, got, want)
}
}
}
Loading