-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
88 lines (73 loc) · 2.02 KB
/
client.go
File metadata and controls
88 lines (73 loc) · 2.02 KB
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
package httpdf
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
)
var (
ErrNotOK = fmt.Errorf("non-OK response status")
)
// Client is a simple HTTP client for the HTTPPDF service.
type Client struct {
httpClient *http.Client
baseURL string
}
// NewClient creates a new HTTPPDF client for the given base URL
func NewClient(baseURL string, opts ...ClientOption) *Client {
c := &Client{
httpClient: &http.Client{},
baseURL: strings.TrimRight(baseURL, "/"),
}
for _, opt := range opts {
opt(c)
}
return c
}
// Render given template using the provided values
func (c *Client) Render(ctx context.Context, template string, values any, lang ...string) (io.ReadCloser, error) {
u := c.baseURL + "/" + path.Join("templates", template, "render")
// Add lang query parameter if provided
if len(lang) > 0 && lang[0] != "" {
parsedURL, err := url.Parse(u)
if err != nil {
return nil, fmt.Errorf("render template: %w", err)
}
q := parsedURL.Query()
q.Set("lang", lang[0])
parsedURL.RawQuery = q.Encode()
u = parsedURL.String()
}
body := bytes.NewBuffer(nil)
if err := json.NewEncoder(body).Encode(values); err != nil {
return nil, fmt.Errorf("render template: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, body)
if err != nil {
return nil, fmt.Errorf("render template: %w", err)
}
req.Header.Set("Content-Type", "application/json")
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("render template: %w", err)
}
if res.StatusCode != http.StatusOK {
defer res.Body.Close()
resBody, _ := io.ReadAll(res.Body)
return nil, fmt.Errorf("render template: %w (%d): %s", ErrNotOK, res.StatusCode, resBody)
}
return res.Body, nil
}
// ClientOption is a function that configures the HTTPPDF client
type ClientOption func(*Client)
// WithHTTPClient sets a custom HTTP client for the HTTPPDF client
func WithHTTPClient(client *http.Client) ClientOption {
return func(c *Client) {
c.httpClient = client
}
}