Skip to content

Commit

Permalink
Hostname can now be set via env var (#10)
Browse files Browse the repository at this point in the history
* Hostname can now be set via env var

* Added 'Changing the hostname' to the readme
  • Loading branch information
laconc authored Aug 20, 2019
1 parent 3205036 commit 44f7fa6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go get github.com/datadotworld/dwapi-go/dwapi
```

## Usage

The full package documentation is available at https://godoc.org/github.com/datadotworld/dwapi-go/dwapi.

You can also check out the API documentation at https://apidocs.data.world/api for specifics on the endpoints.
Expand Down Expand Up @@ -140,3 +141,16 @@ func main() {
*/
}
```

## Changing the hostname

The API calls are made to `https://api.data.world` by default, but the URL can be changed by setting the `DW_API_HOST` environment variable.

For customers in a single-tenant environment, you can also use the `DW_ENVIRONMENT` variable to alter the default URL. For example, for the customer `customer`, setting it will alter the URL to `https://api.customer.data.world`.

Additionally, the hostname can also be changed by explicitly setting the `BaseURL` property of the client, i.e.:
```
dw = dwapi.NewClient("token")
dw.BaseURL = "http://localhost:1010/v0"
```
_Notice that the stage also needs to be set if going down this path._
14 changes: 12 additions & 2 deletions dwapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
POST = "POST"
PUT = "PUT"

defaultBaseURL = "https://api.data.world/v0"
defaultBaseURL = "https://api.data.world"
)

type Client struct {
Expand Down Expand Up @@ -75,7 +75,7 @@ type paginatedResponse struct {

func NewClient(token string) *Client {
c := &Client{
BaseURL: defaultBaseURL,
BaseURL: getBaseURL(),
Token: token,
}
c.Dataset = &DatasetService{c}
Expand All @@ -90,6 +90,16 @@ func NewClient(token string) *Client {
return c
}

func getBaseURL() string {
baseURL := defaultBaseURL
if host := os.Getenv("DW_API_HOST"); host != "" {
baseURL = host
} else if env := os.Getenv("DW_ENVIRONMENT"); env != "" {
baseURL = fmt.Sprintf("https://api.%s.data.world", env)
}
return baseURL + "/v0"
}

func (c *Client) buildHeaders(method, endpoint string) *headers {
return &headers{
Method: method,
Expand Down
16 changes: 15 additions & 1 deletion dwapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,21 @@ func teardown() {

func TestNewClient(t *testing.T) {
dw = getTestClient()
assert.Equal(t, dw.BaseURL, defaultBaseURL)
assert.NotEmpty(t, dw.BaseURL)
assert.Equal(t, dw.Token, "secret.token")
}

func TestGetBaseURL(t *testing.T) {
dw := getTestClient()
assert.Equal(t, dw.BaseURL, defaultBaseURL+"/v0")

_ = os.Setenv("DW_ENVIRONMENT", "sparklesquad")
dw = getTestClient()
assert.Equal(t, dw.BaseURL, "https://api.sparklesquad.data.world/v0")

_ = os.Setenv("DW_API_HOST", "http://localhost:1010")
dw = getTestClient()
assert.Equal(t, dw.BaseURL, "http://localhost:1010/v0")
}

func TestClient_RequestMultiplePages(t *testing.T) {
Expand Down

0 comments on commit 44f7fa6

Please sign in to comment.