-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
68 lines (62 loc) · 2.17 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
const (
gitApiURL = "https://api.github.com"
userEndPoint = "/users/"
)
type User struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
Name string `json:"name"`
Company string `json:"company"`
Blog string `json:"blog"`
Location string `json:"location"`
Email string `json:"email"`
Hireable bool `json:"hireable"`
Bio string `json:"bio"`
PublicRepos int `json:"public_repos"`
PublicGists int `json:"public_gists"`
Followers int `json:"followers"`
Following int `json:"following"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Message string `json:"message"`
}
func getUserFromGit(userName string) (user *User, err error) {
user = &User{}
url := gitApiURL + userEndPoint + userName
resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
return nil, err
}
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
return nil, err
}
//Check for status code
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Failed to fetch from Github. Github returned with Status code: %v and Message: %s", resp.StatusCode, user.Message)
}
return user, nil
}