-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
51 lines (45 loc) · 834 Bytes
/
rest.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
package lanyard
import (
"encoding/json"
"errors"
"io"
"net/http"
)
const (
API_URL = "https://api.lanyard.rest/v1/users/"
)
func FetchUser(userId string) (LanyardResponse, error) {
resp, err := http.Get(API_URL + userId)
if err != nil {
return LanyardResponse{
false,
&LanyardData{},
&LanyardError{},
}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return LanyardResponse{
false,
&LanyardData{},
&LanyardError{},
}, err
}
var data LanyardResponse
err = json.Unmarshal(body, &data)
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return LanyardResponse{
false,
&LanyardData{},
&LanyardError{},
}, errors.New(data.Error.Message)
}
if err != nil {
return LanyardResponse{
false,
&LanyardData{},
&LanyardError{},
}, err
}
return data, nil
}