-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
47 lines (37 loc) · 922 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package paperspace
import (
"context"
"net/http"
"os"
)
type RequestParams struct {
Context context.Context `json:"-"`
Headers map[string]string `json:"-"`
}
type Client struct {
APIKey string
Backend Backend
}
// client that makes requests to Gradient API
func NewClient() *Client {
client := Client{
Backend: NewAPIBackend(),
}
apiKey := os.Getenv("PAPERSPACE_APIKEY")
if apiKey != "" {
client.APIKey = apiKey
}
return &client
}
func NewClientWithBackend(backend Backend) *Client {
client := NewClient()
client.Backend = backend
return client
}
func (c *Client) Request(method string, url string, params, result interface{}, requestParams RequestParams) (*http.Response, error) {
if requestParams.Headers == nil {
requestParams.Headers = make(map[string]string)
}
requestParams.Headers["x-api-key"] = c.APIKey
return c.Backend.Request(method, url, params, result, requestParams)
}