-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
139 lines (104 loc) · 2.58 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gpic
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
const GH_HOSTNAME = "https://avatars.githubusercontent.com"
const GH_MAX_SIZE = 460
type githubAvatar struct {
ID int64
size int16
}
func (this *githubAvatar) IsDefault() bool {
return false
}
// Set a default image is a custom one isn't available. GitHub doesn't yet
// support this.
func (this *githubAvatar) SetDefault(urlS string) error {
return nil
}
func (this *githubAvatar) setDisableDefault(disable bool) {
}
// Set the size of the avatar. A size of '0' is an alias to set maxSize.
func (this *githubAvatar) SetSize(size int16) error {
if size == -1 || size > GH_MAX_SIZE {
size = GH_MAX_SIZE
}
if size < -1 {
return errors.New("Size cannot be a negative number under -1.")
}
this.size = size
return nil
}
func (this *githubAvatar) URL() (*url.URL, error) {
path := fmt.Sprintf("/u/%d?", this.ID)
v := url.Values{}
v.Add("v", "4")
if this.size != 0 {
v.Add("size", strconv.Itoa(int(this.size)))
}
return url.Parse(GH_HOSTNAME + path + v.Encode())
}
// Return a new GitHub profile
// this isn't complete for tokens yet
func NewGitHubAvatar(iType, input string) (*githubAvatar, error) {
avatar := new(githubAvatar)
// check cache first
id, found := cache.read("gh" + iType + input)
if found {
avatar.ID = id
return avatar, nil
}
var url string
switch iType {
case "username":
url = fmt.Sprintf("https://api.github.com/users/%s", input)
case "token":
url = "https://api.github.com/user"
case "id":
aID, err := strconv.ParseFloat(input, 64)
if err != nil {
return nil, err
}
avatar.ID = int64(aID)
return avatar, nil
}
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
if iType == "token" {
req.Header.Add("Authorization", "Bearer "+input)
}
// special case to help mock token tests
if iType == "token" && input == "ghp_test-working-token" {
avatar.ID = 6017470
return avatar, nil
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var githubUser map[string]interface{}
err = json.Unmarshal(body, &githubUser)
if err != nil {
return nil, err
}
if githubUser["message"] != nil {
return nil, errors.New(githubUser["message"].(string))
}
avatar.ID = int64(githubUser["id"].(float64))
// write to cache
cache.write("gh"+iType+input, avatar.ID)
return avatar, nil
}