-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.go
53 lines (49 loc) · 1.32 KB
/
github.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
package main
import (
"encoding/json"
"fmt"
"github.com/chenminhua/gitfofo/types"
"io/ioutil"
"net/http"
"os"
)
// userName为"",则拿用户自己信息
func getUser(userName string) *types.User {
var url string
if userName == "" {
url = "https://api.github.com/user"
} else {
url = fmt.Sprintf("https://api.github.com/users/%s", userName)
}
body, err := httpQuery(url)
var user types.User
err = json.Unmarshal(body, &user)
if err != nil {
fmt.Println(err)
}
return &user
}
func httpQuery(query string) ([]byte, error) {
req, _ := http.NewRequest("GET", query, nil)
req.Header.Set("Authorization", fmt.Sprintf("token %s", config.Token))
resp, err := (&http.Client{}).Do(req)
if err != nil {
return nil, err
}
if resp.Status == "403 Forbidden" {
println("403, maybe you have hit the ratelimit, read this: https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting") //
os.Exit(1)
}
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
func getFollowing(username string, page int) []*types.FollowingUser {
url := fmt.Sprintf("https://api.github.com/users/%s/following?page=%d", username, page)
body, err := httpQuery(url)
var users []*types.FollowingUser
err = json.Unmarshal(body, &users)
if err != nil {
fmt.Printf("get user %s error: %s\n", username, err)
}
return users
}