forked from thecodingmachine/gotenberg-go-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
baserequest.go
129 lines (105 loc) · 3.71 KB
/
baserequest.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
124
125
126
127
128
129
package gotenberg
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"github.com/runatal/gotenberg-go-client/v8/document"
)
type baseRequester interface {
customHeaders() map[httpHeader]string
formFields() map[formField]string
formDocuments() map[string]document.Document
}
type baseRequest struct {
headers map[httpHeader]string
fields map[formField]string
}
func newBaseRequest() *baseRequest {
return &baseRequest{
headers: make(map[httpHeader]string),
fields: make(map[formField]string),
}
}
func (br *baseRequest) customHeaders() map[httpHeader]string {
return br.headers
}
func (br *baseRequest) formFields() map[formField]string {
return br.fields
}
// OutputFilename overrides the default UUID output filename.
//
// NOTE: Gotenberg adds the file extension automatically; you don't have to set it.
func (br *baseRequest) OutputFilename(filename string) {
br.headers[headerOutputFilename] = filename
}
// Trace overrides the default UUID trace, or request ID, that identifies a request in Gotenberg's logs.
func (br *baseRequest) Trace(trace string) {
br.headers[headerTrace] = trace
}
// UseBasicAuth sets the basic authentication credentials.
func (br *baseRequest) UseBasicAuth(username, password string) {
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
br.headers[headerAuthorization] = "Basic " + auth
}
// UseWebhook sets the callback and error callback that Gotenberg will use to send
// respectively the output file and the error response.
func (br *baseRequest) UseWebhook(hookURL string, errorURL string) {
br.headers[headerWebhookURL] = hookURL
br.headers[headerWebhookErrorURL] = errorURL
}
// SetWebhookMethod Overrides the default HTTP method that Gotenberg will use to call the webhook.
func (br *baseRequest) SetWebhookMethod(method string) {
br.headers[headerWebhookMethod] = ensureWebhookMethod(method)
}
// SetWebhookErrorMethod overrides the default HTTP method that Gotenberg will use to call the error webhook.
func (br *baseRequest) SetWebhookErrorMethod(method string) {
br.headers[headerWebhookErrorMethod] = ensureWebhookMethod(method)
}
// SetWebhookExtraHeaders sets the extra HTTP headers that Gotenberg will send alongside the
// request to the webhook and error webhook.
func (br *baseRequest) SetWebhookExtraHeaders(headers map[string]string) error {
marshaledHeaders, err := json.Marshal(headers)
if err != nil {
return fmt.Errorf("marshal headers to JSON: %w", err)
}
br.headers[headerWebhookExtraHeaders] = string(marshaledHeaders)
return nil
}
func hasWebhook(req baseRequester) bool {
url, ok := req.customHeaders()[headerWebhookURL]
if !ok {
return false
}
return url != ""
}
func ensureWebhookMethod(method string) string {
if method == http.MethodPut || method == http.MethodPost || method == http.MethodPatch {
return method
} else {
return http.MethodGet
}
}
// DownloadFrom sets the URLs to download files from.
// This method accepts a JSON string e.g., [{"url":"http://localhost:80/","extraHttpHeaders":{"X-Foo":"Bar"}}]. For Go,
// this is equivalent to map[string]map[string]string, which this method accepts, but headers map can be nil.
//
// URLs MUST return a Content-Disposition header with a filename parameter.
func (br *baseRequest) DownloadFrom(downloads map[string]map[string]string) {
dfs := make([]downloadFrom, 0, len(downloads))
for url, headers := range downloads {
dfs = append(dfs, downloadFrom{
URL: url,
ExtraHTTPHeaders: headers,
})
}
marshaled, err := json.Marshal(dfs)
if err != nil {
return
}
br.fields[fieldDownloadFrom] = string(marshaled)
}
type downloadFrom struct {
URL string `json:"url"`
ExtraHTTPHeaders map[string]string `json:"extraHttpHeaders"`
}