-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
123 lines (107 loc) · 2.72 KB
/
httpclient.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
// Package provides an HTTP client which uses a persistent cache. The caching
// behavior is not RFC 7234 compliant by design, and is not involved at the
// HTTP transport layer.
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
type Client struct {
http *http.Client
cache *Cache
}
func NewHTTPClient() *Client {
return &Client{
http: &http.Client{},
cache: NewCache("/var/cache/ussher"),
}
}
func (c *Client) GetURL(url string) []string {
if cached, ok := c.cache.Get(url); ok {
return bodyToKeys(cached)
}
log.Printf("GET %v", url)
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
return make([]string, 0)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return make([]string, 0)
}
c.cache.Set(url, bodyBytes)
return bodyToKeys(bodyBytes)
}
return make([]string, 0)
}
type GHEKey struct {
ID int `json:"id"`
Key string `json:"key"`
}
func (c *Client) GetGHE(ghe GithubEnterprise) []string {
url := "https://" + ghe.Hostname + "/users/" + ghe.Username + "/keys"
/* This will cache responses regardless of the token in context here, which could be a security risk. */
if cached, ok := c.cache.Get(url); ok {
var keys []GHEKey
err := json.Unmarshal(cached, &keys)
if err != nil {
log.Printf("Failed to decode JSON from cache for %v: %v", url, err)
return make([]string, 0)
}
return GHEKeysToKeys(keys)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("Unable to create new request", err)
return make([]string, 0)
}
req.Header = http.Header{
"Accept": {"application/vnd.github+json"},
"Authorization": {"token " + ghe.Token}}
log.Printf("GET %v", url)
resp, err := c.http.Do(req)
if err != nil {
log.Fatal("Request failed", err)
return make([]string, 0)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return make([]string, 0)
}
c.cache.Set(url, bodyBytes)
var keys []GHEKey
if err := json.Unmarshal(bodyBytes, &keys); err != nil {
log.Printf("Failed to decode JSON from %v: %v", url, err)
return make([]string, 0)
}
return GHEKeysToKeys(keys)
} else {
log.Fatal("HTTP ", resp.StatusCode, ": ", url)
}
return make([]string, 0)
}
func bodyToKeys(body []byte) []string {
s := string(body)
s = strings.TrimSuffix(s, "\n")
keys := strings.Split(s, "\n")
log.Printf("Found %v key(s)", len(keys))
return keys
}
func GHEKeysToKeys(gheKeys []GHEKey) []string {
var keys []string
for _, gheKey := range gheKeys {
keys = append(keys, gheKey.Key)
}
log.Printf("Found %v key(s)", len(keys))
return keys
}