-
Notifications
You must be signed in to change notification settings - Fork 5
/
github_info.go
54 lines (44 loc) · 1.07 KB
/
github_info.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func fetchPublicKeyByUserID(userID string) (string, error) {
res, err := http.Get("https://github.com/" + userID + ".keys")
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Failed HTTP request to fetch a public key: %d", res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
r := regexp.MustCompile("\r?\n")
lines := r.Split(string(body), -1)
return lines[0], nil
}
type users struct {
Email string `json:"email"`
}
func fetchEmailAddressByUserID(userID string) (string, error) {
res, err := http.Get("https://api.github.com/users/" + userID)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Failed HTTP request to fetch an email adress: %d", res.StatusCode)
}
userInfo := new(users)
err = json.NewDecoder(res.Body).Decode(userInfo)
if err != nil {
return "", err
}
return userInfo.Email, nil
}