-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
187 lines (167 loc) · 4.54 KB
/
Copy pathapi.go
File metadata and controls
187 lines (167 loc) · 4.54 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type apiClient struct {
base string
token string
http *http.Client
}
func newAPIClient(base, token string) *apiClient {
return &apiClient{
base: strings.TrimRight(base, "/"),
token: token,
http: &http.Client{Timeout: 30 * time.Second},
}
}
type signupIntentResp struct {
IntentID string `json:"intent_id"`
SignupURL string `json:"signup_url"`
ExpiresAt string `json:"expires_at"`
}
type signupIntentPollResp struct {
Status string `json:"status"`
APIToken string `json:"api_token,omitempty"`
}
type shareResp struct {
UUID string `json:"uuid"`
ShortID string `json:"short_id"`
Filename string `json:"filename"`
Path string `json:"path,omitempty"`
Watch bool `json:"watch"`
URL string `json:"url"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
SizeBytes int `json:"size_bytes"`
}
func (c *apiClient) do(method, path string, body, dst any) error {
status, err := c.doStatus(method, path, body, dst)
if err != nil {
return err
}
_ = status
return nil
}
func (c *apiClient) doStatus(method, path string, body, dst any) (int, error) {
var rdr io.Reader
if body != nil {
buf, err := json.Marshal(body)
if err != nil {
return 0, fmt.Errorf("marshal: %w", err)
}
rdr = bytes.NewReader(buf)
}
req, err := http.NewRequest(method, c.base+path, rdr)
if err != nil {
return 0, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.http.Do(req)
if err != nil {
return 0, fmt.Errorf("request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return resp.StatusCode, fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(raw)))
}
if dst == nil {
_, _ = io.Copy(io.Discard, resp.Body)
return resp.StatusCode, nil
}
if err := json.NewDecoder(resp.Body).Decode(dst); err != nil {
return resp.StatusCode, fmt.Errorf("decode: %w", err)
}
return resp.StatusCode, nil
}
func (c *apiClient) Signup(email string) (*signupIntentResp, error) {
var out signupIntentResp
if err := c.do("POST", "/api/signup/intent", map[string]string{"email": email}, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *apiClient) PollSignupIntent(id string) (*signupIntentPollResp, error) {
var out signupIntentPollResp
if err := c.do("GET", "/api/signup/intent/"+id, nil, &out); err != nil {
return nil, err
}
return &out, nil
}
type manageIntentResp struct {
IntentID string `json:"intent_id"`
DashboardURL string `json:"dashboard_url"`
ExpiresAt string `json:"expires_at"`
}
func (c *apiClient) OpenManageIntent() (*manageIntentResp, error) {
var out manageIntentResp
if err := c.do("POST", "/api/manage/intent", nil, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *apiClient) CreateShare(filename, path, content string, watch bool) (*shareResp, bool, error) {
var out shareResp
status, err := c.doStatus("POST", "/api/shares", map[string]any{
"filename": filename,
"path": path,
"content": content,
"watch": watch,
}, &out)
if err != nil {
return nil, false, err
}
return &out, status == http.StatusCreated, nil
}
func (c *apiClient) UpdateShare(uuid, content string) (*shareResp, error) {
var out shareResp
if err := c.do("PUT", fmt.Sprintf("/api/shares/%s", uuid), map[string]string{"content": content}, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *apiClient) DeleteShare(uuid string) error {
return c.do("DELETE", fmt.Sprintf("/api/shares/%s", uuid), nil, nil)
}
func (c *apiClient) ValidateToken() error {
return c.do("GET", "/api/shares", nil, nil)
}
func (c *apiClient) ListShares() ([]shareResp, error) {
var out []shareResp
if err := c.do("GET", "/api/shares", nil, &out); err != nil {
return nil, err
}
return out, nil
}
func (c *apiClient) ListSharesByFilename(filename string) ([]shareResp, error) {
var out []shareResp
path := "/api/shares?filename=" + url.QueryEscape(filename)
if err := c.do("GET", path, nil, &out); err != nil {
return nil, err
}
return out, nil
}
func (c *apiClient) GetShareByShortID(shortID string) (*shareResp, error) {
all, err := c.ListShares()
if err != nil {
return nil, err
}
for i := range all {
if all[i].ShortID == shortID {
return &all[i], nil
}
}
return nil, fmt.Errorf("share %s not found in your account", shortID)
}