|
1 | 1 | package bitbucket |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "github.com/mitchellh/mapstructure" |
| 5 | +) |
| 6 | + |
3 | 7 | // User is the sub struct of Client |
| 8 | +// Reference: https://developer.atlassian.com/bitbucket/api/2/reference/resource/user |
4 | 9 | type User struct { |
5 | | - c *Client |
| 10 | + c *Client |
| 11 | + Uuid string |
| 12 | + Username string |
| 13 | + Nickname string |
| 14 | + Website string |
| 15 | + AccountStatus string `mapstructure:"account_status"` |
| 16 | + DisplayName string `mapstructure:"display_name"` |
| 17 | + CreatedOn string `mapstructure:"created_on"` |
| 18 | + Has2faEnabled bool `mapstructure:"has_2fa_enabled"` |
| 19 | + Links map[string]interface{} |
6 | 20 | } |
7 | 21 |
|
8 | 22 | // Profile is getting the user data |
9 | | -func (u *User) Profile() (interface{}, error) { |
| 23 | +func (u *User) Profile() (*User, error) { |
10 | 24 | urlStr := u.c.GetApiBaseURL() + "/user/" |
11 | | - return u.c.execute("GET", urlStr, "") |
| 25 | + response, err := u.c.execute("GET", urlStr, "") |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + return decodeUser(response) |
12 | 30 | } |
13 | 31 |
|
14 | 32 | // Emails is getting user's emails |
15 | 33 | func (u *User) Emails() (interface{}, error) { |
16 | 34 | urlStr := u.c.GetApiBaseURL() + "/user/emails" |
17 | 35 | return u.c.execute("GET", urlStr, "") |
18 | 36 | } |
| 37 | + |
| 38 | +func decodeUser(userResponse interface{}) (*User, error) { |
| 39 | + userMap := userResponse.(map[string]interface{}) |
| 40 | + |
| 41 | + if userMap["type"] == "error" { |
| 42 | + return nil, DecodeError(userMap) |
| 43 | + } |
| 44 | + |
| 45 | + var user = new(User) |
| 46 | + err := mapstructure.Decode(userMap, user) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + return user, nil |
| 52 | +} |
0 commit comments