Skip to content

Commit 44f7fa6

Browse files
authored
Hostname can now be set via env var (#10)
* Hostname can now be set via env var * Added 'Changing the hostname' to the readme
1 parent 3205036 commit 44f7fa6

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go get github.com/datadotworld/dwapi-go/dwapi
1616
```
1717

1818
## Usage
19+
1920
The full package documentation is available at https://godoc.org/github.com/datadotworld/dwapi-go/dwapi.
2021

2122
You can also check out the API documentation at https://apidocs.data.world/api for specifics on the endpoints.
@@ -140,3 +141,16 @@ func main() {
140141
*/
141142
}
142143
```
144+
145+
## Changing the hostname
146+
147+
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.
148+
149+
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`.
150+
151+
Additionally, the hostname can also be changed by explicitly setting the `BaseURL` property of the client, i.e.:
152+
```
153+
dw = dwapi.NewClient("token")
154+
dw.BaseURL = "http://localhost:1010/v0"
155+
```
156+
_Notice that the stage also needs to be set if going down this path._

dwapi/api.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242
POST = "POST"
4343
PUT = "PUT"
4444

45-
defaultBaseURL = "https://api.data.world/v0"
45+
defaultBaseURL = "https://api.data.world"
4646
)
4747

4848
type Client struct {
@@ -75,7 +75,7 @@ type paginatedResponse struct {
7575

7676
func NewClient(token string) *Client {
7777
c := &Client{
78-
BaseURL: defaultBaseURL,
78+
BaseURL: getBaseURL(),
7979
Token: token,
8080
}
8181
c.Dataset = &DatasetService{c}
@@ -90,6 +90,16 @@ func NewClient(token string) *Client {
9090
return c
9191
}
9292

93+
func getBaseURL() string {
94+
baseURL := defaultBaseURL
95+
if host := os.Getenv("DW_API_HOST"); host != "" {
96+
baseURL = host
97+
} else if env := os.Getenv("DW_ENVIRONMENT"); env != "" {
98+
baseURL = fmt.Sprintf("https://api.%s.data.world", env)
99+
}
100+
return baseURL + "/v0"
101+
}
102+
93103
func (c *Client) buildHeaders(method, endpoint string) *headers {
94104
return &headers{
95105
Method: method,

dwapi/api_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,21 @@ func teardown() {
132132

133133
func TestNewClient(t *testing.T) {
134134
dw = getTestClient()
135-
assert.Equal(t, dw.BaseURL, defaultBaseURL)
135+
assert.NotEmpty(t, dw.BaseURL)
136+
assert.Equal(t, dw.Token, "secret.token")
137+
}
138+
139+
func TestGetBaseURL(t *testing.T) {
140+
dw := getTestClient()
141+
assert.Equal(t, dw.BaseURL, defaultBaseURL+"/v0")
142+
143+
_ = os.Setenv("DW_ENVIRONMENT", "sparklesquad")
144+
dw = getTestClient()
145+
assert.Equal(t, dw.BaseURL, "https://api.sparklesquad.data.world/v0")
146+
147+
_ = os.Setenv("DW_API_HOST", "http://localhost:1010")
148+
dw = getTestClient()
149+
assert.Equal(t, dw.BaseURL, "http://localhost:1010/v0")
136150
}
137151

138152
func TestClient_RequestMultiplePages(t *testing.T) {

0 commit comments

Comments
 (0)