git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/mailgun/domains.go (about)

     1  package mailgun
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  type SpamAction string
    10  
    11  // Use these to specify a spam action when creating a new domain.
    12  const (
    13  	// Tag the received message with headers providing a measure of its spamness.
    14  	SpamActionTag = SpamAction("tag")
    15  	// Prevents Mailgun from taking any action on what it perceives to be spam.
    16  	SpamActionDisabled = SpamAction("disabled")
    17  	// instructs Mailgun to just block or delete the message all-together.
    18  	SpamActionDelete = SpamAction("delete")
    19  
    20  	DKIMKeySize1024 = 1024
    21  	DKIMKeySize2048 = 2048
    22  )
    23  
    24  // A Domain structure holds information about a domain used when sending mail.
    25  type Domain struct {
    26  	CreatedAt    RFC2822Time `json:"created_at"`
    27  	SMTPLogin    string      `json:"smtp_login"`
    28  	Name         string      `json:"name"`
    29  	SMTPPassword string      `json:"smtp_password"`
    30  	Wildcard     bool        `json:"wildcard"`
    31  	SpamAction   SpamAction  `json:"spam_action"`
    32  	State        string      `json:"state"`
    33  }
    34  
    35  // DNSRecord structures describe intended records to properly configure your domain for use with Mailgun.
    36  // Note that Mailgun does not host DNS records.
    37  type DNSRecord struct {
    38  	Priority   string
    39  	RecordType string `json:"record_type"`
    40  	Valid      string
    41  	Name       string
    42  	Value      string
    43  }
    44  
    45  type DomainResponse struct {
    46  	Domain              Domain      `json:"domain"`
    47  	ReceivingDNSRecords []DNSRecord `json:"receiving_dns_records"`
    48  	SendingDNSRecords   []DNSRecord `json:"sending_dns_records"`
    49  }
    50  
    51  type domainConnectionResponse struct {
    52  	Connection DomainConnection `json:"connection"`
    53  }
    54  
    55  type domainsListResponse struct {
    56  	// is -1 if Next() or First() have not been called
    57  	TotalCount int      `json:"total_count"`
    58  	Items      []Domain `json:"items"`
    59  }
    60  
    61  // Specify the domain connection options
    62  type DomainConnection struct {
    63  	RequireTLS       bool `json:"require_tls"`
    64  	SkipVerification bool `json:"skip_verification"`
    65  }
    66  
    67  // Specify the domain tracking options
    68  type DomainTracking struct {
    69  	Click       TrackingStatus `json:"click"`
    70  	Open        TrackingStatus `json:"open"`
    71  	Unsubscribe TrackingStatus `json:"unsubscribe"`
    72  }
    73  
    74  // The tracking status of a domain
    75  type TrackingStatus struct {
    76  	Active     bool   `json:"active"`
    77  	HTMLFooter string `json:"html_footer"`
    78  	TextFooter string `json:"text_footer"`
    79  }
    80  
    81  type domainTrackingResponse struct {
    82  	Tracking DomainTracking `json:"tracking"`
    83  }
    84  
    85  ////////////////////////////////////////////////////////////////////////////////////////////////////
    86  // Requests
    87  ////////////////////////////////////////////////////////////////////////////////////////////////////
    88  
    89  // Optional parameters when creating a domain
    90  type CreateDomainInput struct {
    91  	Name               string      `json:"name"`
    92  	SMTPPassword       *string     `json:"smtp_password"`
    93  	SpamAction         *SpamAction `json:"spam_action"`
    94  	Wildcard           *bool       `json:"wildcard"`
    95  	ForceDKIMAuthority *bool       `json:"force_dkim_authority"`
    96  	DKIMKeySize        *int        `json:"dkim_key_size"`
    97  	IPS                []string    `json:"ips"`
    98  	PoolID             *string     `json:"pool_id"`
    99  	WebScheme          *string     `json:"web_scheme"`
   100  }
   101  
   102  type UpdateDomainDKIMSelectorInput struct {
   103  	Domain       string `json:"-"`
   104  	DKIMSelector string `json:"dkim_selector"`
   105  }
   106  
   107  ////////////////////////////////////////////////////////////////////////////////////////////////////
   108  // API Methods
   109  ////////////////////////////////////////////////////////////////////////////////////////////////////
   110  
   111  func (client *Client) CreateDomain(ctx context.Context, input CreateDomainInput) (res Domain, err error) {
   112  	err = client.request(ctx, requestParams{
   113  		Payload: input,
   114  		Method:  http.MethodPost,
   115  		URL:     "/v4/domains",
   116  	}, &res)
   117  
   118  	return
   119  }
   120  
   121  func (client *Client) DeleteDomain(ctx context.Context, domain string) (err error) {
   122  	err = client.request(ctx, requestParams{
   123  		Payload: nil,
   124  		Method:  http.MethodDelete,
   125  		URL:     fmt.Sprintf("/v4/domains/%s", domain),
   126  	}, nil)
   127  
   128  	return
   129  }
   130  
   131  func (client *Client) UpdateDomainDKIMSelector(ctx context.Context, input UpdateDomainDKIMSelectorInput) (err error) {
   132  	err = client.request(ctx, requestParams{
   133  		Payload: input,
   134  		Method:  http.MethodPut,
   135  		URL:     fmt.Sprintf("/v4/domains/%s/dkim_selector", input.Domain),
   136  	}, nil)
   137  
   138  	return
   139  }
   140  
   141  func (client *Client) GetDomain(ctx context.Context, domain string) (res DomainResponse, err error) {
   142  	err = client.request(ctx, requestParams{
   143  		Method: http.MethodGet,
   144  		URL:    fmt.Sprintf("/v4/domains/%s", domain),
   145  	}, &res)
   146  
   147  	return
   148  }