Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

141 lignes
3.7KB

  1. package qcclient
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. type APIResponse[T any] struct {
  9. Status string `json:"status"`
  10. Data T `json:"data"`
  11. }
  12. type APIError struct {
  13. Status string `json:"status"`
  14. Message string `json:"message"`
  15. Code string `json:"code"`
  16. Details map[string]any `json:"details,omitempty"`
  17. }
  18. type Template struct {
  19. ID int64 `json:"id"`
  20. Name string `json:"name"`
  21. Description string `json:"description"`
  22. Locale string `json:"locale"`
  23. ThumbnailURL string `json:"thumbnailUrl"`
  24. TemplatePreviewURL string `json:"templatePreviewUrl"`
  25. Type string `json:"type"`
  26. PaletteReady Boolish `json:"paletteReady"`
  27. GlobalColors map[string]any `json:"globalColors"`
  28. TextsWithGlobalColors map[string]any `json:"textsWithGlobalColors"`
  29. TemplateHeadings []TemplateHeading `json:"templateHeadings,omitempty"`
  30. Raw map[string]interface{} `json:"-"`
  31. }
  32. type Boolish bool
  33. func (b Boolish) Bool() bool {
  34. return bool(b)
  35. }
  36. func (b Boolish) MarshalJSON() ([]byte, error) {
  37. return json.Marshal(bool(b))
  38. }
  39. func (b *Boolish) UnmarshalJSON(data []byte) error {
  40. trimmed := strings.TrimSpace(string(data))
  41. if trimmed == "" || strings.EqualFold(trimmed, "null") {
  42. *b = false
  43. return nil
  44. }
  45. var boolVal bool
  46. if err := json.Unmarshal(data, &boolVal); err == nil {
  47. *b = Boolish(boolVal)
  48. return nil
  49. }
  50. var numVal float64
  51. if err := json.Unmarshal(data, &numVal); err == nil {
  52. *b = Boolish(numVal != 0)
  53. return nil
  54. }
  55. var stringVal string
  56. if err := json.Unmarshal(data, &stringVal); err == nil {
  57. parsed, err := parseBoolishString(stringVal)
  58. if err != nil {
  59. return err
  60. }
  61. *b = Boolish(parsed)
  62. return nil
  63. }
  64. return fmt.Errorf("unsupported boolish value: %s", trimmed)
  65. }
  66. func parseBoolishString(v string) (bool, error) {
  67. normalized := strings.ToLower(strings.TrimSpace(v))
  68. switch normalized {
  69. case "", "0", "false", "f", "no", "n", "off":
  70. return false, nil
  71. case "1", "true", "t", "yes", "y", "on":
  72. return true, nil
  73. }
  74. numeric, err := strconv.ParseFloat(normalized, 64)
  75. if err == nil {
  76. return numeric != 0, nil
  77. }
  78. return false, fmt.Errorf("unsupported boolish string value: %q", v)
  79. }
  80. type TemplateHeading struct {
  81. TranslationKey string `json:"translationKey"`
  82. }
  83. type GenerateContentRequest struct {
  84. TemplateID int64 `json:"templateId"`
  85. GlobalData map[string]any `json:"globalData"`
  86. Empty bool `json:"empty"`
  87. ToneOfVoice string `json:"toneOfVoice,omitempty"`
  88. TargetAudience string `json:"targetAudience,omitempty"`
  89. CustomTemplateData map[string]any `json:"customTemplateData,omitempty"`
  90. }
  91. type GenerateContentData map[string]map[string]any
  92. type CreateSiteRequest struct {
  93. TemplateID int64 `json:"templateId"`
  94. GlobalData map[string]any `json:"globalData"`
  95. Content CreateSiteContent `json:"content"`
  96. }
  97. type CreateSiteContent struct {
  98. AIData map[string]map[string]any `json:"aiData"`
  99. }
  100. type CreateSiteResponseData struct {
  101. Status string `json:"status"`
  102. JobID int64 `json:"jobId"`
  103. }
  104. type JobStatusResult struct {
  105. SiteID int64 `json:"siteId"`
  106. PreviewURL string `json:"previewUrl"`
  107. }
  108. type JobStatusData struct {
  109. JobID int64 `json:"jobId"`
  110. Status string `json:"status"`
  111. CreatedAt int64 `json:"createdAt"`
  112. Result JobStatusResult `json:"result"`
  113. }
  114. type SiteEditorLoginData struct {
  115. LoginURL string `json:"loginUrl"`
  116. }