/
user.go
42 lines (33 loc) · 817 Bytes
/
user.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 da
import (
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
func getUserID(path string, token string) (nickname string, err error) {
task := http.Client{}
req, err := http.NewRequest("GET",
path+"/forgeapps/me",
nil,
)
req.Header.Set("Authorization", "Bearer "+token)
response, err := task.Do(req)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
content, _ := ioutil.ReadAll(response.Body)
err = errors.New("[" + strconv.Itoa(response.StatusCode) + "] " + string(content))
return
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
//TODO: Review why the data has quotes in its content and find a more elegant way to remove them
nickname = strings.Replace(string(data), "\"", "", -1 )
return
}