aboutsummaryrefslogtreecommitdiff
path: root/web/fly/flymachines/client.go
blob: 30fc881dd0567027f3b83e10d40ce68231b0dcdf (plain)
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
package flymachines

import (
	"bytes"
	"context"
	"encoding/json"
	"net/http"
	"os"

	"within.website/x/web"
)

const (
	internalURL = "http://_api.internal:4280"
	publicURL   = "https://api.machines.dev"
)

type Client struct {
	token, apiURL string
	cli           *http.Client
}

// NewClient returns a new client for the Fly machines API with the given API token and URL.
//
// This is a fairly low-level operation for if you know what URL you need, you probably want to use New instead.
func NewClient(token, apiURL string, cli *http.Client) *Client {
	return &Client{token, apiURL, cli}
}

// New returns a new client for the Fly machines API with the given API token.
//
// This will automatically detect if you have access to the internal API either by a
// WireGuard tunnel or by being on the Fly network.
func New(token string, cli *http.Client) *Client {
	resp, err := http.Get(internalURL)
	if err != nil {
		return NewClient(token, publicURL, cli)
	}

	if resp.StatusCode != http.StatusNotFound {
		return NewClient(token, publicURL, cli)
	}

	return NewClient(token, internalURL, cli)
}

// Do performs a HTTP request with the appropriate authentication and user agent headers.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
	req.Header.Set("Authorization", "Bearer "+c.token)
	req.Header.Set("User-Agent", "within.website/x/web/fly/flymachines in "+os.Args[0])
	return c.cli.Do(req)
}

func (c *Client) doRequestNoResponse(ctx context.Context, method, path string, wantStatusCode int) error {
	req, err := http.NewRequestWithContext(ctx, method, c.apiURL+path, nil)
	if err != nil {
		return err
	}

	resp, err := c.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != wantStatusCode {
		return web.NewError(wantStatusCode, resp)
	}

	return nil
}

// zilch returns the zero value of a given type.
func zilch[T any]() T { return *new(T) }

func doJSON[Output any](ctx context.Context, c *Client, method, path string, wantStatusCode int) (Output, error) {
	req, err := http.NewRequestWithContext(ctx, method, c.apiURL+path, nil)
	if err != nil {
		return zilch[Output](), err
	}

	resp, err := c.Do(req)
	if err != nil {
		return zilch[Output](), err
	}
	defer resp.Body.Close()

	if resp.StatusCode != wantStatusCode {
		return zilch[Output](), web.NewError(wantStatusCode, resp)
	}

	var output Output

	if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
		return zilch[Output](), err
	}

	return output, nil
}

func doJSONBody[Input any, Output any](ctx context.Context, c *Client, method, path string, input Input, wantStatusCode int) (Output, error) {
	buf := new(bytes.Buffer)
	if err := json.NewEncoder(buf).Encode(input); err != nil {
		return zilch[Output](), err
	}

	req, err := http.NewRequestWithContext(ctx, method, c.apiURL+path, buf)
	if err != nil {
		return zilch[Output](), err
	}

	resp, err := c.Do(req)
	if err != nil {
		return zilch[Output](), err
	}
	defer resp.Body.Close()

	if resp.StatusCode != wantStatusCode {
		return zilch[Output](), web.NewError(wantStatusCode, resp)
	}

	var output Output

	if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
		return zilch[Output](), err
	}

	return output, nil
}