-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmindupload.go
More file actions
278 lines (248 loc) · 7.36 KB
/
Copy pathmindupload.go
File metadata and controls
278 lines (248 loc) · 7.36 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
package mindupload
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"time"
)
// DefaultBaseURL is the Mind Upload partner API endpoint.
const DefaultBaseURL = "https://partner.mindupload.app"
const authHeader = "X-Partner-Key"
// Only server backpressure is retried. Operations are non-idempotent POSTs (Rag
// spends credits, Create* mutate), so 5xx / network / timeout failures are
// surfaced immediately rather than risking a duplicate side effect.
var retryStatuses = map[int]bool{429: true}
// Client is a Mind Upload partner API client. Create one with New; it is safe
// for concurrent use.
type Client struct {
partnerKey string
baseURL string
preferredLanguage string
httpClient *http.Client
maxRetries int
timeout time.Duration
userAgent string
}
// Option configures a Client.
type Option func(*Client)
// WithBaseURL overrides the API endpoint.
func WithBaseURL(u string) Option {
return func(c *Client) { c.baseURL = strings.TrimRight(u, "/") }
}
// WithPreferredLanguage sets a default locale sent with every call; a per-call
// PreferredLanguage overrides it.
func WithPreferredLanguage(lang string) Option {
return func(c *Client) { c.preferredLanguage = lang }
}
// WithHTTPClient supplies a custom *http.Client (for proxies, transports, etc.).
func WithHTTPClient(h *http.Client) Option {
return func(c *Client) {
if h != nil {
c.httpClient = h
}
}
}
// WithTimeout sets a per-call timeout, applied via context so it composes with
// any custom HTTP client (and never mutates a caller-owned one). Zero disables it.
func WithTimeout(d time.Duration) Option {
return func(c *Client) { c.timeout = d }
}
// WithMaxRetries sets how many times an explicit 429 response is retried.
func WithMaxRetries(n int) Option {
return func(c *Client) {
if n >= 0 {
c.maxRetries = n
}
}
}
// WithUserAgent overrides the User-Agent header.
func WithUserAgent(ua string) Option {
return func(c *Client) {
if ua != "" {
c.userAgent = ua
}
}
}
// New creates a client. partnerKey is a server-side secret — never embed it in
// client-side code.
func New(partnerKey string, opts ...Option) *Client {
c := &Client{
partnerKey: partnerKey,
baseURL: DefaultBaseURL,
httpClient: &http.Client{},
maxRetries: 2,
timeout: 30 * time.Second,
userAgent: "mindupload-go/" + Version,
}
for _, opt := range opts {
opt(c)
}
return c
}
// Response is a decoded API response. Success is always set; every returned
// field is available via Get/String or Decode, keyed by its documented name.
type Response struct {
Success bool
ErrorMessage string
Data map[string]any
}
// Get returns a raw field from the response (nil if absent).
func (r *Response) Get(key string) any { return r.Data[key] }
// String returns a string field (empty if absent or not a string).
func (r *Response) String(key string) string {
if s, ok := r.Data[key].(string); ok {
return s
}
return ""
}
// Bool returns a boolean field (false if absent or not a bool).
func (r *Response) Bool(key string) bool {
if b, ok := r.Data[key].(bool); ok {
return b
}
return false
}
// Strings returns a string-array field (nil if absent or malformed).
func (r *Response) Strings(key string) []string {
values, ok := r.Data[key].([]any)
if !ok {
return nil
}
result := make([]string, 0, len(values))
// Conversion is local and bounded by one response field; no external call
// occurs inside this loop.
for _, value := range values {
item, ok := value.(string)
if !ok {
return nil
}
result = append(result, item)
}
return result
}
// Decode unmarshals the full response into v (a struct or map).
func (r *Response) Decode(v any) error {
b, err := json.Marshal(r.Data)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
// Ptr returns a pointer to v — for optional scalar params, e.g. Ptr(true).
func Ptr[T any](v T) *T { return &v }
func structToMap(v any) map[string]any {
b, err := json.Marshal(v)
if err != nil {
return map[string]any{}
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil || m == nil {
return map[string]any{}
}
return m
}
func (c *Client) do(ctx context.Context, operation string, params map[string]any) (*Response, error) {
if c.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.timeout)
defer cancel()
}
if _, ok := params["preferred_language"]; !ok && c.preferredLanguage != "" {
params["preferred_language"] = c.preferredLanguage
}
payload, err := json.Marshal(params)
if err != nil {
return nil, &Error{Operation: operation, Message: "failed to encode request: " + err.Error()}
}
url := c.baseURL + "/v1/" + operation
for attempt := 0; ; attempt++ {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
if err != nil {
return nil, &Error{Operation: operation, Message: err.Error(), err: ErrConnection}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set(authHeader, c.partnerKey)
req.Header.Set("User-Agent", c.userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
if ctx.Err() != nil {
// Wrap the real context error so errors.Is(err, context.DeadlineExceeded) works.
return nil, &Error{Operation: operation, Message: ctx.Err().Error(), err: ctx.Err()}
}
// Not retried: the request may already have reached the backend.
return nil, &Error{Operation: operation, Message: err.Error(), err: ErrConnection}
}
bodyBytes, _ := io.ReadAll(resp.Body)
resp.Body.Close()
data := map[string]any{}
if len(bodyBytes) > 0 {
_ = json.Unmarshal(bodyBytes, &data)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if retryStatuses[resp.StatusCode] && attempt < c.maxRetries {
if werr := sleepCtx(ctx, backoff(attempt+1, resp.Header.Get("Retry-After"))); werr != nil {
return nil, werr
}
continue
}
return nil, errorFor(resp.StatusCode, data, operation, resp.Header.Get("Retry-After"))
}
success, _ := data["success"].(bool)
msg, _ := data["error_message"].(string)
if !success {
if msg == "" {
msg = operation + " failed"
}
return nil, &Error{Operation: operation, Message: msg, Response: data}
}
return &Response{Success: true, ErrorMessage: msg, Data: data}, nil
}
}
func errorFor(status int, data map[string]any, operation, retryAfter string) *Error {
msg, _ := data["error_message"].(string)
if msg == "" {
msg = "HTTP " + strconv.Itoa(status)
}
e := &Error{Operation: operation, Status: status, Message: msg, Response: data}
switch status {
case 401:
e.err = ErrAuthentication
case 429:
e.err = ErrRateLimit
if ra, perr := strconv.ParseFloat(retryAfter, 64); perr == nil {
e.RetryAfter = ra
}
}
return e
}
func backoff(attempt int, retryAfter string) time.Duration {
if retryAfter != "" {
if s, err := strconv.ParseFloat(retryAfter, 64); err == nil {
d := time.Duration(s * float64(time.Second))
if d > 60*time.Second {
d = 60 * time.Second
}
return d
}
}
ms := 500 * (1 << uint(attempt-1))
if ms > 8000 {
ms = 8000
}
return time.Duration(ms) * time.Millisecond
}
func sleepCtx(ctx context.Context, d time.Duration) error {
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
return &Error{Message: ctx.Err().Error(), err: ctx.Err()}
case <-t.C:
return nil
}
}