Skip to content

Commit b17220a

Browse files
Philip Reichenbergerpaperspace-philip
Philip Reichenberger
authored andcommitted
refactor to support client params
1 parent c02af57 commit b17220a

File tree

5 files changed

+84
-36
lines changed

5 files changed

+84
-36
lines changed

api_backend.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package paperspace
22

33
import (
44
"bytes"
5-
"context"
65
"encoding/json"
76
"errors"
87
"fmt"
@@ -35,13 +34,13 @@ func NewAPIBackend() *APIBackend {
3534
}
3635
}
3736

38-
func (c *APIBackend) Request(ctx context.Context, method string, url string,
39-
params, result interface{}, headers map[string]string) (res *http.Response, err error) {
37+
func (c *APIBackend) Request(method string, url string,
38+
params, result interface{}, clientParams ClientParams) (res *http.Response, err error) {
4039
for i := 0; i < c.RetryCount+1; i++ {
4140
retryDuration := time.Duration((math.Pow(2, float64(i))-1)/2*1000) * time.Millisecond
4241
time.Sleep(retryDuration)
4342

44-
res, err = c.request(ctx, method, url, params, result, headers)
43+
res, err = c.request(method, url, params, result, clientParams)
4544
if res != nil && res.StatusCode == 429 {
4645
continue
4746
} else {
@@ -52,9 +51,10 @@ func (c *APIBackend) Request(ctx context.Context, method string, url string,
5251
return res, err
5352
}
5453

55-
func (c *APIBackend) request(ctx context.Context, method string, url string,
56-
params, result interface{}, headers map[string]string) (res *http.Response, err error) {
54+
func (c *APIBackend) request(method string, url string,
55+
params, result interface{}, clientParams ClientParams) (res *http.Response, err error) {
5756
var data []byte
57+
var req *http.Request
5858
body := bytes.NewReader(make([]byte, 0))
5959

6060
if params != nil {
@@ -67,16 +67,24 @@ func (c *APIBackend) request(ctx context.Context, method string, url string,
6767
}
6868

6969
fullURL := fmt.Sprintf("%s%s", c.BaseURL, url)
70-
req, err := http.NewRequestWithContext(ctx, method, fullURL, body)
71-
if err != nil {
72-
return res, err
70+
71+
if clientParams.Context == nil {
72+
req, err = http.NewRequest(method, fullURL, body)
73+
if err != nil {
74+
return res, err
75+
}
76+
} else {
77+
req, err = http.NewRequestWithContext(clientParams.Context, method, fullURL, body)
78+
if err != nil {
79+
return res, err
80+
}
7381
}
7482

7583
req.Header.Add("Accept", "application/json")
7684
req.Header.Add("Content-Type", "application/json")
7785
req.Header.Add("User-Agent", "Go Paperspace Gradient 1.0")
7886

79-
for key, value := range headers {
87+
for key, value := range clientParams.Headers {
8088
req.Header.Add(key, value)
8189
}
8290

backend.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package paperspace
22

33
import (
4-
"context"
54
"net/http"
65
)
76

87
type Backend interface {
9-
Request(ctx context.Context, method string, url string, params, result interface{}, headers map[string]string) (*http.Response, error)
8+
Request(method string, url string, params, result interface{}, clientParams ClientParams) (*http.Response, error)
109
}

client.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55
"net/http"
66
)
77

8+
type ClientParams struct {
9+
Context context.Context `json:"-"`
10+
Headers map[string]string `json:"-"`
11+
}
12+
813
type Client struct {
914
APIKey string
1015
Backend Backend
@@ -23,9 +28,11 @@ func NewClientWithBackend(backend Backend) *Client {
2328
}
2429
}
2530

26-
func (c *Client) Request(ctx context.Context, method string, url string, params, result interface{}) (*http.Response, error) {
27-
headers := map[string]string{
28-
"x-api-key": c.APIKey,
31+
func (c *Client) Request(method string, url string, params, result interface{}, clientParams ClientParams) (*http.Response, error) {
32+
if clientParams.Headers == nil {
33+
clientParams.Headers = make(map[string]string)
2934
}
30-
return c.Backend.Request(ctx, method, url, params, result, headers)
35+
clientParams.Headers["x-api-key"] = c.APIKey
36+
37+
return c.Backend.Request(method, url, params, result, clientParams)
3138
}

cluster.go

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package paperspace
22

33
import (
4-
"context"
54
"fmt"
65
)
76

@@ -49,6 +48,8 @@ type Cluster struct {
4948
}
5049

5150
type ClusterCreateParams struct {
51+
ClientParams
52+
5253
ArtifactsAccessKeyID string `json:"accessKey,omitempty" yaml:"artifactsAccessKeyId,omitempty"`
5354
ArtifactsBucketPath string `json:"bucketPath,omitempty" yaml:"artifactsBucketPath,omitempty"`
5455
ArtifactsSecretAccessKey string `json:"secretKey,omitempty" yaml:"artifactsSecretAccessKey,omitempty"`
@@ -61,28 +62,38 @@ type ClusterCreateParams struct {
6162
}
6263

6364
type ClusterListParams struct {
65+
ClientParams
66+
6467
Filter map[string]string `json:"filter"`
6568
}
6669

6770
type ClusterUpdateAttributeParams struct {
71+
ClientParams
72+
6873
Domain string `json:"fqdn,omitempty" yaml:"domain"`
6974
Name string `json:"name,omitempty" yaml:"name"`
7075
}
7176

7277
type ClusterUpdateRegistryParams struct {
78+
ClientParams
79+
7380
URL string `json:"url,omitempty"`
7481
Password string `json:"password,omitempty"`
7582
Repository string `json:"repository,omitempty"`
7683
Username string `json:"username,omitempty"`
7784
}
7885

7986
type ClusterUpdateS3Params struct {
87+
ClientParams
88+
8089
AccessKey string `json:"accessKey,omitempty"`
8190
Bucket string `json:"bucket,omitempty"`
8291
SecretKey string `json:"secretKey,omitempty"`
8392
}
8493

8594
type ClusterUpdateParams struct {
95+
ClientParams
96+
8697
Attributes ClusterUpdateAttributeParams `json:"attributes,omitempty"`
8798
CreateNewToken bool `json:"createNewToken,omitempty"`
8899
RegistryAttributes ClusterUpdateRegistryParams `json:"registryAttributes,omitempty"`
@@ -99,26 +110,31 @@ func NewClusterListParams() *ClusterListParams {
99110
return &clusterListParams
100111
}
101112

102-
func (c Client) CreateCluster(ctx context.Context, params ClusterCreateParams) (Cluster, error) {
113+
func (c Client) CreateCluster(params ClusterCreateParams) (Cluster, error) {
103114
cluster := Cluster{}
104115
params.Type = DefaultClusterType
105116

106117
url := fmt.Sprintf("/clusters/createCluster")
107-
_, err := c.Request(ctx, "POST", url, params, &cluster)
118+
_, err := c.Request("POST", url, params, &cluster, params.ClientParams)
108119

109120
return cluster, err
110121
}
111122

112-
func (c Client) GetCluster(ctx context.Context, id string) (Cluster, error) {
123+
func (c Client) GetCluster(id string, p ...ClientParams) (Cluster, error) {
124+
var clientParams ClientParams
113125
cluster := Cluster{}
114126

127+
if len(p) > 0 {
128+
clientParams = p[0]
129+
}
130+
115131
url := fmt.Sprintf("/clusters/getCluster?id=%s", id)
116-
_, err := c.Request(ctx, "GET", url, nil, &cluster)
132+
_, err := c.Request("GET", url, nil, &cluster, clientParams)
117133

118134
return cluster, err
119135
}
120136

121-
func (c Client) GetClusters(ctx context.Context, p ...ClusterListParams) ([]Cluster, error) {
137+
func (c Client) GetClusters(p ...ClusterListParams) ([]Cluster, error) {
122138
clusters := []Cluster{}
123139
params := NewClusterListParams()
124140

@@ -127,16 +143,16 @@ func (c Client) GetClusters(ctx context.Context, p ...ClusterListParams) ([]Clus
127143
}
128144

129145
url := fmt.Sprintf("/clusters/getClusters")
130-
_, err := c.Request(ctx, "GET", url, params, &clusters)
146+
_, err := c.Request("GET", url, params, &clusters, params.ClientParams)
131147

132148
return clusters, err
133149
}
134150

135-
func (c Client) UpdateCluster(ctx context.Context, id string, p ClusterUpdateParams) (Cluster, error) {
151+
func (c Client) UpdateCluster(id string, p ClusterUpdateParams) (Cluster, error) {
136152
cluster := Cluster{}
137153

138154
url := fmt.Sprintf("/clusters/updateCluster")
139-
_, err := c.Request(ctx, "POST", url, p, &cluster)
155+
_, err := c.Request("POST", url, p, &cluster, p.ClientParams)
140156

141157
return cluster, err
142158
}

machine.go

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package paperspace
22

33
import (
4-
"context"
54
"fmt"
65
"time"
76
)
@@ -44,6 +43,8 @@ type Machine struct {
4443
}
4544

4645
type MachineCreateParams struct {
46+
ClientParams
47+
4748
Name string `json:"name"`
4849
Region string `json:"region"`
4950
MachineType string `json:"machineType"`
@@ -60,14 +61,20 @@ type MachineCreateParams struct {
6061
}
6162

6263
type MachineListParams struct {
64+
ClientParams
65+
6366
Filter map[string]string `json:"filter"`
6467
}
6568

6669
type MachineUpdateAttributeParams struct {
70+
ClientParams
71+
6772
Name string `json:"name,omitempty" yaml:"name"`
6873
}
6974

7075
type MachineUpdateParams struct {
76+
ClientParams
77+
7178
ID string `json:"machineId"`
7279
Name string `json:"machineName,omitempty"`
7380
ShutdownTimeoutInHours int `json:"shutdownTimeoutInHours,omitempty"`
@@ -86,25 +93,30 @@ func NewMachineListParams() *MachineListParams {
8693
return &machineListParams
8794
}
8895

89-
func (c Client) CreateMachine(ctx context.Context, params MachineCreateParams) (Machine, error) {
96+
func (c Client) CreateMachine(params MachineCreateParams) (Machine, error) {
9097
machine := Machine{}
9198

9299
url := fmt.Sprintf("/machines/createSingleMachinePublic")
93-
_, err := c.Request(ctx, "POST", url, params, &machine)
100+
_, err := c.Request("POST", url, params, &machine, params.ClientParams)
94101

95102
return machine, err
96103
}
97104

98-
func (c Client) GetMachine(ctx context.Context, id string) (Machine, error) {
105+
func (c Client) GetMachine(id string, p ...ClientParams) (Machine, error) {
106+
var params ClientParams
99107
machine := Machine{}
100108

109+
if len(p) > 0 {
110+
params = p[0]
111+
}
112+
101113
url := fmt.Sprintf("/machines/getMachinePublic?machineId=%s", id)
102-
_, err := c.Request(ctx, "GET", url, nil, &machine)
114+
_, err := c.Request("GET", url, nil, &machine, params)
103115

104116
return machine, err
105117
}
106118

107-
func (c Client) GetMachines(ctx context.Context, p ...MachineListParams) ([]Machine, error) {
119+
func (c Client) GetMachines(p ...MachineListParams) ([]Machine, error) {
108120
var machines []Machine
109121
params := NewMachineListParams()
110122

@@ -113,23 +125,29 @@ func (c Client) GetMachines(ctx context.Context, p ...MachineListParams) ([]Mach
113125
}
114126

115127
url := fmt.Sprintf("/machines/getMachines")
116-
_, err := c.Request(ctx, "GET", url, params, &machines)
128+
_, err := c.Request("GET", url, params, &machines, params.ClientParams)
117129

118130
return machines, err
119131
}
120132

121-
func (c Client) UpdateMachine(ctx context.Context, p MachineUpdateParams) (Machine, error) {
133+
func (c Client) UpdateMachine(p MachineUpdateParams) (Machine, error) {
122134
machine := Machine{}
123135

124136
url := fmt.Sprintf("/machines/updateMachine")
125-
_, err := c.Request(ctx, "POST", url, p, &machine)
137+
_, err := c.Request("POST", url, p, &machine, p.ClientParams)
126138

127139
return machine, err
128140
}
129141

130-
func (c Client) DeleteMachine(ctx context.Context, id string) error {
142+
func (c Client) DeleteMachine(id string, p ...ClientParams) error {
143+
var params ClientParams
144+
145+
if len(p) > 0 {
146+
params = p[0]
147+
}
148+
131149
url := fmt.Sprintf("/machines/%s/destroyMachine", id)
132-
_, err := c.Request(ctx, "POST", url, nil, nil)
150+
_, err := c.Request("POST", url, nil, nil, params)
133151

134152
return err
135153
}

0 commit comments

Comments
 (0)