-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
344 lines (290 loc) · 9.76 KB
/
client.go
File metadata and controls
344 lines (290 loc) · 9.76 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Package sprites provides an idiomatic Go API for working with sprites.
package sprites
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
)
// Client is the main SDK client for interacting with the sprite API.
type Client struct {
baseURL string
token string
httpClient *http.Client
spriteVersion atomic.Value // stores string, empty until captured from response header
// Control connection pools per sprite
poolsMu sync.RWMutex
pools map[string]*controlPool
// Control initialization behavior
controlInitTimeout time.Duration
// disableControl prevents automatic control connection usage
disableControl bool
}
// Option is a functional option for configuring the SDK client.
type Option func(*Client)
// New creates a new SDK client with the given token and options.
func New(token string, opts ...Option) *Client {
c := &Client{
baseURL: "https://api.sprites.dev",
token: token,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
pools: make(map[string]*controlPool),
controlInitTimeout: 2 * time.Second,
}
for _, opt := range opts {
opt(c)
}
// Wrap transport to capture Sprite-Version header from responses
transport := c.httpClient.Transport
if transport == nil {
transport = http.DefaultTransport
}
c.httpClient.Transport = &versionCapturingTransport{
wrapped: transport,
versionHolder: &c.spriteVersion,
}
// Normalize baseURL
c.baseURL = strings.TrimRight(c.baseURL, "/")
return c
}
// NewClient creates a new Sprites API client with explicit parameters.
// This is an alternative constructor for compatibility.
func NewClient(baseURL, token string) *Client {
return New(token, WithBaseURL(baseURL))
}
// WithBaseURL sets a custom base URL for the sprite API.
func WithBaseURL(url string) Option {
return func(c *Client) {
c.baseURL = url
}
}
// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(client *http.Client) Option {
return func(c *Client) {
c.httpClient = client
}
}
// WithControlInitTimeout sets how long Sprite() will wait to establish a control connection
// before falling back to legacy endpoint API for that Sprite. Defaults to 2s.
func WithControlInitTimeout(d time.Duration) Option {
return func(c *Client) {
c.controlInitTimeout = d
}
}
// WithDisableControl prevents the SDK from using control connections.
// When disabled, all operations use direct WebSocket connections per request.
func WithDisableControl() Option {
return func(c *Client) {
c.disableControl = true
}
}
// Sprite returns a Sprite instance for the given name.
// This doesn't create the sprite on the server, it just returns a handle to work with it.
func (c *Client) Sprite(name string) *Sprite {
s := &Sprite{
name: name,
client: c,
}
// Attempt to establish control connection upfront; block until success or timeout/404
ctx, cancel := context.WithTimeout(context.Background(), c.controlInitTimeout)
defer cancel()
s.ensureControlSupport(ctx)
return s
}
// SpriteWithOrg returns a Sprite instance for the given name with organization information.
// This doesn't create the sprite on the server, it just returns a handle to work with it.
func (c *Client) SpriteWithOrg(name string, org *OrganizationInfo) *Sprite {
s := &Sprite{
name: name,
client: c,
org: org,
}
ctx, cancel := context.WithTimeout(context.Background(), c.controlInitTimeout)
defer cancel()
s.ensureControlSupport(ctx)
return s
}
// Create creates a new sprite with the given name and returns a handle to it.
// Deprecated: Use CreateSprite with context instead.
func (c *Client) Create(name string) (*Sprite, error) {
return c.CreateSprite(context.Background(), name, nil)
}
// List returns a list of available sprites.
// Deprecated: Use ListSprites with context instead.
func (c *Client) List() ([]*Sprite, error) {
return c.ListAllSprites(context.Background(), "")
}
// SpriteVersion returns the captured server version, or empty string if unknown.
func (c *Client) SpriteVersion() string {
if v := c.spriteVersion.Load(); v != nil {
return v.(string)
}
return ""
}
// supportsPathAttach returns true if the server supports path-based attach endpoint.
func (c *Client) supportsPathAttach() bool {
return supportsPathAttach(c.SpriteVersion())
}
// FetchVersion makes a lightweight API call to capture the sprite's version.
// This is called automatically before attach operations if the version is unknown.
// The spriteName parameter is required to route the request to the specific sprite,
// since each sprite has its own version.
func (c *Client) FetchVersion(ctx context.Context, spriteName string) error {
url := fmt.Sprintf("%s/v1/sprites/%s/exec", c.baseURL, spriteName)
req, err := http.NewRequestWithContext(ctx, "HEAD", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch version: %w", err)
}
defer resp.Body.Close()
// Version is captured by the transport wrapper
return nil
}
// CreateToken creates a sprite access token using a Fly.io macaroon token.
// This is a static method that doesn't require a client instance.
//
// The flyMacaroon should be a valid Fly.io authentication token (starts with "FlyV1").
// The orgSlug is the organization slug (e.g., "personal" or organization name).
// The inviteCode is optional and only needed for organizations that require it.
//
// This method is typically used during initial setup to exchange Fly.io credentials
// for sprite-specific access tokens.
func CreateToken(ctx context.Context, flyMacaroon, orgSlug string, inviteCode string, apiURL ...string) (string, error) {
// Default API URL if not provided
baseURL := "https://api.sprites.dev"
if len(apiURL) > 0 && apiURL[0] != "" {
baseURL = apiURL[0]
}
// Build request URL
url := fmt.Sprintf("%s/v1/organizations/%s/tokens", baseURL, orgSlug)
// Create request body
reqBody := struct {
Description string `json:"description,omitempty"`
InviteCode string `json:"invite_code,omitempty"`
}{
Description: "Sprite SDK Token",
}
if inviteCode != "" {
reqBody.InviteCode = inviteCode
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return "", fmt.Errorf("failed to marshal request: %w", err)
}
// Create HTTP request
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
// Set headers - Fly tokens use "FlyV1" prefix
authHeader := "FlyV1 " + flyMacaroon
httpReq.Header.Set("Authorization", authHeader)
httpReq.Header.Set("Content-Type", "application/json")
// Make request
// Force HTTP/1.1 to avoid HTTP/2 header size limits in Fly.io's edge proxy
// The edge proxy rejects HTTP/2 requests with large headers (>16KB) even though
// the backend supports much larger headers (256KB)
// Setting TLSNextProto to a non-nil empty map disables HTTP/2 in the transport
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
},
}
resp, err := client.Do(httpReq)
if err != nil {
return "", fmt.Errorf("failed to create token: %w", err)
}
defer resp.Body.Close()
// Read response
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
}
// Check status
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return "", apiErr
}
return "", fmt.Errorf("API returned status %d: %s", resp.StatusCode, string(body))
}
// Parse response
var tokenResp struct {
Token string `json:"token"`
}
if err := json.Unmarshal(body, &tokenResp); err != nil {
return "", fmt.Errorf("failed to parse response: %w", err)
}
if tokenResp.Token == "" {
return "", fmt.Errorf("no token returned in response")
}
return tokenResp.Token, nil
}
// signalSession sends a signal to a session via HTTP POST.
// This is used as a fallback when the server doesn't support WebSocket signals.
func (c *Client) signalSession(ctx context.Context, spriteName, sessionID, signal string) error {
url := fmt.Sprintf("%s/v1/sprites/%s/exec/%s/kill?signal=%s&timeout=0s",
c.baseURL, spriteName, sessionID, signal)
req, err := http.NewRequestWithContext(ctx, "POST", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send signal: %w", err)
}
defer resp.Body.Close()
// 410 Gone means session already exited - not an error
if resp.StatusCode == http.StatusGone {
return nil
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("signal failed (status %d): %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
return nil
}
// getOrCreatePool gets or creates a control pool for the given sprite
func (c *Client) getOrCreatePool(spriteName string) *controlPool {
c.poolsMu.RLock()
pool, exists := c.pools[spriteName]
c.poolsMu.RUnlock()
if exists {
return pool
}
c.poolsMu.Lock()
defer c.poolsMu.Unlock()
// Check again in case another goroutine created it
pool, exists = c.pools[spriteName]
if exists {
return pool
}
pool = newControlPool(c, spriteName)
c.pools[spriteName] = pool
return pool
}
// Close closes the client and all pooled connections
func (c *Client) Close() error {
c.poolsMu.Lock()
defer c.poolsMu.Unlock()
for _, pool := range c.pools {
pool.close()
}
c.pools = nil
return nil
}