Skip to content

Commit 79a8637

Browse files
author
Arthur Amstutz
committed
feat: Allow setting the user-agent in WASM binary + add a setter for the endpoint
Signed-off-by: Arthur Amstutz <[email protected]>
1 parent 2a15e40 commit 79a8637

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.github/workflows/golang-build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
go-version: ["1.20", "1.21", "1.22"]
11+
go-version: ["1.20", "1.21", "1.22", "1.23", "1.24"]
1212

1313
steps:
1414
- uses: actions/checkout@v3

ovh/ovh.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io/ioutil"
1212
"net/http"
13+
"runtime"
1314
"strconv"
1415
"strings"
1516
"sync/atomic"
@@ -162,6 +163,16 @@ func (c *Client) Endpoint() string {
162163
return c.endpoint
163164
}
164165

166+
func (c *Client) SetEndpoint(endpoint string) error {
167+
if strings.HasSuffix(endpoint, "/") {
168+
return errors.New("endpoint name cannot have a tailing slash")
169+
}
170+
171+
c.endpoint = endpoint
172+
173+
return nil
174+
}
175+
165176
//
166177
// High level helpers
167178
//
@@ -377,7 +388,13 @@ func (c *Client) NewRequest(method, path string, reqBody interface{}, needAuth b
377388
c.Client.Timeout = c.Timeout
378389

379390
if c.UserAgent != "" {
380-
req.Header.Set("User-Agent", "github.com/ovh/go-ovh ("+c.UserAgent+")")
391+
// When running in a WebAssembly binary, let the caller set
392+
// the user-agent freely to be able to use the browser's one.
393+
if runtime.GOARCH == "wasm" && runtime.GOOS == "js" {
394+
req.Header.Set("User-Agent", c.UserAgent)
395+
} else {
396+
req.Header.Set("User-Agent", "github.com/ovh/go-ovh ("+c.UserAgent+")")
397+
}
381398
} else {
382399
req.Header.Set("User-Agent", "github.com/ovh/go-ovh")
383400
}

ovh/ovh_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,18 @@ func TestOAuth2_ForReal(t *testing.T) {
685685
assert.Contains(out.Identities[0], "/oauth2-")
686686
assert.Contains(out.Identities[0], ":identity:credential:")
687687
}
688+
689+
func TestSetManualEndpointValid(t *testing.T) {
690+
client := &Client{}
691+
err := client.SetEndpoint("/fake-endpoint")
692+
693+
td.CmpNoError(t, err)
694+
td.CmpString(t, client.Endpoint(), "/fake-endpoint")
695+
}
696+
697+
func TestSetManualEndpointInvalid(t *testing.T) {
698+
client := &Client{}
699+
err := client.SetEndpoint("/invalid-endpoint/")
700+
701+
td.CmpString(t, err, "endpoint name cannot have a tailing slash")
702+
}

0 commit comments

Comments
 (0)