-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_data.go
55 lines (48 loc) · 1.34 KB
/
user_data.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
package disgoauth
// Import Packages
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// The GetUserData() function is used to send an api
// request to the discord/users/@me endpoint with
// the provided accessToken.
func GetUserData(token string) (map[string]interface{}, error) {
// Establish a new request object
req, err := http.NewRequest("GET", "https://discord.com/api/users/@me", nil)
// Handle the error
if err != nil {
return map[string]interface{}{}, err
}
// Set the request object's headers
req.Header = http.Header{
"Content-Type": []string{"application/json"},
"Authorization": []string{token},
}
// Send the http request
resp, err := RequestClient.Do(req)
// Handle the error
// If the response status isn't a success
if resp.StatusCode != 200 || err != nil {
// Read the http body
body, _err := io.ReadAll(resp.Body)
// Handle the read body error
if _err != nil {
return map[string]interface{}{}, _err
}
// Handle http response error
return map[string]interface{}{},
fmt.Errorf("status: %d, code: %v, body: %s",
resp.StatusCode, err, string(body))
}
// Readable golang map used for storing
// the response body
var data map[string]interface{}
// Handle the error
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return map[string]interface{}{}, err
}
return data, nil
}