-
Notifications
You must be signed in to change notification settings - Fork 1
/
java_widget.go
55 lines (42 loc) · 1.13 KB
/
java_widget.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
package mcstatus
import (
"fmt"
"image"
"image/png"
"net/http"
"net/url"
"strconv"
)
type JavaWidgetOptions struct {
Dark bool
Rounded bool
Timeout float64
}
func GetJavaWidget(host string, port uint16, options ...JavaWidgetOptions) (image.Image, error) {
opts := JavaWidgetOptions{
Dark: true,
Rounded: true,
Timeout: 5.0,
}
if len(options) > 0 {
opts = options[0]
}
params := &url.Values{}
params.Set("dark", strconv.FormatBool(opts.Dark))
params.Set("rounded", strconv.FormatBool(opts.Rounded))
params.Set("timeout", strconv.FormatFloat(opts.Timeout, 'f', 1, 64))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/widget/java/%s:%s?%s", baseURL, url.PathEscape(host), url.PathEscape(strconv.FormatUint(uint64(port), 10)), params.Encode()), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("go-mcstatus %s", version))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("mcstatus: unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close()
return png.Decode(resp.Body)
}