-
Notifications
You must be signed in to change notification settings - Fork 0
/
MedialogClient.go
112 lines (89 loc) · 2.18 KB
/
MedialogClient.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package medialogclient
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/nyudlts/go-medialog/models"
yaml "gopkg.in/yaml.v2"
)
type Creds struct {
URL string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type MedialogClient struct {
Token string
BaseURL string
Client *http.Client
}
const API_ROOT = "/api/v0"
func NewClient(config string, environment string, timeout int) (*MedialogClient, error) {
mlClient := MedialogClient{}
creds, err := getCreds(config, environment)
if err != nil {
return &mlClient, err
}
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: time.Duration(timeout) * time.Second,
DisableCompression: true,
}
mlClient.Client = &http.Client{Transport: tr}
mlClient.BaseURL = creds.URL + API_ROOT
mlClient.getToken(creds)
return &mlClient, nil
}
func getCreds(config string, environment string) (Creds, error) {
credsMap := map[string]Creds{}
configBytes, err := os.ReadFile(config)
if err != nil {
return Creds{}, err
}
if err = yaml.Unmarshal(configBytes, &credsMap); err != nil {
return Creds{}, err
}
for k, v := range credsMap {
if environment == k {
return v, nil
}
}
return Creds{}, fmt.Errorf("credentials file did not contain %s\n", environment)
}
func (mlClient *MedialogClient) getToken(creds Creds) {
url := fmt.Sprintf("%s/users/%s/login?password=%s", mlClient.BaseURL, creds.Username, creds.Password)
response, err := mlClient.Client.Post(url, "", nil)
if err != nil {
panic(err.Error())
}
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
token := models.Token{}
if err := json.Unmarshal(body, &token); err != nil {
panic(err)
}
mlClient.Token = token.Token
}
func (mlc MedialogClient) GetHostInfo() (models.MedialogInfo, error) {
mlInfo := models.MedialogInfo{}
req, err := http.NewRequest("GET", mlc.BaseURL, nil)
if err != nil {
return mlInfo, err
}
resp, err := mlc.Client.Do(req)
if err != nil {
return mlInfo, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return mlInfo, err
}
if err := json.Unmarshal(body, &mlInfo); err != nil {
return mlInfo, err
}
return mlInfo, nil
}