-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
domains.go
90 lines (73 loc) · 2.2 KB
/
domains.go
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
package internal
import (
"context"
"errors"
"fmt"
"net/http"
querystring "github.com/google/go-querystring/query"
)
// DomainService API access to Domain.
type DomainService service
// GetAll domains.
// https://api-docs.constellix.com/?version=latest#484c3f21-d724-4ee4-a6fa-ab22c8eb9e9b
func (s *DomainService) GetAll(ctx context.Context, params *PaginationParameters) ([]Domain, error) {
endpoint, err := s.client.createEndpoint(defaultVersion, "domains")
if err != nil {
return nil, fmt.Errorf("failed to create request endpoint: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
if params != nil {
v, errQ := querystring.Values(params)
if errQ != nil {
return nil, errQ
}
req.URL.RawQuery = v.Encode()
}
var domains []Domain
err = s.client.do(req, &domains)
if err != nil {
return nil, err
}
return domains, nil
}
// GetByName Gets domain by name.
func (s *DomainService) GetByName(ctx context.Context, domainName string) (Domain, error) {
domains, err := s.Search(ctx, Exact, domainName)
if err != nil {
return Domain{}, err
}
if len(domains) == 0 {
return Domain{}, fmt.Errorf("domain not found: %s", domainName)
}
if len(domains) > 1 {
return Domain{}, fmt.Errorf("multiple domains found: %v", domains)
}
return domains[0], nil
}
// Search searches for a domain by name.
// https://api-docs.constellix.com/?version=latest#3d7b2679-2209-49f3-b011-b7d24e512008
func (s *DomainService) Search(ctx context.Context, filter searchFilter, value string) ([]Domain, error) {
endpoint, err := s.client.createEndpoint(defaultVersion, "domains", "search")
if err != nil {
return nil, fmt.Errorf("failed to create request endpoint: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
query := req.URL.Query()
query.Set(string(filter), value)
req.URL.RawQuery = query.Encode()
var domains []Domain
err = s.client.do(req, &domains)
if err != nil {
var nf *NotFound
if !errors.As(err, &nf) {
return nil, err
}
}
return domains, nil
}