Skip to content

Commit 17d64cf

Browse files
committed
feat: simplified auth
1 parent 1b32fd5 commit 17d64cf

File tree

5 files changed

+32
-100
lines changed

5 files changed

+32
-100
lines changed

auth.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

gen/gen_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import (
8888
"io"
8989
"net/http"
9090
"net/http/httptest"
91+
"net/url"
9192
"os"
9293
"path/filepath"
9394
"testing"
@@ -128,11 +129,8 @@ func setupFakeAPI(t *testing.T, dir, operation string) *Client {
128129
}
129130
})
130131
131-
transport, err := NewTokenTransport("1234")
132-
require.NoError(t, err)
133-
134-
client := NewClient(transport.Client())
135-
client.Endpoint = server.URL
132+
client := NewClient("1234")
133+
client.Endpoint, _ = url.Parse(server.URL)
136134
137135
return client
138136
}

namesilo.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package namesilo
44
import (
55
"context"
66
"errors"
7-
"fmt"
87
"net/http"
98
"net/url"
9+
"time"
1010

1111
querystring "github.com/google/go-querystring/query"
1212
)
@@ -31,59 +31,63 @@ const (
3131

3232
// Client the Namesilo client.
3333
type Client struct {
34-
Endpoint string
34+
apiKey string
35+
36+
Endpoint *url.URL
3537
HTTPClient *http.Client
3638
}
3739

3840
// NewClient Creates a Namesilo client.
39-
func NewClient(httpClient *http.Client) *Client {
40-
if httpClient == nil {
41-
httpClient = http.DefaultClient
42-
}
41+
func NewClient(apiKey string) *Client {
42+
endpoint, _ := url.Parse(DefaultAPIEndpoint)
4343

4444
return &Client{
45-
Endpoint: DefaultAPIEndpoint,
46-
HTTPClient: httpClient,
45+
apiKey: apiKey,
46+
Endpoint: endpoint,
47+
HTTPClient: &http.Client{Timeout: 10 * time.Second},
4748
}
4849
}
4950

5051
func (c *Client) get(ctx context.Context, name string, params any) (*http.Response, error) {
51-
uri, err := url.Parse(fmt.Sprintf("%s/%s", c.Endpoint, name))
52-
if err != nil {
53-
return nil, err
54-
}
52+
endpoint := c.Endpoint.JoinPath(name)
5553

5654
if params != nil {
5755
var v url.Values
5856

59-
v, err = querystring.Values(params)
57+
v, err := querystring.Values(params)
6058
if err != nil {
6159
return nil, err
6260
}
6361

64-
uri.RawQuery = v.Encode()
62+
endpoint.RawQuery = v.Encode()
6563
}
6664

67-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), http.NoBody)
65+
query := endpoint.Query()
66+
query.Set("version", "1")
67+
query.Set("type", "xml")
68+
query.Set("key", c.apiKey)
69+
endpoint.RawQuery = query.Encode()
70+
71+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
6872
if err != nil {
6973
return nil, err
7074
}
7175

7276
return c.HTTPClient.Do(req)
7377
}
7478

75-
func GetEndpoint(prod, ote bool) (string, error) {
79+
func GetEndpoint(prod, ote bool) (*url.URL, error) {
7680
if prod && ote {
77-
return "", errors.New("prod and ote are mutually exclusive")
81+
return nil, errors.New("prod and ote are mutually exclusive")
7882
}
7983

8084
if prod {
81-
return DefaultAPIEndpoint, nil
85+
return url.Parse(DefaultAPIEndpoint)
8286
}
8387

8488
if ote {
85-
return OTEAPIEndpoint, nil
89+
return url.Parse(OTEAPIEndpoint)
8690
}
8791

88-
return SandboxAPIEndpoint, nil
92+
return url.Parse(SandboxAPIEndpoint)
8993
}

readme.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ import (
2222
)
2323

2424
func main() {
25-
transport, err := namesilo.NewTokenTransport("1234")
26-
if err != nil {
27-
log.Fatal(err)
28-
}
29-
30-
client := namesilo.NewClient(transport.Client())
25+
client := namesilo.NewClient("1234")
3126

3227
params := &namesilo.AddAccountFundsParams{
3328
Amount: "1000000",
@@ -55,12 +50,7 @@ import (
5550
)
5651

5752
func main() {
58-
transport, err := namesilo.NewTokenTransport("1234")
59-
if err != nil {
60-
log.Fatal(err)
61-
}
62-
63-
client := namesilo.NewClient(transport.Client())
53+
client := namesilo.NewClient("1234")
6454

6555
// Set the endpoint to use the OTE endpoint.
6656
endpoint, err := namesilo.GetEndpoint(false, true)

zz_gen_client_test.go

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)