-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain.go
85 lines (69 loc) · 1.77 KB
/
domain.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
package arvanvod
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// Set subdomain for VOD service
func (c *Client) SetDomain(ctx context.Context, subdomain *SetSubDomainModel) error {
jsonBody, err := json.Marshal(subdomain)
if err != nil {
return err
}
bodyReader := bytes.NewReader(jsonBody)
requestURL := fmt.Sprintf("%s/domain", c.options.BaseUrl)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bodyReader)
if err != nil {
return err
}
// add authorization header to the req
req.Header.Add("Authorization", fmt.Sprintf("Apikey %s", c.options.ApiKey))
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
return getErrorByStatus(res.StatusCode)
}
// Get subdomain
func (c *Client) GetDomain(ctx context.Context) (*GetSubdomainModel, error) {
requestURL := fmt.Sprintf("%s/domain", c.options.BaseUrl)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
if err != nil {
return nil, err
}
// add authorization header to the req
req.Header.Add("Authorization", fmt.Sprintf("Apikey %s", c.options.ApiKey))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
err = getErrorByStatus(res.StatusCode)
if err != nil {
return nil, err
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := new(GetSubdomainModel)
err = json.Unmarshal(resBody, response)
if err != nil {
return nil, err
}
return response, nil
}
type SetSubDomainModel struct {
Subdomain string `json:"subdomain"`
}
type GetSubdomainModel struct {
Data struct {
Subdomain string `json:"subdomain"`
Domain string `json:"domain"`
CreatedAT *time.Time `json:"created_at"`
} `json:"data"`
}