-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdomains.go
More file actions
62 lines (49 loc) · 1.5 KB
/
domains.go
File metadata and controls
62 lines (49 loc) · 1.5 KB
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
// Package domains provides methods for managing an app's domains.
package domains
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List domains registered with an app.
func List(c *drycc.Client, appID string, results int) (api.Domains, int, error) {
u := fmt.Sprintf("/v2/apps/%s/domains/", appID)
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Domain{}, -1, reqErr
}
var domains []api.Domain
if err := json.Unmarshal([]byte(body), &domains); err != nil {
return []api.Domain{}, -1, err
}
return domains, count, reqErr
}
// New adds a domain to an app.
func New(c *drycc.Client, appID, domain, Ptype string) (api.Domain, error) {
u := fmt.Sprintf("/v2/apps/%s/domains/", appID)
req := api.DomainCreateRequest{Domain: domain, Ptype: Ptype}
body, err := json.Marshal(req)
if err != nil {
return api.Domain{}, err
}
res, reqErr := c.Request("POST", u, body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Domain{}, reqErr
}
defer res.Body.Close()
d := api.Domain{}
if err = json.NewDecoder(res.Body).Decode(&d); err != nil {
return api.Domain{}, err
}
return d, reqErr
}
// Delete removes a domain from an app.
func Delete(c *drycc.Client, appID string, domain string) error {
u := fmt.Sprintf("/v2/apps/%s/domains/%s", appID, domain)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}