Skip to content

Commit

Permalink
Merge pull request #3 from digitalocean/prepend-base-path
Browse files Browse the repository at this point in the history
client{,_test}: allow base URL path to be specified and prepended to …
  • Loading branch information
mdlayher authored Jul 8, 2016
2 parents a06f2c7 + 3977c33 commit 2a8cd77
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"net/http"
"net/url"
"path"
)

// A Client is a NetBox client. It can be used to retrieve network and
Expand Down Expand Up @@ -69,6 +70,16 @@ func (c *Client) newRequest(method string, endpoint string, options valuer) (*ht
if err != nil {
return nil, err
}

// Allow specifying a base path for API requests, so if a NetBox server
// resides at a path like http://example.com/netbox/, API requests will
// be sent to http://example.com/netbox/api/...
//
// Enables support of: https://github.com/digitalocean/netbox/issues/212.
if c.u.Path != "" {
rel.Path = path.Join(c.u.Path, rel.Path)
}

u := c.u.ResolveReference(rel)

// If no valuer specified, create a request with no query parameters
Expand Down
22 changes: 22 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ func TestClientQueryParameters(t *testing.T) {
}
}

func TestClientPrependBaseURLPath(t *testing.T) {
u, err := url.Parse("http://example.com/netbox/")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

c := &Client{
u: u,
client: &http.Client{},
}

req, err := c.newRequest(http.MethodGet, "/api/ipam/vlans", nil)
if err != nil {
t.Fatal("expected an error, but no error returned")
}

if want, got := "/netbox/api/ipam/vlans", req.URL.Path; want != got {
t.Fatalf("unexpected URL path:\n- want: %q\n- got: %q",
want, got)
}
}

type testValuer struct {
Foo string
Bar int
Expand Down

0 comments on commit 2a8cd77

Please sign in to comment.