/
client.go
59 lines (47 loc) · 1.08 KB
/
client.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
package rendertron
import (
"context"
"fmt"
"net/http"
"net/url"
)
var (
_ RendertronClient = (*ProxyRendertronClient)(nil)
)
type RendertronClient interface {
Render(ctx context.Context, u string, opt *Options) (*http.Response, error)
}
type Options struct {
InjectShadyDom bool
}
func (o *Options) GetInjectShadyDom() bool {
if o == nil {
return false
}
return o.InjectShadyDom
}
type ProxyRendertronClient struct {
proxy string
client *http.Client
}
func NewProxyRendertronClient(proxy string) *ProxyRendertronClient {
if proxy[len(proxy)-1] != '/' {
proxy += "/"
}
proxy += "render/"
return &ProxyRendertronClient{
proxy: proxy,
client: &http.Client{},
}
}
func (cli *ProxyRendertronClient) Render(ctx context.Context, u string, opt *Options) (*http.Response, error) {
ru := cli.proxy + url.PathEscape(u)
if opt.GetInjectShadyDom() {
ru += "?wc-inject-shadydom=true"
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ru, nil)
if err != nil {
return nil, fmt.Errorf("redertron: unable to create request: %w", err)
}
return cli.client.Do(req)
}