-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_user.go
More file actions
47 lines (38 loc) · 878 Bytes
/
get_user.go
File metadata and controls
47 lines (38 loc) · 878 Bytes
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
package gowandbox
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
/*
Returns a user, given the session key (provided by WandBox).
If the session key is invalid, a blank string is returned for the username.
Maps to the `/user.json` endpoint.
*/
func GetUser(sessionKey string, ctx context.Context) (GWBUser, error) {
var result GWBUser
client := http.DefaultClient
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
WandBoxUrl+"/user.json?session="+sessionKey,
nil,
)
if err != nil {
return result, err
}
resp, err := client.Do(req)
if err != nil {
return result, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
e, _ := ioutil.ReadAll(resp.Body)
return result, errors.New(string(e))
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&result)
return result, err
}