porkbacon/internal/porkbun/models.go
2026-01-26 21:45:35 +00:00

152 lines
3.8 KiB
Go

package porkbun
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
)
// PorkbunBadEngineeringAPIResponseType represents the great undecisiveness
// from the api endpoint in choosing one consistent data type for certain fields.
// Ranging from null, "1" and 0. Why not just use -1, 1, and 0!? or even just null, 1 and 0...
type PorkbunBadEngineeringAPIResponseType int
func (t *PorkbunBadEngineeringAPIResponseType) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) || len(b) == 0 {
*t = 0
return nil
}
s := string(b)
s = strings.Trim(s, "\"")
if s == "" {
*t = 0
return nil
}
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return fmt.Errorf("cannot parse porkbun bad engeineering api response type: %w", err)
}
*t = PorkbunBadEngineeringAPIResponseType(i)
return nil
}
type NullString struct {
Valid bool
Value string
}
func (ns *NullString) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) || len(b) == 0 {
ns.Valid = false
ns.Value = ""
return nil
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
ns.Valid = true
ns.Value = s
return nil
}
func (ns NullString) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.Value)
}
func (ns NullString) String() string {
if ns.Valid {
return ""
}
return ns.Value
}
// BaseRequest contains the authentication fields required for most Porkbun API calls.
type BaseRequest struct {
APIKey string `json:"apikey"`
SecretAPIKey string `json:"secretapikey"`
}
// BaseResponse contains the common fields returned by the Porkbun API.
type BaseResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
}
// PingRequest is used to verify credentials.
type PingRequest struct {
BaseRequest
}
// PingResponse is the response from the ping endpoint.
type PingResponse struct {
BaseResponse
YourIP string `json:"yourIp"`
}
// DomainListAllRequest is used for getting a list of all domains.
type DomainListAllRequest struct {
BaseRequest
Start int `json:"start"`
IncludeLabels string `json:"includeLabels,omitempty"`
}
// DomainListAllResponse is the response from the domain list all endpoint.
type DomainListAllResponse struct {
BaseResponse
Domains []Domain `json:"domains"`
}
// Domain represents a single domain returned by domain endpoints.
type Domain struct {
Domain string `json:"domain"`
Status string `json:"status"`
TLD string `json:"tld"`
CreateDate string `json:"createDate"`
ExpireDate string `json:"expireDate"`
SecurityLock PorkbunBadEngineeringAPIResponseType `json:"securityLock"`
WhoIsPrivacy PorkbunBadEngineeringAPIResponseType `json:"whoisPrivacy"`
AutoRenew PorkbunBadEngineeringAPIResponseType `json:"autoRenew"`
NotLocal PorkbunBadEngineeringAPIResponseType `json:"notLocal"`
Labels []DomainLabel `json:"labels,omitempty"`
}
// DomainLabel represents a label associated with a domain.
type DomainLabel struct {
ID string `json:"id"`
Title string `json:"title"`
Color string `json:"color"`
}
// DNSRecordsRequest is used to get a list of DNS records
type DNSRecordsRequest struct {
BaseRequest
}
// DNSRecord it is what it says it is.
type DNSRecord struct {
ID string `json:"id"`
Type string `json:"type"`
Content string `json:"content"`
Name string `json:"name,omitempty"`
TTL NullString `json:"ttl,omitempty"`
Priority NullString `json:"prio,omitempty"`
Notes NullString `json:"notes,omitempty"`
}
// DNSRecordsResponse it is what it says it is.
type DNSRecordsResponse struct {
BaseResponse
Records []DNSRecord `json:"records"`
}