whmcsapi_types.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log/slog"
  7. "net/http"
  8. )
  9. type APIResponser interface {
  10. CheckDecodedResponse() error
  11. }
  12. // General API Response type. All API responses will have
  13. // A result field and if it's not success a message field
  14. type APIResponse struct {
  15. Result string `json:"result"`
  16. Message string `json:"message,omitempty"`
  17. }
  18. func DecodeWHMCSResponse[T any](response *http.Response) (decoded_response T, err error) {
  19. body, err := io.ReadAll(response.Body)
  20. if err != nil {
  21. slog.Error("Failed to read response", "body", body, "err", err.Error())
  22. return decoded_response, err
  23. }
  24. if err = json.Unmarshal(body, &decoded_response); err != nil {
  25. bodySmol := string(body[0:min(20, len(body))])
  26. if len(body) < 20 {
  27. bodySmol = fmt.Sprintf("%v...", bodySmol)
  28. }
  29. slog.Error("Failed to decode response from upstream server.",
  30. "body", body[0:min(20, len(body))],
  31. "responsecode", response.StatusCode,
  32. "error", err.Error(),
  33. )
  34. return decoded_response, err
  35. }
  36. return decoded_response, nil
  37. }
  38. func (r APIResponse) CheckDecodedResponse() error {
  39. if r.Result != "success" {
  40. slog.Error("Upstream server error", "message", r.Message)
  41. return fmt.Errorf("Got error from upstream: %v", r.Message)
  42. }
  43. return nil
  44. }
  45. // API Response data types. These exist just to be target data types for
  46. // unmarshalling.
  47. // API GetTickets
  48. // https://developers.whmcs.com/api-reference/gettickets/
  49. type GetTicketsResponse struct {
  50. APIResponse
  51. Tickets struct {
  52. Ticket []GetTicketsTicket `json:"ticket"`
  53. } `json:"tickets"`
  54. }
  55. type GetTicketsTicket struct {
  56. Id int `json:"id"`
  57. Tid string `json:"tid"`
  58. Deptid int `json:"deptid,omitempty"`
  59. Userid int `json:"userid,omitempty"`
  60. Name string `json:"name,omitempty"`
  61. Owner_name string `json:"owner_name,omitempty"`
  62. Email string `json:"email,omitempty"`
  63. Requestor_name string `json:"requestor_name,omitempty"`
  64. Requestor_email string `json:"requestor_email,omitempty"`
  65. Requestor_type string `json:"requestor_type,omitempty"`
  66. Cc string `json:"cc,omitempty"`
  67. C string `json:"c,omitempty"`
  68. Date string `json:"date,omitempty"`
  69. Subject string `json:"subject,omitempty"`
  70. Status string `json:"status,omitempty"`
  71. Priority string `json:"priority,omitempty"`
  72. Admin string `json:"admin,omitempty"`
  73. Lastreply string `json:"lastreply,omitempty"`
  74. Flag int `json:"flag,omitempty"`
  75. Service string `json:"service,omitempty"`
  76. }
  77. // API GetTicket
  78. // https://developers.whmcs.com/api-reference/getticket/
  79. type GetTicketNote struct {
  80. Noteid int `json:"noteid"`
  81. Date string `json:"date,omitempty"`
  82. Message string `json:"message,omitempty"`
  83. Admin string `json:"admin,omitempty"`
  84. }
  85. type GetTicketReply struct {
  86. Replyid json.Number `json:"replyid"`
  87. Userid int `json:"userid"`
  88. Contactid int `json:"contactid,omitempty"`
  89. Name string `json:"name,omitempty"`
  90. Email string `json:"email,omitempty"`
  91. Requestor_name string `json:"requestor_name,omitempty"`
  92. Requestor_email string `json:"requestor_email,omitempty"`
  93. Requestor_type string `json:"requestor_type,omitempty"`
  94. Date string `json:"date,omitempty"`
  95. Message string `json:"message,omitempty"`
  96. Admin string `json:"admin,omitempty"`
  97. Rating json.Number `json:"rating,omitempty"`
  98. }
  99. type GetTicketResponse struct {
  100. APIResponse
  101. Ticketid int `json:"ticketid"`
  102. Tid string `json:"tid,omitempty"`
  103. C string `json:"c,omitempty"`
  104. Deptid int `json:"deptid,omitempty"`
  105. Deptname string `json:"deptname,omitempty"`
  106. Userid int `json:"userid,omitempty"`
  107. Contactid int `json:"contactid,omitempty"`
  108. Name string `json:"name,omitempty"`
  109. Email string `json:"email,omitempty"`
  110. Requestor_name string `json:"requestor_name,omitempty"`
  111. Requestor_type string `json:"requestor_type,omitempty"`
  112. Requestor_email string `json:"requestor_email,omitempty"`
  113. Cc string `json:"cc,omitempty"`
  114. Date string `json:"date,omitempty"`
  115. Subject string `json:"subject,omitempty"`
  116. Status string `json:"status,omitempty"`
  117. Priority string `json:"priority,omitempty"`
  118. Admin string `json:"admin,omitempty"`
  119. Lastreply string `json:"lastreply,omitempty"`
  120. Flag int `json:"flag,omitempty"`
  121. Service string `json:"service,omitempty"`
  122. Replies struct {
  123. Reply []GetTicketReply `json:"reply"`
  124. } `json:"replies"`
  125. Notes []any `json:"notes,omitempty"`
  126. }