package main import ( "encoding/json" "fmt" "io" "log/slog" "net/http" ) type APIResponser interface { CheckDecodedResponse() error } // General API Response type. All API responses will have // A result field and if it's not success a message field type APIResponse struct { Result string `json:"result"` Message string `json:"message,omitempty"` } func DecodeWHMCSResponse[T any](response *http.Response) (decoded_response T, err error) { body, err := io.ReadAll(response.Body) if err != nil { slog.Error("Failed to read response", "body", body, "err", err.Error()) return decoded_response, err } if err = json.Unmarshal(body, &decoded_response); err != nil { bodySmol := string(body[0:min(20, len(body))]) if len(body) < 20 { bodySmol = fmt.Sprintf("%v...", bodySmol) } slog.Error("Failed to decode response from upstream server.", "body", body[0:min(20, len(body))], "responsecode", response.StatusCode, "error", err.Error(), ) return decoded_response, err } return decoded_response, nil } func (r APIResponse) CheckDecodedResponse() error { if r.Result != "success" { slog.Error("Upstream server error", "message", r.Message) return fmt.Errorf("Got error from upstream: %v", r.Message) } return nil } // API Response data types. These exist just to be target data types for // unmarshalling. // API GetTickets // https://developers.whmcs.com/api-reference/gettickets/ type GetTicketsResponse struct { APIResponse Tickets struct { Ticket []GetTicketsTicket `json:"ticket"` } `json:"tickets"` } type GetTicketsTicket struct { Id int `json:"id"` Tid string `json:"tid"` Deptid int `json:"deptid,omitempty"` Userid int `json:"userid,omitempty"` Name string `json:"name,omitempty"` Owner_name string `json:"owner_name,omitempty"` Email string `json:"email,omitempty"` Requestor_name string `json:"requestor_name,omitempty"` Requestor_email string `json:"requestor_email,omitempty"` Requestor_type string `json:"requestor_type,omitempty"` Cc string `json:"cc,omitempty"` C string `json:"c,omitempty"` Date string `json:"date,omitempty"` Subject string `json:"subject,omitempty"` Status string `json:"status,omitempty"` Priority string `json:"priority,omitempty"` Admin string `json:"admin,omitempty"` Lastreply string `json:"lastreply,omitempty"` Flag int `json:"flag,omitempty"` Service string `json:"service,omitempty"` } // API GetTicket // https://developers.whmcs.com/api-reference/getticket/ type GetTicketNote struct { Noteid int `json:"noteid"` Date string `json:"date,omitempty"` Message string `json:"message,omitempty"` Admin string `json:"admin,omitempty"` } type GetTicketReply struct { Replyid json.Number `json:"replyid"` Userid int `json:"userid"` Contactid int `json:"contactid,omitempty"` Name string `json:"name,omitempty"` Email string `json:"email,omitempty"` Requestor_name string `json:"requestor_name,omitempty"` Requestor_email string `json:"requestor_email,omitempty"` Requestor_type string `json:"requestor_type,omitempty"` Date string `json:"date,omitempty"` Message string `json:"message,omitempty"` Admin string `json:"admin,omitempty"` Rating json.Number `json:"rating,omitempty"` } type GetTicketResponse struct { APIResponse Ticketid int `json:"ticketid"` Tid string `json:"tid,omitempty"` C string `json:"c,omitempty"` Deptid int `json:"deptid,omitempty"` Deptname string `json:"deptname,omitempty"` Userid int `json:"userid,omitempty"` Contactid int `json:"contactid,omitempty"` Name string `json:"name,omitempty"` Email string `json:"email,omitempty"` Requestor_name string `json:"requestor_name,omitempty"` Requestor_type string `json:"requestor_type,omitempty"` Requestor_email string `json:"requestor_email,omitempty"` Cc string `json:"cc,omitempty"` Date string `json:"date,omitempty"` Subject string `json:"subject,omitempty"` Status string `json:"status,omitempty"` Priority string `json:"priority,omitempty"` Admin string `json:"admin,omitempty"` Lastreply string `json:"lastreply,omitempty"` Flag int `json:"flag,omitempty"` Service string `json:"service,omitempty"` Replies struct { Reply []GetTicketReply `json:"reply"` } `json:"replies"` Notes []any `json:"notes,omitempty"` }