1package dnsimple
2
3import (
4	"context"
5	"fmt"
6)
7
8// DomainPush represents a domain push in DNSimple.
9type DomainPush struct {
10	ID         int64  `json:"id,omitempty"`
11	DomainID   int64  `json:"domain_id,omitempty"`
12	ContactID  int64  `json:"contact_id,omitempty"`
13	AccountID  int64  `json:"account_id,omitempty"`
14	CreatedAt  string `json:"created_at,omitempty"`
15	UpdatedAt  string `json:"updated_at,omitempty"`
16	AcceptedAt string `json:"accepted_at,omitempty"`
17}
18
19func domainPushPath(accountID string, pushID int64) (path string) {
20	path = fmt.Sprintf("/%v/pushes", accountID)
21	if pushID != 0 {
22		path += fmt.Sprintf("/%v", pushID)
23	}
24	return
25}
26
27// DomainPushResponse represents a response from an API method that returns a DomainPush struct.
28type DomainPushResponse struct {
29	Response
30	Data *DomainPush `json:"data"`
31}
32
33// DomainPushesResponse represents a response from an API method that returns a collection of DomainPush struct.
34type DomainPushesResponse struct {
35	Response
36	Data []DomainPush `json:"data"`
37}
38
39// DomainPushAttributes represent a domain push payload (see initiate).
40type DomainPushAttributes struct {
41	NewAccountEmail string `json:"new_account_email,omitempty"`
42	ContactID       int64  `json:"contact_id,omitempty"`
43}
44
45// InitiatePush initiate a new domain push.
46//
47// See https://developer.dnsimple.com/v2/domains/pushes/#initiateDomainPush
48func (s *DomainsService) InitiatePush(ctx context.Context, accountID, domainID string, pushAttributes DomainPushAttributes) (*DomainPushResponse, error) {
49	path := versioned(fmt.Sprintf("/%v/pushes", domainPath(accountID, domainID)))
50	pushResponse := &DomainPushResponse{}
51
52	resp, err := s.client.post(ctx, path, pushAttributes, pushResponse)
53	if err != nil {
54		return nil, err
55	}
56
57	pushResponse.HTTPResponse = resp
58	return pushResponse, nil
59}
60
61// ListPushes lists the pushes for an account.
62//
63// See https://developer.dnsimple.com/v2/domains/pushes/#listPushes
64func (s *DomainsService) ListPushes(ctx context.Context, accountID string, options *ListOptions) (*DomainPushesResponse, error) {
65	path := versioned(domainPushPath(accountID, 0))
66	pushesResponse := &DomainPushesResponse{}
67
68	path, err := addURLQueryOptions(path, options)
69	if err != nil {
70		return nil, err
71	}
72
73	resp, err := s.client.get(ctx, path, pushesResponse)
74	if err != nil {
75		return nil, err
76	}
77
78	pushesResponse.HTTPResponse = resp
79	return pushesResponse, nil
80}
81
82// AcceptPush accept a push for a domain.
83//
84// See https://developer.dnsimple.com/v2/domains/pushes/#acceptPush
85func (s *DomainsService) AcceptPush(ctx context.Context, accountID string, pushID int64, pushAttributes DomainPushAttributes) (*DomainPushResponse, error) {
86	path := versioned(domainPushPath(accountID, pushID))
87	pushResponse := &DomainPushResponse{}
88
89	resp, err := s.client.post(ctx, path, pushAttributes, nil)
90	if err != nil {
91		return nil, err
92	}
93
94	pushResponse.HTTPResponse = resp
95	return pushResponse, nil
96}
97
98// RejectPush reject a push for a domain.
99//
100// See https://developer.dnsimple.com/v2/domains/pushes/#rejectPush
101func (s *DomainsService) RejectPush(ctx context.Context, accountID string, pushID int64) (*DomainPushResponse, error) {
102	path := versioned(domainPushPath(accountID, pushID))
103	pushResponse := &DomainPushResponse{}
104
105	resp, err := s.client.delete(ctx, path, nil, nil)
106	if err != nil {
107		return nil, err
108	}
109
110	pushResponse.HTTPResponse = resp
111	return pushResponse, nil
112}
113