-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_responses.go
More file actions
321 lines (291 loc) · 10.4 KB
/
types_responses.go
File metadata and controls
321 lines (291 loc) · 10.4 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
package cloudlayer
import (
"encoding/json"
"fmt"
"math"
)
// Job represents a CloudLayer.io conversion job.
type Job struct {
ID string `json:"id"`
UID string `json:"uid"`
Name *string `json:"name,omitempty"`
Type *JobType `json:"type,omitempty"`
Status JobStatus `json:"status"`
Timestamp json.RawMessage `json:"timestamp,omitempty"`
WorkerName *string `json:"workerName,omitempty"`
ProcessTime *int `json:"processTime,omitempty"`
APIKeyUsed *string `json:"apiKeyUsed,omitempty"`
ProcessTimeCost *float64 `json:"processTimeCost,omitempty"`
APICreditCost *float64 `json:"apiCreditCost,omitempty"`
BandwidthCost *float64 `json:"bandwidthCost,omitempty"`
TotalCost *float64 `json:"totalCost,omitempty"`
Size *int `json:"size,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
AssetURL *string `json:"assetUrl,omitempty"`
PreviewURL *string `json:"previewUrl,omitempty"`
Self *string `json:"self,omitempty"`
AssetID *string `json:"assetId,omitempty"`
ProjectID *string `json:"projectId,omitempty"`
Error *string `json:"error,omitempty"`
}
// TimestampUnix returns the job timestamp as Unix milliseconds.
// It handles both numeric timestamps and Firestore timestamp objects
// ({_seconds, _nanoseconds}).
func (j *Job) TimestampUnix() int64 {
if j == nil || len(j.Timestamp) == 0 {
return 0
}
return parseTimestamp(j.Timestamp)
}
// Asset represents a generated file asset.
type Asset struct {
UID string `json:"uid"`
ID *string `json:"id,omitempty"`
FileID string `json:"fileId"`
PreviewFileID *string `json:"previewFileId,omitempty"`
Type *string `json:"type,omitempty"`
Ext *string `json:"ext,omitempty"`
PreviewExt *string `json:"previewExt,omitempty"`
URL *string `json:"url,omitempty"`
PreviewURL *string `json:"previewUrl,omitempty"`
Size *int `json:"size,omitempty"`
Timestamp json.RawMessage `json:"timestamp,omitempty"`
ProjectID *string `json:"projectId,omitempty"`
JobID *string `json:"jobId,omitempty"`
Name *string `json:"name,omitempty"`
}
// TimestampUnix returns the asset timestamp as Unix milliseconds.
// It handles both numeric timestamps and Firestore timestamp objects.
func (a *Asset) TimestampUnix() int64 {
if a == nil || len(a.Timestamp) == 0 {
return 0
}
return parseTimestamp(a.Timestamp)
}
// parseTimestamp extracts Unix milliseconds from either a number or a
// Firestore timestamp object ({_seconds, _nanoseconds}).
func parseTimestamp(data json.RawMessage) int64 {
// Try numeric first (most common)
var n float64
if err := json.Unmarshal(data, &n); err == nil {
return int64(n)
}
// Try Firestore timestamp object
var ts struct {
Seconds int64 `json:"_seconds"`
Nanoseconds int64 `json:"_nanoseconds"`
}
if err := json.Unmarshal(data, &ts); err == nil && ts.Seconds > 0 {
return ts.Seconds*1000 + ts.Nanoseconds/1_000_000
}
return 0
}
// StorageListItem is returned by [Client.ListStorage].
// It contains only the ID and title — use [Client.GetStorage] for full details.
type StorageListItem struct {
ID string `json:"id"`
Title string `json:"title"`
}
// StorageDetail is returned by [Client.GetStorage].
type StorageDetail struct {
Data string `json:"data"`
ID string `json:"id"`
Title string `json:"title"`
UID string `json:"uid"`
}
// StorageParams are the parameters for [Client.AddStorage].
type StorageParams struct {
Title string `json:"title"`
Region string `json:"region"`
AccessKeyID string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Bucket string `json:"bucket"`
Endpoint *string `json:"endpoint,omitempty"`
}
// StorageCreateResponse is returned by [Client.AddStorage] on success.
type StorageCreateResponse struct {
Title string `json:"title"`
ID string `json:"id"`
}
// StorageNotAllowedResponse is returned when the user's plan does not
// support custom storage. Note: this comes as HTTP 200, not an error status.
type StorageNotAllowedResponse struct {
Allowed bool `json:"allowed"`
Reason string `json:"reason"`
StatusCode int `json:"statusCode"`
}
// AccountInfo contains account usage and subscription information.
type AccountInfo struct {
Email string `json:"-"`
CallsLimit int `json:"-"`
Calls int `json:"-"`
StorageUsed int `json:"-"`
StorageLimit int `json:"-"`
Subscription string `json:"-"`
BytesTotal int `json:"-"`
BytesLimit int `json:"-"`
ComputeTimeTotal int `json:"-"`
ComputeTimeLimit int `json:"-"`
SubType string `json:"-"`
UID string `json:"-"`
Credit *float64 `json:"-"`
SubActive bool `json:"-"`
Extra map[string]interface{} `json:"-"`
}
// UnmarshalJSON implements custom unmarshaling for AccountInfo.
// Known fields are extracted into typed struct fields; remaining fields
// are collected into the Extra map.
func (a *AccountInfo) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return fmt.Errorf("unmarshaling AccountInfo: %w", err)
}
known := map[string]bool{
"email": true, "callsLimit": true, "calls": true,
"storageUsed": true, "storageLimit": true, "subscription": true,
"bytesTotal": true, "bytesLimit": true, "computeTimeTotal": true,
"computeTimeLimit": true, "subType": true, "uid": true,
"credit": true, "subActive": true,
}
unmarshalString := func(key string) string {
if v, ok := raw[key]; ok {
var s string
if json.Unmarshal(v, &s) == nil {
return s
}
}
return ""
}
unmarshalInt := func(key string) int {
if v, ok := raw[key]; ok {
var n float64
if json.Unmarshal(v, &n) == nil {
return int(math.Round(n))
}
}
return 0
}
unmarshalBool := func(key string) bool {
if v, ok := raw[key]; ok {
var b bool
if json.Unmarshal(v, &b) == nil {
return b
}
}
return false
}
a.Email = unmarshalString("email")
a.CallsLimit = unmarshalInt("callsLimit")
a.Calls = unmarshalInt("calls")
a.StorageUsed = unmarshalInt("storageUsed")
a.StorageLimit = unmarshalInt("storageLimit")
a.Subscription = unmarshalString("subscription")
a.BytesTotal = unmarshalInt("bytesTotal")
a.BytesLimit = unmarshalInt("bytesLimit")
a.ComputeTimeTotal = unmarshalInt("computeTimeTotal")
a.ComputeTimeLimit = unmarshalInt("computeTimeLimit")
a.SubType = unmarshalString("subType")
a.UID = unmarshalString("uid")
a.SubActive = unmarshalBool("subActive")
if v, ok := raw["credit"]; ok {
var c float64
if json.Unmarshal(v, &c) == nil {
a.Credit = &c
}
}
a.Extra = make(map[string]interface{})
for key, val := range raw {
if !known[key] {
var v interface{}
if json.Unmarshal(val, &v) == nil {
a.Extra[key] = v
}
}
}
return nil
}
// MarshalJSON implements custom marshaling for AccountInfo.
func (a AccountInfo) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{
"email": a.Email,
"callsLimit": a.CallsLimit,
"calls": a.Calls,
"storageUsed": a.StorageUsed,
"storageLimit": a.StorageLimit,
"subscription": a.Subscription,
"bytesTotal": a.BytesTotal,
"bytesLimit": a.BytesLimit,
"computeTimeTotal": a.ComputeTimeTotal,
"computeTimeLimit": a.ComputeTimeLimit,
"subType": a.SubType,
"uid": a.UID,
"subActive": a.SubActive,
}
if a.Credit != nil {
m["credit"] = *a.Credit
}
for k, v := range a.Extra {
m[k] = v
}
return json.Marshal(m)
}
// StatusResponse is returned by [Client.GetStatus].
type StatusResponse struct {
Status string `json:"status"`
}
// PublicTemplate represents a public template from the CloudLayer.io gallery.
type PublicTemplate struct {
ID string `json:"id"`
Name *string `json:"name,omitempty"`
Type *string `json:"type,omitempty"`
Category *string `json:"category,omitempty"`
Tags *string `json:"tags,omitempty"`
Raw json.RawMessage `json:"-"`
}
// UnmarshalJSON implements custom unmarshaling for PublicTemplate.
// Known fields are extracted; the full raw JSON is preserved in Raw.
func (t *PublicTemplate) UnmarshalJSON(data []byte) error {
// Preserve the raw JSON
t.Raw = make(json.RawMessage, len(data))
copy(t.Raw, data)
// Extract known fields using an alias to avoid infinite recursion
type Alias PublicTemplate
var alias struct {
Alias
}
if err := json.Unmarshal(data, &alias); err != nil {
return err
}
t.ID = alias.ID
t.Name = alias.Name
t.Type = alias.Type
t.Category = alias.Category
t.Tags = alias.Tags
return nil
}
// ConversionResult wraps the response from a conversion endpoint.
type ConversionResult struct {
// Job is populated for v2 responses and v1 async responses.
Job *Job
// Data is populated for v1 synchronous responses (raw binary).
Data []byte
// Headers contains CloudLayer-specific response headers.
Headers *ResponseHeaders
// Status is the HTTP status code.
Status int
// Filename is from the Content-Disposition header (v1 only).
Filename string
}
// ResponseHeaders contains parsed cl-* response headers from the CloudLayer.io API.
type ResponseHeaders struct {
WorkerJobID *string `json:"cl-worker-job-id,omitempty"`
ClusterID *string `json:"cl-cluster-id,omitempty"`
Worker *string `json:"cl-worker,omitempty"`
Bandwidth *int `json:"cl-bandwidth,omitempty"`
ProcessTime *int `json:"cl-process-time,omitempty"`
CallsRemaining *int `json:"cl-calls-remaining,omitempty"`
ChargedTime *int `json:"cl-charged-time,omitempty"`
BandwidthCost *float64 `json:"cl-bandwidth-cost,omitempty"`
ProcessTimeCost *float64 `json:"cl-process-time-cost,omitempty"`
APICreditCost *float64 `json:"cl-api-credit-cost,omitempty"`
}