Skip to content

Commit 11020ca

Browse files
Philip Reichenbergerpaperspace-philip
Philip Reichenberger
authored andcommitted
Add environment variable support
1 parent db93387 commit 11020ca

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

README.md

+2-17
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,8 @@ import (
99
)
1010

1111
func getClient() *paperspace.Client {
12-
apiBackend := paperspace.NewAPIBackend()
13-
if p.BaseURL != "" {
14-
apiBackend.BaseURL = p.BaseURL
15-
}
16-
if os.Getenv("PAPERSPACE_BASEURL") != "" {
17-
apiBackend.BaseURL = os.Getenv("PAPERSPACE_BASEURL")
18-
}
19-
apiBackend.Debug = p.Debug
20-
if os.Getenv("PAPERSPACE_DEBUG") != "" {
21-
apiBackend.Debug = true
22-
}
23-
apiBackend.DebugBody = p.DebugBody
24-
if os.Getenv("PAPERSPACE_DEBUG_BODY") != "" {
25-
apiBackend.DebugBody = true
26-
}
27-
client := paperspace.NewClientWithBackend(paperspace.Backend(apiBackend))
12+
client := paperspace.NewClient()
2813
client.APIKey = p.APIKey
2914
return client
3015
}
31-
```
16+
```

api_backend.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"math"
1111
"net/http"
1212
"net/http/httputil"
13+
"os"
1314
"time"
1415
)
1516

@@ -24,14 +25,31 @@ type APIBackend struct {
2425
}
2526

2627
func NewAPIBackend() *APIBackend {
27-
return &APIBackend{
28+
apiBackend := APIBackend{
2829
BaseURL: DefaultBaseURL,
29-
Debug: false,
3030
HTTPClient: &http.Client{
3131
Timeout: 15 * time.Second,
3232
},
3333
RetryCount: 0,
3434
}
35+
36+
baseURL := os.Getenv("PAPERSPACE_BASEURL")
37+
if baseURL != "" {
38+
apiBackend.BaseURL = baseURL
39+
}
40+
41+
debug := os.Getenv("PAPERSPACE_DEBUG")
42+
if debug != "" {
43+
apiBackend.Debug = true
44+
}
45+
46+
debugBody := os.Getenv("PAPERSPACE_DEBUG_BODY")
47+
if debugBody != "" {
48+
apiBackend.DebugBody = true
49+
}
50+
51+
52+
return &apiBackend
3553
}
3654

3755
func (c *APIBackend) Request(method string, url string,

client.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package paperspace
33
import (
44
"context"
55
"net/http"
6+
"os"
67
)
78

89
type RequestParams struct {
@@ -17,15 +18,23 @@ type Client struct {
1718

1819
// client that makes requests to Gradient API
1920
func NewClient() *Client {
20-
return &Client{
21+
client := Client{
2122
Backend: NewAPIBackend(),
2223
}
24+
25+
apiKey := os.Getenv("PAPERSPACE_APIKEY")
26+
if apiKey != "" {
27+
client.APIKey = apiKey
28+
}
29+
30+
return &client
2331
}
2432

2533
func NewClientWithBackend(backend Backend) *Client {
26-
return &Client{
27-
Backend: backend,
28-
}
34+
client := NewClient()
35+
client.Backend = backend
36+
37+
return client
2938
}
3039

3140
func (c *Client) Request(method string, url string, params, result interface{}, requestParams RequestParams) (*http.Response, error) {

0 commit comments

Comments
 (0)