-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
179 lines (147 loc) · 3.85 KB
/
client.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package bl3auto
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/cookiejar"
"github.com/PuerkitoBio/goquery"
"github.com/thedevsaddam/gojsonq/v2"
)
type HttpClient struct {
http.Client
headers http.Header
}
type HttpResponse struct {
http.Response
}
func NewHttpClient() (*HttpClient, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, errors.New("failed to setup cookies")
}
return &HttpClient{
http.Client{
Jar: jar,
},
http.Header{
"User-Agent": []string{"BL3 Auto SHiFT"},
},
}, nil
}
func (response *HttpResponse) BodyAsHtmlDoc() (*goquery.Document, error) {
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, errors.New("invalid response code")
}
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
return nil, errors.New("invalid html")
}
return doc, nil
}
func (response *HttpResponse) BodyAsJson() (*gojsonq.JSONQ, error) {
defer response.Body.Close()
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("invalid response json")
}
return JsonFromBytes(bodyBytes), nil
}
func getResponse(res *http.Response, err error) (*HttpResponse, error) {
return &HttpResponse{
*res,
}, err
}
func (client *HttpClient) SetDefaultHeader(k, v string) {
client.headers.Set(k, v)
}
func (client *HttpClient) Do(req *http.Request) (*HttpResponse, error) {
for k, v := range client.headers {
for _, x := range v {
req.Header.Set(k, x)
}
}
return getResponse(client.Client.Do(req))
}
func (client *HttpClient) Get(url string) (*HttpResponse, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return client.Do(req)
}
func (client *HttpClient) Head(url string) (*HttpResponse, error) {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
return client.Do(req)
}
func (client *HttpClient) Post(url, contentType string, body io.Reader) (*HttpResponse, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return client.Do(req)
}
func (client *HttpClient) PostJson(url string, data interface{}) (*HttpResponse, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
return client.Post(url, "application/json", bytes.NewBuffer(jsonData))
}
type Bl3Client struct {
HttpClient
Config Bl3Config
}
func NewBl3Client() (*Bl3Client, error) {
client, err := NewHttpClient()
if err != nil {
return nil, errors.New("failed to start client")
}
res, err := client.Get("https://raw.githubusercontent.com/jauderho/bl3auto/main/config.json")
if err != nil {
return nil, errors.New("failed to get config")
}
configJson, err := res.BodyAsJson()
if err != nil {
return nil, errors.New("failed to get config")
}
config := Bl3Config{}
configJson.Out(&config)
for header, value := range config.RequestHeaders {
client.SetDefaultHeader(header, value)
}
return &Bl3Client{
HttpClient: *client,
Config: config,
}, nil
}
func (client *Bl3Client) Login(username string, password string) error {
data := map[string]string{
"username": username,
"password": password,
}
loginRes, err := client.PostJson(client.Config.LoginUrl, data)
if err != nil {
return errors.New("failed to submit login credentials")
}
defer loginRes.Body.Close()
if loginRes.StatusCode != 200 {
return errors.New("failed to login")
}
/* if loginRes.Header.Get(client.Config.LoginRedirectHeader) == "" {
return errors.New("Failed to start session")
}
sessionRes, err := client.Get(loginRes.Header.Get(client.Config.LoginRedirectHeader))
if err != nil {
return errors.New("Failed to get session")
}
defer sessionRes.Body.Close()*/
client.SetDefaultHeader(client.Config.SessionHeader, loginRes.Header.Get(client.Config.SessionIdHeader))
return nil
}