-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_client.go
More file actions
615 lines (556 loc) · 17.9 KB
/
api_client.go
File metadata and controls
615 lines (556 loc) · 17.9 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// BackendTemplate is a PDF template from the anysubscriptions backend.
type BackendTemplate struct {
ID string `json:"id"` // UUID
Name string `json:"name"`
Description string `json:"description"`
Categoria string `json:"categoria"`
Icon string `json:"icon"`
ThermalPrintable int `json:"thermal_printable"`
ConfigAdicional string `json:"config_adicional,omitempty"` // JSON string
}
// FieldInfo describes a template field with its name and type (legacy, kept for compat).
type FieldInfo struct {
Name string `json:"name"`
Type string `json:"type"` // e.g. "multiVariableText", "qrcode", "image", "text"
}
// FieldDetail describes a template field with full pdfme layout information.
type FieldDetail struct {
Name string `json:"name"`
Type string `json:"type"`
X float64 `json:"x"`
Y float64 `json:"y"`
Width float64 `json:"width"`
Height float64 `json:"height"`
FontSize float64 `json:"font_size,omitempty"`
Alignment string `json:"alignment,omitempty"`
Variables []string `json:"variables,omitempty"` // variable names for mapping (multiVariableText or {placeholder})
}
// BackendTemplateDetail includes the pdfme content and thermal config.
type BackendTemplateDetail struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Categoria string `json:"categoria"`
ThermalPrintable int `json:"thermal_printable"`
Fields []string `json:"fields"` // field names (backward compat)
FieldsTyped []FieldDetail `json:"fields_typed"` // fields with types + positions
Schema json.RawMessage `json:"schema,omitempty"` // raw pdfme content for preview
ThermalConfig *ThermalConfig `json:"thermal_config,omitempty"`
}
// ThermalConfig is the thermal printing configuration from config_adicional.
type ThermalConfig struct {
Layout string `json:"layout"` // "single" or "matrix_3x1"
Matrix *MatrixConfig `json:"matrix,omitempty"`
}
// MatrixConfig defines multi-column label layout.
type MatrixConfig struct {
Columns int `json:"columns"`
ColOffsets []int `json:"col_offsets"` // dots
TotalWidthMm int `json:"total_width_mm"`
}
// TsplResponse is the response from the backend TSPL generation endpoint.
type TsplResponse struct {
Commands string `json:"commands"`
CommandsArray []string `json:"commands_array"`
SizeBytes int `json:"size_bytes"`
Copies int `json:"copies"`
Layout string `json:"layout"`
}
// ApiClient communicates with the anysubscriptions backend.
type ApiClient struct {
baseURL string
bearerVal string // "KEY:SECRET" or "eyJ..." JWT
wlID string // White Label ID header value
httpClient *http.Client
}
// NewApiClient creates a client from the current config.
// Supports two auth modes:
// - API Key:Secret → Bearer KEY:SECRET (detected by presence of api_key + api_secret)
// - JWT Token → Bearer eyJ... (fallback to api_token)
func NewApiClient(cfg AppConfig) *ApiClient {
bearer := cfg.ApiToken
if cfg.ApiKey != "" && cfg.ApiSecret != "" {
bearer = cfg.ApiKey + ":" + cfg.ApiSecret
}
wl := "20" // default ISI Hospital
if cfg.ApiWhiteLabel > 0 {
wl = fmt.Sprintf("%d", cfg.ApiWhiteLabel)
}
return &ApiClient{
baseURL: cfg.ApiURL,
bearerVal: bearer,
wlID: wl,
httpClient: &http.Client{
Timeout: 15 * time.Second,
},
}
}
func (c *ApiClient) doGet(path string) ([]byte, error) {
url := c.baseURL + path
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.bearerVal)
req.Header.Set("X-Any-Wl", c.wlID)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
}
return body, nil
}
func (c *ApiClient) doPost(path string, payload any) ([]byte, error) {
url := c.baseURL + path
jsonBody, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.bearerVal)
req.Header.Set("X-Any-Wl", c.wlID)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
}
return body, nil
}
// TestConnection verifies the backend is reachable and authenticated.
func (c *ApiClient) TestConnection() error {
// Use pdf-templates/all as health check since /status may not need auth
body, err := c.doGet("/pdf-templates/all?limit=1")
if err != nil {
return err
}
var resp struct {
Status int `json:"status"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return fmt.Errorf("invalid response: %w", err)
}
if resp.Status != 1 {
return fmt.Errorf("API returned status %d", resp.Status)
}
return nil
}
// BrandInfo contains branding information from the backend.
type BrandInfo struct {
BrandName string `json:"brandName"`
BrandLogo string `json:"brandLogo"`
BrandURL string `json:"brandUrl"`
StoreName string `json:"storeName"`
StoreLogo string `json:"storeLogo"`
WLName string `json:"whiteLabelName"`
WLLogo string `json:"whiteLabelLogo"`
WLDomain string `json:"whiteLabelDomain"`
PrimaryColor string `json:"primary"`
SecondaryColor string `json:"secondary"`
}
// FetchBrandInfo returns brand/whitelabel info for the authenticated user.
// GET /users/brand-info
func (c *ApiClient) FetchBrandInfo() (*BrandInfo, error) {
body, err := c.doGet("/users/brand-info")
if err != nil {
return nil, err
}
var resp struct {
Status int `json:"status"`
Data struct {
BrandName string `json:"brandName"`
BrandLogo string `json:"brandLogo"`
BrandURL string `json:"brandUrl"`
StoreName string `json:"storeName"`
StoreLogo string `json:"storeLogo"`
WLName string `json:"whiteLabelName"`
WLLogo string `json:"whiteLabelLogo"`
WLDomain string `json:"whiteLabelDomain"`
Colors struct {
Primary string `json:"primary"`
Secondary string `json:"secondary"`
} `json:"colors"`
} `json:"data"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("parse brand info: %w", err)
}
d := resp.Data
return &BrandInfo{
BrandName: d.BrandName,
BrandLogo: d.BrandLogo,
BrandURL: d.BrandURL,
StoreName: d.StoreName,
StoreLogo: d.StoreLogo,
WLName: d.WLName,
WLLogo: d.WLLogo,
WLDomain: d.WLDomain,
PrimaryColor: d.Colors.Primary,
SecondaryColor: d.Colors.Secondary,
}, nil
}
// FetchTemplates returns all PDF templates from the backend.
// GET /pdf-templates/all
func (c *ApiClient) FetchTemplates() ([]BackendTemplate, error) {
body, err := c.doGet("/pdf-templates/all?limit=100")
if err != nil {
return nil, err
}
var resp struct {
Status int `json:"status"`
Data struct {
Templates []BackendTemplate `json:"templates"`
} `json:"data"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("parse templates: %w", err)
}
return resp.Data.Templates, nil
}
// FetchTemplateFields returns the field names for a template.
// GET /pdf-templates/{uuid}/fields
func (c *ApiClient) FetchTemplateFields(templateID string) ([]string, error) {
body, err := c.doGet("/pdf-templates/" + templateID + "/fields")
if err != nil {
return nil, err
}
var resp struct {
Status int `json:"status"`
Data struct {
Fields []string `json:"fields"`
} `json:"data"`
}
if err := json.Unmarshal(body, &resp); err != nil {
// Fallback: try parsing as direct array
var resp2 struct {
Status int `json:"status"`
Data []string `json:"data"`
}
if err2 := json.Unmarshal(body, &resp2); err2 != nil {
return nil, fmt.Errorf("parse fields: %w", err)
}
return resp2.Data, nil
}
return resp.Data.Fields, nil
}
// FetchTemplateDetail returns a template with its schema content.
// GET /pdf-templates/{uuid}
//
// Uses two-pass unmarshal: pass 1 extracts metadata/fields (order irrelevant),
// pass 2 preserves raw JSON bytes for the schema so key order (= z-order in PDF)
// is maintained. Without this, Go's map[string]any alphabetizes keys and
// guilloche patterns render ON TOP of text fields.
func (c *ApiClient) FetchTemplateDetail(templateID string) (*BackendTemplateDetail, error) {
body, err := c.doGet("/pdf-templates/" + templateID)
if err != nil {
return nil, err
}
// Pass 1: Extract metadata and fields (order doesn't matter for these)
var resp struct {
Status int `json:"status"`
Data struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Categoria string `json:"categoria"`
ThermalPrintable int `json:"thermal_printable"`
ConfigAdicional any `json:"config_adicional"`
Content any `json:"content"` // pdfme schema — used only for field extraction
} `json:"data"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("parse template: %w", err)
}
detail := &BackendTemplateDetail{
ID: resp.Data.ID,
Name: resp.Data.Name,
Description: resp.Data.Description,
Categoria: resp.Data.Categoria,
ThermalPrintable: resp.Data.ThermalPrintable,
}
// Extract fields from pdfme schema content (uses map[string]any, order irrelevant)
if resp.Data.Content != nil {
detail.Fields, detail.FieldsTyped = extractPdfmeFieldsDetailed(resp.Data.Content)
}
// Pass 2: Preserve raw JSON bytes for schema (ORDER MATTERS for z-order!)
// json.RawMessage keeps the original byte sequence without re-marshaling through map[string]any
var rawResp struct {
Data struct {
Content json.RawMessage `json:"content"`
} `json:"data"`
}
if err := json.Unmarshal(body, &rawResp); err == nil && len(rawResp.Data.Content) > 0 {
detail.Schema = rawResp.Data.Content // Direct assignment, NO re-marshal!
}
// Extract thermal_config from config_adicional
detail.ThermalConfig = extractThermalConfig(resp.Data.ConfigAdicional)
return detail, nil
}
// GenerateTSPL calls the backend to generate TSPL commands from a template + data.
// POST /pdfs/generate-tspl-commands
func (c *ApiClient) GenerateTSPL(templateID string, data map[string]string, copies int, layout string, preset string) (*TsplResponse, error) {
payload := map[string]any{
"template_id": templateID,
"data": data,
"copies": copies,
}
if layout != "" {
payload["layout"] = layout
}
if preset != "" {
payload["preset"] = preset
}
body, err := c.doPost("/pdfs/generate-tspl-commands", payload)
if err != nil {
return nil, err
}
var resp struct {
Status int `json:"status"`
Data TsplResponse `json:"data"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("parse tspl response: %w", err)
}
if resp.Status != 1 {
return nil, fmt.Errorf("TSPL generation failed (status %d)", resp.Status)
}
return &resp.Data, nil
}
// extractPdfmeFieldsDetailed reads field names, types, positions, dimensions, and variables from a pdfme schema.
// Supports both v4 (keyed objects) and v5 (array with name property) formats.
// Variables are extracted so the dashboard can map at the variable level (not field name level).
func extractPdfmeFieldsDetailed(content any) ([]string, []FieldDetail) {
contentMap, ok := content.(map[string]any)
if !ok {
return nil, nil
}
schemas, ok := contentMap["schemas"].([]any)
if !ok || len(schemas) == 0 {
return nil, nil
}
// Only these types are mappable data fields (skip line, rectangle, image, etc.)
mappableTypes := map[string]bool{"text": true, "multiVariableText": true, "qrcode": true, "barcode": true}
seen := map[string]bool{}
var names []string
var detailed []FieldDetail
for _, page := range schemas {
switch p := page.(type) {
case []any:
// v5 format: array of objects with "name", "type", "position", "width", "height"
for _, elem := range p {
obj, ok := elem.(map[string]any)
if !ok {
continue
}
name, _ := obj["name"].(string)
fieldType, _ := obj["type"].(string)
if name == "" || seen[name] {
continue
}
if !mappableTypes[fieldType] {
continue
}
seen[name] = true
names = append(names, name)
fd := FieldDetail{Name: name}
fd.Type, _ = obj["type"].(string)
fd.Width, _ = obj["width"].(float64)
fd.Height, _ = obj["height"].(float64)
fd.FontSize, _ = obj["fontSize"].(float64)
fd.Alignment, _ = obj["alignment"].(string)
if pos, ok := obj["position"].(map[string]any); ok {
fd.X, _ = pos["x"].(float64)
fd.Y, _ = pos["y"].(float64)
}
fd.Variables = extractFieldVariables(obj)
detailed = append(detailed, fd)
}
case map[string]any:
// v4 format: keyed objects where value may have "type"
for key, val := range p {
if seen[key] {
continue
}
fd := FieldDetail{Name: key}
if obj, ok := val.(map[string]any); ok {
fd.Type, _ = obj["type"].(string)
if !mappableTypes[fd.Type] {
continue
}
fd.Width, _ = obj["width"].(float64)
fd.Height, _ = obj["height"].(float64)
fd.FontSize, _ = obj["fontSize"].(float64)
fd.Alignment, _ = obj["alignment"].(string)
if pos, ok := obj["position"].(map[string]any); ok {
fd.X, _ = pos["x"].(float64)
fd.Y, _ = pos["y"].(float64)
}
fd.Variables = extractFieldVariables(obj)
} else if !mappableTypes[fd.Type] {
continue
}
seen[key] = true
names = append(names, key)
detailed = append(detailed, fd)
}
}
}
return names, detailed
}
// extractFieldVariables extracts the mappable variable names from a pdfme field object.
// Handles:
// - multiVariableText/text with "variables" array (e.g. ["gafete.nombre", "gafete.apellido"])
// - qrcode/barcode/text with {placeholder} in content (e.g. "{gafete.token}")
// - table with body as variable string (e.g. "{medicamentos_tabla}")
func extractFieldVariables(obj map[string]any) []string {
// Any field type can have an explicit variables array (multiVariableText, text with ISI custom vars, table)
if vars, ok := obj["variables"].([]any); ok && len(vars) > 0 {
var result []string
for _, v := range vars {
if s, ok := v.(string); ok && s != "" {
result = append(result, s)
}
}
if len(result) > 0 {
return result
}
}
// Check text template for {placeholder} patterns (e.g. text: "{gafete.nombre}")
if text, ok := obj["text"].(string); ok && text != "" {
vars := extractAllPlaceholders(text)
if len(vars) > 0 {
return vars
}
}
// Check content for single {placeholder} (qrcode, barcode, text without variables array)
if content, ok := obj["content"].(string); ok && content != "" {
if ph := extractSinglePlaceholder(content); ph != "" {
return []string{ph}
}
}
// Check table body for variable reference
if body, ok := obj["body"].(string); ok && body != "" {
if ph := extractSinglePlaceholder(body); ph != "" {
return []string{ph}
}
}
return nil
}
// extractAllPlaceholders finds all {varName} patterns in a text string.
func extractAllPlaceholders(text string) []string {
var result []string
seen := map[string]bool{}
remaining := text
for {
start := strings.Index(remaining, "{")
if start == -1 {
break
}
end := strings.Index(remaining[start:], "}")
if end == -1 {
break
}
inner := remaining[start+1 : start+end]
remaining = remaining[start+end+1:]
// Skip JSON-like patterns and Handlebars conditionals
if strings.ContainsAny(inner, "\":# ") {
continue
}
if inner != "" && !seen[inner] {
seen[inner] = true
result = append(result, inner)
}
}
return result
}
// extractSinglePlaceholder returns the variable name if the string is exactly "{varName}".
func extractSinglePlaceholder(s string) string {
s = strings.TrimSpace(s)
if len(s) < 3 || s[0] != '{' || s[len(s)-1] != '}' {
return ""
}
inner := s[1 : len(s)-1]
// Must not contain spaces, braces, or colons (which would indicate JSON)
if strings.ContainsAny(inner, " {}:\"") {
return ""
}
return inner
}
// extractThermalConfig parses config_adicional to get thermal_config.
func extractThermalConfig(configAdicional any) *ThermalConfig {
if configAdicional == nil {
return nil
}
var cfgMap map[string]any
switch v := configAdicional.(type) {
case map[string]any:
cfgMap = v
case string:
if v == "" {
return nil
}
if err := json.Unmarshal([]byte(v), &cfgMap); err != nil {
return nil
}
default:
return nil
}
tcRaw, ok := cfgMap["thermal_config"]
if !ok {
return nil
}
tcMap, ok := tcRaw.(map[string]any)
if !ok {
return nil
}
tc := &ThermalConfig{}
if layout, ok := tcMap["layout"].(string); ok {
tc.Layout = layout
}
if matrixRaw, ok := tcMap["matrix"].(map[string]any); ok {
tc.Matrix = &MatrixConfig{}
if cols, ok := matrixRaw["columns"].(float64); ok {
tc.Matrix.Columns = int(cols)
}
if tw, ok := matrixRaw["total_width_mm"].(float64); ok {
tc.Matrix.TotalWidthMm = int(tw)
}
if offsets, ok := matrixRaw["col_offsets"].([]any); ok {
for _, o := range offsets {
if v, ok := o.(float64); ok {
tc.Matrix.ColOffsets = append(tc.Matrix.ColOffsets, int(v))
}
}
}
}
return tc
}