File: domains.go

package info (click to toggle)
golang-github-weppos-dnsimple-go 0.0~git20160204.0.65c1ca7-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: makefile: 2
file content (121 lines) | stat: -rw-r--r-- 3,350 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package dnsimple

import (
	"fmt"
	"time"
)

// DomainsService handles communication with the domain related
// methods of the DNSimple API.
//
// DNSimple API docs: http://developer.dnsimple.com/domains/
type DomainsService struct {
	client *Client
}

type Domain struct {
	Id             int        `json:"id,omitempty"`
	UserId         int        `json:"user_id,omitempty"`
	RegistrantId   int        `json:"registrant_id,omitempty"`
	Name           string     `json:"name,omitempty"`
	UnicodeName    string     `json:"unicode_name,omitempty"`
	Token          string     `json:"token,omitempty"`
	State          string     `json:"state,omitempty"`
	Language       string     `json:"language,omitempty"`
	Lockable       bool       `json:"lockable,omitempty"`
	AutoRenew      bool       `json:"auto_renew,omitempty"`
	WhoisProtected bool       `json:"whois_protected,omitempty"`
	RecordCount    int        `json:"record_count,omitempty"`
	ServiceCount   int        `json:"service_count,omitempty"`
	ExpiresOn      *Date      `json:"expires_on,omitempty"`
	CreatedAt      *time.Time `json:"created_at,omitempty"`
	UpdatedAt      *time.Time `json:"updated_at,omitempty"`
}

type domainWrapper struct {
	Domain Domain `json:"domain"`
}

// domainRequest represents a generic wrapper for a domain request,
// when domainWrapper cannot be used because of type constraint on Domain.
type domainRequest struct {
	Domain interface{} `json:"domain"`
}

func domainIdentifier(value interface{}) string {
	switch value := value.(type) {
	case string:
		return value
	case int:
		return fmt.Sprintf("%d", value)
	}
	return ""
}

// domainPath generates the resource path for given domain.
func domainPath(domain interface{}) string {
	if domain != nil {
		return fmt.Sprintf("domains/%s", domainIdentifier(domain))
	}
	return "domains"
}

// List the domains.
//
// DNSimple API docs: http://developer.dnsimple.com/domains/#list
func (s *DomainsService) List() ([]Domain, *Response, error) {
	path := domainPath(nil)
	returnedDomains := []domainWrapper{}

	res, err := s.client.get(path, &returnedDomains)
	if err != nil {
		return []Domain{}, res, err
	}

	domains := []Domain{}
	for _, domain := range returnedDomains {
		domains = append(domains, domain.Domain)
	}

	return domains, res, nil
}

// Create a new domain.
//
// DNSimple API docs: http://developer.dnsimple.com/domains/#create
func (s *DomainsService) Create(domainAttributes Domain) (Domain, *Response, error) {
	path := domainPath(nil)
	wrappedDomain := domainWrapper{Domain: domainAttributes}
	returnedDomain := domainWrapper{}

	res, err := s.client.post(path, wrappedDomain, &returnedDomain)
	if err != nil {
		return Domain{}, res, err
	}

	return returnedDomain.Domain, res, nil
}

// Get fetches a domain.
//
// DNSimple API docs: http://developer.dnsimple.com/domains/#get
func (s *DomainsService) Get(domain interface{}) (Domain, *Response, error) {
	path := domainPath(domain)
	returnedDomain := domainWrapper{}

	res, err := s.client.get(path, &returnedDomain)
	if err != nil {
		return Domain{}, res, err
	}

	return returnedDomain.Domain, res, nil
}

// Delete a domain.
//
// DNSimple API docs: http://developer.dnsimple.com/domains/#delete
func (s *DomainsService) Delete(domain interface{}) (*Response, error) {
	path := domainPath(domain)

	return s.client.delete(path, nil)
}