-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
188 lines (170 loc) · 5.58 KB
/
client.go
File metadata and controls
188 lines (170 loc) · 5.58 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package codesandboxsdk
import (
"fmt"
"net/http"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
csbbase "github.com/callstackai/codesandboxsdk-go/pkg/base"
csbgit "github.com/callstackai/codesandboxsdk-go/pkg/git"
csbport "github.com/callstackai/codesandboxsdk-go/pkg/port"
csbsandboxcontainer "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/container"
csbsandboxfs "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/fs"
csbsandboxgit "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/git"
csbsandboxsetup "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/setup"
csbsandboxshell "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/shell"
csbsandboxsystem "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/system"
csbsandboxtask "github.com/callstackai/codesandboxsdk-go/pkg/sandbox/task"
)
const (
APIBaseURL = "https://api.codesandbox.io"
)
type SandboxClients struct {
Container *csbsandboxcontainer.ClientWithResponses
FileSystem *csbsandboxfs.ClientWithResponses
Git *csbsandboxgit.ClientWithResponses
Setup *csbsandboxsetup.ClientWithResponses
Shell *csbsandboxshell.ClientWithResponses
System *csbsandboxsystem.ClientWithResponses
Task *csbsandboxtask.ClientWithResponses
}
type Client struct {
*csbbase.ClientWithResponses
Git *csbgit.ClientWithResponses
Port *csbport.ClientWithResponses
Sandbox *SandboxClients
}
type opts struct {
client *http.Client
baseURL string
token string
}
type Opts func(*opts)
func WithHTTPClient(client *http.Client) Opts {
return func(o *opts) {
o.client = client
}
}
func WithToken(token string) Opts {
return func(o *opts) {
o.token = token
}
}
func WithBaseURL(baseURL string) Opts {
return func(o *opts) {
o.baseURL = baseURL
}
}
func resolveOpts(options ...Opts) *opts {
cfg := &opts{
client: &http.Client{},
baseURL: APIBaseURL,
}
for _, o := range options {
o(cfg)
}
return cfg
}
func NewClient(opts ...Opts) (*Client, error) {
config := resolveOpts(opts...)
bearerAuth, err := securityprovider.NewSecurityProviderBearerToken(config.token)
if err != nil {
return nil, fmt.Errorf("failed to create auth provider: %w", err)
}
baseClient, err := csbbase.NewClientWithResponses(
config.baseURL,
csbbase.WithHTTPClient(config.client),
csbbase.WithBaseURL(config.baseURL),
csbbase.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create base client: %w", err)
}
gitClient, err := csbgit.NewClientWithResponses(
config.baseURL,
csbgit.WithHTTPClient(config.client),
csbgit.WithBaseURL(config.baseURL),
csbgit.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create git client: %w", err)
}
portClient, err := csbport.NewClientWithResponses(
config.baseURL,
csbport.WithHTTPClient(config.client),
csbport.WithBaseURL(config.baseURL),
csbport.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create port client: %w", err)
}
sandboxClients := &SandboxClients{}
sandboxClients.Container, err = csbsandboxcontainer.NewClientWithResponses(
config.baseURL,
csbsandboxcontainer.WithHTTPClient(config.client),
csbsandboxcontainer.WithBaseURL(config.baseURL),
csbsandboxcontainer.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox container client: %w", err)
}
sandboxClients.FileSystem, err = csbsandboxfs.NewClientWithResponses(
config.baseURL,
csbsandboxfs.WithHTTPClient(config.client),
csbsandboxfs.WithBaseURL(config.baseURL),
csbsandboxfs.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox filesystem client: %w", err)
}
sandboxClients.Git, err = csbsandboxgit.NewClientWithResponses(
config.baseURL,
csbsandboxgit.WithHTTPClient(config.client),
csbsandboxgit.WithBaseURL(config.baseURL),
csbsandboxgit.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox git client: %w", err)
}
sandboxClients.Setup, err = csbsandboxsetup.NewClientWithResponses(
config.baseURL,
csbsandboxsetup.WithHTTPClient(config.client),
csbsandboxsetup.WithBaseURL(config.baseURL),
csbsandboxsetup.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox setup client: %w", err)
}
sandboxClients.Shell, err = csbsandboxshell.NewClientWithResponses(
config.baseURL,
csbsandboxshell.WithHTTPClient(config.client),
csbsandboxshell.WithBaseURL(config.baseURL),
csbsandboxshell.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox shell client: %w", err)
}
sandboxClients.System, err = csbsandboxsystem.NewClientWithResponses(
config.baseURL,
csbsandboxsystem.WithHTTPClient(config.client),
csbsandboxsystem.WithBaseURL(config.baseURL),
csbsandboxsystem.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox system client: %w", err)
}
sandboxClients.Task, err = csbsandboxtask.NewClientWithResponses(
config.baseURL,
csbsandboxtask.WithHTTPClient(config.client),
csbsandboxtask.WithBaseURL(config.baseURL),
csbsandboxtask.WithRequestEditorFn(bearerAuth.Intercept),
)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox task client: %w", err)
}
c := &Client{
ClientWithResponses: baseClient,
Git: gitClient,
Port: portClient,
Sandbox: sandboxClients,
}
return c, nil
}