-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp.go
68 lines (55 loc) · 1.37 KB
/
http.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
//go:build !tinygo
// +build !tinygo
package gfx
import (
"image"
"net/http"
"time"
)
// HTTP is the HTTP client and user agent used by the gfx package.
type HTTP struct {
*http.Client
UserAgent string
}
// HTTPClient is the default client used by Get/GetPNG/GetTileset, etc.
var HTTPClient = HTTP{
Client: &http.Client{
Timeout: 30 * time.Second,
},
UserAgent: "gfx.HTTPClient",
}
// Get performs a HTTP GET request using the DefaultClient.
func Get(rawurl string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, rawurl, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", HTTPClient.UserAgent)
return HTTPClient.Do(req)
}
// GetPNG retrieves a remote PNG using DefaultClient
func GetPNG(rawurl string) (image.Image, error) {
resp, err := Get(rawurl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return DecodePNG(resp.Body)
}
// GetImage retrieves a remote image using DefaultClient
func GetImage(rawurl string) (image.Image, error) {
resp, err := Get(rawurl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return DecodeImage(resp.Body)
}
// GetTileset retrieves a remote tileset using GetPNG.
func GetTileset(p Palette, tileSize image.Point, rawurl string) (*Tileset, error) {
m, err := GetPNG(rawurl)
if err != nil {
return nil, err
}
return NewTilesetFromImage(p, tileSize, m), nil
}