-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
42 lines (34 loc) · 951 Bytes
/
request.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
package genshin
import (
"encoding/json"
"errors"
"io"
"net/http"
)
func makeOSRequest(uid string, endpoint string, locale string, ds string, headers http.Header) (User, error) {
client := &http.Client{}
site := endpoint + "index" + "?role_id=" + uid + "&server=" + locale
req, err := http.NewRequest("GET", site, nil)
if err != nil {
return User{}, errors.New("got error " + err.Error())
}
req.Header = headers
response, err := client.Do(req)
if err != nil {
return User{}, errors.New("got error " + err.Error())
}
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
// bodyBytes, err := io.ReadAll(response.Body)
// if err != nil {
// return "", err
// }
// bodyString := string(bodyBytes)
// return bodyString, nil
var user User
data, _ := io.ReadAll(response.Body)
json.Unmarshal(data, &user)
return user, nil
}
return User{}, errors.New("request failed")
}