adding PHP example for custom HTTP headers in documentation + improving logs for custom HTTP headers

This commit is contained in:
Julien Neuhart
2019-12-09 16:50:08 +01:00
parent 2251267ae4
commit 126cdd73e4
16 changed files with 133 additions and 95 deletions

View File

@@ -18,7 +18,7 @@ Unless your project already has a PSR7 `HttpClient`, install `php-http/guzzle6-a
$ composer require php-http/guzzle6-adapter $ composer require php-http/guzzle6-adapter
``` ```
Then the PHP client: Then the [PHP client](https://github.com/thecodingmachine/gotenberg-php-client):
```bash ```bash
$ composer require thecodingmachine/gotenberg-php-client $ composer require thecodingmachine/gotenberg-php-client

View File

@@ -59,7 +59,7 @@ use TheCodingMachine\Gotenberg\HTMLRequest;
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client()); $client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
$index = DocumentFactory::makeFromPath('index.html', 'index.html'); $index = DocumentFactory::makeFromPath('index.html', 'index.html');
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -146,7 +146,7 @@ $footer = DocumentFactory::makeFromPath('footer.html', 'footer.html');
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request->setHeader($header); $request->setHeader($header);
$request->setFooter($footer); $request->setFooter($footer);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -237,7 +237,7 @@ $assets = [
]; ];
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request->setAssets($assets); $request->setAssets($assets);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -296,7 +296,7 @@ $request = new HTMLRequest($index);
$request->setPaperSize(Request::A4); $request->setPaperSize(Request::A4);
$request->setMargins(Request::NO_MARGINS); $request->setMargins(Request::NO_MARGINS);
$request->setLandscape(true); $request->setLandscape(true);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -345,7 +345,7 @@ $client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client()
$index = DocumentFactory::makeFromPath('index.html', 'index.html'); $index = DocumentFactory::makeFromPath('index.html', 'index.html');
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request->setWaitDelay(5.5); $request->setWaitDelay(5.5);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -397,6 +397,6 @@ $client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client()
$index = DocumentFactory::makeFromPath('index.html', 'index.html'); $index = DocumentFactory::makeFromPath('index.html', 'index.html');
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request->setGoogleChromeRpccBufferSize(1048576); $request->setGoogleChromeRpccBufferSize(1048576);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```

View File

@@ -51,7 +51,7 @@ use TheCodingMachine\Gotenberg\URLRequest;
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client()); $client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
$request = new URLRequest('https://google.com'); $request = new URLRequest('https://google.com');
$request->setMargins(Request::NO_MARGINS); $request->setMargins(Request::NO_MARGINS);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -85,4 +85,13 @@ $ curl --request POST \
### PHP ### PHP
// TODO ```php
use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\URLRequest;
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
$request = new URLRequest('https://google.com');
$request->addRemoteURLHTTPHeader('Your-Header', 'Foo')
$dest = 'result.pdf';
$client->store($request, $dest);
```

View File

@@ -65,6 +65,6 @@ $markdowns = [
DocumentFactory::makeFromPath('file.md', 'file.md'), DocumentFactory::makeFromPath('file.md', 'file.md'),
]; ];
$request = new MarkdownRequest($index, $markdowns); $request = new MarkdownRequest($index, $markdowns);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```

View File

@@ -64,7 +64,7 @@ $files = [
DocumentFactory::makeFromPath('document2.docx', 'document2.docx'), DocumentFactory::makeFromPath('document2.docx', 'document2.docx'),
]; ];
$request = new OfficeRequest($files); $request = new OfficeRequest($files);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
@@ -112,6 +112,6 @@ $files = [
]; ];
$request = new OfficeRequest($files); $request = new OfficeRequest($files);
$request->setLandscape(true); $request->setLandscape(true);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```

View File

@@ -50,6 +50,6 @@ $files = [
DocumentFactory::makeFromPath('file2.pdf', 'file2.pdf'), DocumentFactory::makeFromPath('file2.pdf', 'file2.pdf'),
]; ];
$request = new MergeRequest($files); $request = new MergeRequest($files);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```

View File

@@ -48,6 +48,6 @@ $client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client()
$index = DocumentFactory::makeFromPath('index.html', 'index.html'); $index = DocumentFactory::makeFromPath('index.html', 'index.html');
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request->setWaitTimeout(2.5); $request->setWaitTimeout(2.5);
$dest = "result.pdf"; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```

View File

@@ -128,4 +128,15 @@ $ curl --request POST \
### PHP ### PHP
// TODO ```php
use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
$index = DocumentFactory::makeFromPath('index.html', 'index.html');
$request = new HTMLRequest($index);
$request->setWebhookURL('http://myapp.com/webhook/');
$request->addWebhookURLHTTPHeader('Your-Header', 'Foo');
$resp = $client->post($request);
```

View File

@@ -233,7 +233,7 @@ Gotenberg API is available at <a href="http://localhost:3000">http://localhost:3
<pre class="chroma">$ composer require php-http/guzzle6-adapter <pre class="chroma">$ composer require php-http/guzzle6-adapter
</pre> </pre>
<p>Then the PHP client:</p> <p>Then the <a href="https://github.com/thecodingmachine/gotenberg-php-client">PHP client</a>:</p>
<pre class="chroma">$ composer require thecodingmachine/gotenberg-php-client <pre class="chroma">$ composer require thecodingmachine/gotenberg-php-client
</pre> </pre>
@@ -474,7 +474,7 @@ use TheCodingMachine\Gotenberg\HTMLRequest;
$client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client()); $client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client());
$index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;); $index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;);
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -569,7 +569,7 @@ $footer = DocumentFactory::makeFromPath(&#39;footer.html&#39;, &#39;footer.html&
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request-&gt;setHeader($header); $request-&gt;setHeader($header);
$request-&gt;setFooter($footer); $request-&gt;setFooter($footer);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -665,7 +665,7 @@ $assets = [
]; ];
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request-&gt;setAssets($assets); $request-&gt;setAssets($assets);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -731,7 +731,7 @@ $request = new HTMLRequest($index);
$request-&gt;setPaperSize(Request::A4); $request-&gt;setPaperSize(Request::A4);
$request-&gt;setMargins(Request::NO_MARGINS); $request-&gt;setMargins(Request::NO_MARGINS);
$request-&gt;setLandscape(true); $request-&gt;setLandscape(true);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -787,7 +787,7 @@ $client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\
$index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;); $index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;);
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request-&gt;setWaitDelay(5.5); $request-&gt;setWaitDelay(5.5);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -846,7 +846,7 @@ $client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\
$index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;); $index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;);
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request-&gt;setGoogleChromeRpccBufferSize(1048576); $request-&gt;setGoogleChromeRpccBufferSize(1048576);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -912,7 +912,7 @@ use TheCodingMachine\Gotenberg\URLRequest;
$client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client()); $client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client());
$request = new URLRequest(&#39;https://google.com&#39;); $request = new URLRequest(&#39;https://google.com&#39;);
$request-&gt;setMargins(Request::NO_MARGINS); $request-&gt;setMargins(Request::NO_MARGINS);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -955,7 +955,15 @@ canonical key for <code>accept-encoding</code> is <code>Accept-Encoding</code>.<
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>PHP</h3> </a>PHP</h3>
<p>// TODO</p> <pre class="chroma">use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\URLRequest;
$client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client());
$request = new URLRequest(&#39;https://google.com&#39;);
$request-&gt;addRemoteURLHTTPHeader(&#39;Your-Header&#39;, &#39;Foo&#39;)
$dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest);
</pre>
</div> </div>
@@ -1030,7 +1038,7 @@ $markdowns = [
DocumentFactory::makeFromPath(&#39;file.md&#39;, &#39;file.md&#39;), DocumentFactory::makeFromPath(&#39;file.md&#39;, &#39;file.md&#39;),
]; ];
$request = new MarkdownRequest($index, $markdowns); $request = new MarkdownRequest($index, $markdowns);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -1111,7 +1119,7 @@ $files = [
DocumentFactory::makeFromPath(&#39;document2.docx&#39;, &#39;document2.docx&#39;), DocumentFactory::makeFromPath(&#39;document2.docx&#39;, &#39;document2.docx&#39;),
]; ];
$request = new OfficeRequest($files); $request = new OfficeRequest($files);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -1164,7 +1172,7 @@ $files = [
]; ];
$request = new OfficeRequest($files); $request = new OfficeRequest($files);
$request-&gt;setLandscape(true); $request-&gt;setLandscape(true);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -1229,7 +1237,7 @@ $files = [
DocumentFactory::makeFromPath(&#39;file2.pdf&#39;, &#39;file2.pdf&#39;), DocumentFactory::makeFromPath(&#39;file2.pdf&#39;, &#39;file2.pdf&#39;),
]; ];
$request = new MergeRequest($files); $request = new MergeRequest($files);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -1292,7 +1300,7 @@ $client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\
$index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;); $index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;);
$request = new HTMLRequest($index); $request = new HTMLRequest($index);
$request-&gt;setWaitTimeout(2.5); $request-&gt;setWaitTimeout(2.5);
$dest = &#34;result.pdf&#34;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>
@@ -1449,7 +1457,17 @@ canonical key for <code>accept-encoding</code> is <code>Accept-Encoding</code>.<
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>PHP</h3> </a>PHP</h3>
<p>// TODO</p> <pre class="chroma">use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
$client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client());
$index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;index.html&#39;);
$request = new HTMLRequest($index);
$request-&gt;setWebhookURL(&#39;http://myapp.com/webhook/&#39;);
$request-&gt;addWebhookURLHTTPHeader(&#39;Your-Header&#39;, &#39;Foo&#39;);
$resp = $client-&gt;post($request);
</pre>
</div> </div>

View File

@@ -138,7 +138,7 @@ func urlHandler(c echo.Context) error {
if err != nil { if err != nil {
return err return err
} }
opts.CustomHeaders = resource.RemoteURLCustomHeaders(r) opts.CustomHTTPHeaders = resource.RemoteURLCustomHTTPHeaders(r)
if !r.HasArg(resource.RemoteURLArgKey) { if !r.HasArg(resource.RemoteURLArgKey) {
return xerror.Invalid( return xerror.Invalid(
op, op,
@@ -338,14 +338,14 @@ func convertAsync(ctx context.Context, p printer.Printer, filename, fpath string
} }
req.Header.Set(echo.HeaderContentType, "application/pdf") req.Header.Set(echo.HeaderContentType, "application/pdf")
// set custom headers (if any). // set custom headers (if any).
customHeaders := resource.WebhookURLCustomHeaders(r) customHeaders := resource.WebhookURLCustomHTTPHeaders(r)
if len(customHeaders) > 0 { if len(customHeaders) > 0 {
for key, value := range customHeaders { for key, value := range customHeaders {
req.Header.Set(key, value) req.Header.Set(key, value)
logger.DebugfOp(op, "set '%s' to custom header '%s'", value, key) logger.DebugfOp(op, "set '%s' to custom HTTP header '%s'", value, key)
} }
} else { } else {
logger.DebugOp(op, "skipping custom headers as none have been provided...") logger.DebugOp(op, "skipping custom HTTP headers as none have been provided...")
} }
// send the result file. // send the result file.
logger.DebugfOp( logger.DebugfOp(

View File

@@ -582,7 +582,7 @@ func TestOfficeHandler(t *testing.T) {
func TestWebhook(t *testing.T) { func TestWebhook(t *testing.T) {
customHeaderRealKey := http.CanonicalHeaderKey("MyCustomHeader") customHeaderRealKey := http.CanonicalHeaderKey("MyCustomHeader")
customHeaderKey := fmt.Sprintf("%s%s", resource.WebhookURLCustomHeaderCanonicalBaseKey, customHeaderRealKey) customHeaderKey := fmt.Sprintf("%s%s", resource.WebhookURLCustomHTTPHeaderCanonicalBaseKey, customHeaderRealKey)
customHeaderValue := "foo" customHeaderValue := "foo"
status := make(chan error, 2) status := make(chan error, 2)
rcv := echo.New() rcv := echo.New()

View File

@@ -80,7 +80,7 @@ func (ctx *Context) WithResource(directoryName string) error {
} }
// retrieve custom headers from request. // retrieve custom headers from request.
for key, value := range ctx.Request().Header { for key, value := range ctx.Request().Header {
r.WithCustomHeader(key, value[0]) r.WithCustomHTTPHeader(key, value[0])
} }
// retrieve form values from request. // retrieve form values from request.
for _, key := range resource.ArgKeys() { for _, key := range resource.ArgKeys() {

View File

@@ -5,15 +5,15 @@ import (
) )
const ( const (
// RemoteURLCustomHeaderCanonicalBaseKey is the base key // RemoteURLCustomHTTPHeaderCanonicalBaseKey is the base key
// of custom headers send to the remote URL. // of custom headers send to the remote URL.
RemoteURLCustomHeaderCanonicalBaseKey string = "Gotenberg-Remoteurl-" RemoteURLCustomHTTPHeaderCanonicalBaseKey string = "Gotenberg-Remoteurl-"
// WebhookURLCustomHeaderCanonicalBaseKey is the base key // WebhookURLCustomHTTPHeaderCanonicalBaseKey is the base key
// of custom headers send to the webhook URL. // of custom headers send to the webhook URL.
WebhookURLCustomHeaderCanonicalBaseKey string = "Gotenberg-Webhookurl-" WebhookURLCustomHTTPHeaderCanonicalBaseKey string = "Gotenberg-Webhookurl-"
) )
func fetchCustomHeaders(r Resource, baseKey string) map[string]string { func fetchCustomHTTPHeaders(r Resource, baseKey string) map[string]string {
customHeaders := make(map[string]string) customHeaders := make(map[string]string)
for key, value := range r.customHeaders { for key, value := range r.customHeaders {
if strings.Contains(key, baseKey) { if strings.Contains(key, baseKey) {
@@ -24,14 +24,14 @@ func fetchCustomHeaders(r Resource, baseKey string) map[string]string {
return customHeaders return customHeaders
} }
// RemoteURLCustomHeaders is a helper for retrieving // RemoteURLCustomHTTPHeaders is a helper for retrieving
// the custom headers for the URL conversion. // the custom headers for the URL conversion.
func RemoteURLCustomHeaders(r Resource) map[string]string { func RemoteURLCustomHTTPHeaders(r Resource) map[string]string {
return fetchCustomHeaders(r, RemoteURLCustomHeaderCanonicalBaseKey) return fetchCustomHTTPHeaders(r, RemoteURLCustomHTTPHeaderCanonicalBaseKey)
} }
// WebhookURLCustomHeaders is a helper for retrieving // WebhookURLCustomHTTPHeaders is a helper for retrieving
// the custom headers for the webhook URL. // the custom headers for the webhook URL.
func WebhookURLCustomHeaders(r Resource) map[string]string { func WebhookURLCustomHTTPHeaders(r Resource) map[string]string {
return fetchCustomHeaders(r, WebhookURLCustomHeaderCanonicalBaseKey) return fetchCustomHTTPHeaders(r, WebhookURLCustomHTTPHeaderCanonicalBaseKey)
} }

View File

@@ -17,16 +17,16 @@ func TestRemoteURLCustomHeaders(t *testing.T) {
// should find the custom header. // should find the custom header.
customHeaderValue := "bar" customHeaderValue := "bar"
customHeaderCanonicalRealKey := "Foo" customHeaderCanonicalRealKey := "Foo"
customHeaderCanonicalKey := http.CanonicalHeaderKey(fmt.Sprintf("%s%s", RemoteURLCustomHeaderCanonicalBaseKey, customHeaderCanonicalRealKey)) customHeaderCanonicalKey := http.CanonicalHeaderKey(fmt.Sprintf("%s%s", RemoteURLCustomHTTPHeaderCanonicalBaseKey, customHeaderCanonicalRealKey))
r.WithCustomHeader(customHeaderCanonicalKey, customHeaderValue) r.WithCustomHTTPHeader(customHeaderCanonicalKey, customHeaderValue)
r.WithCustomHeader("Bar", "Bar") r.WithCustomHTTPHeader("Bar", "Bar")
expected := map[string]string{ expected := map[string]string{
customHeaderCanonicalRealKey: customHeaderValue, customHeaderCanonicalRealKey: customHeaderValue,
} }
notExpected := map[string]string{ notExpected := map[string]string{
customHeaderCanonicalKey: customHeaderValue, customHeaderCanonicalKey: customHeaderValue,
} }
v := RemoteURLCustomHeaders(r) v := RemoteURLCustomHTTPHeaders(r)
assert.Equal(t, expected, v) assert.Equal(t, expected, v)
assert.NotEqual(t, notExpected, v) assert.NotEqual(t, notExpected, v)
} }
@@ -39,16 +39,16 @@ func TestWebhookURLCustomHeaders(t *testing.T) {
// should find the custom header. // should find the custom header.
customHeaderValue := "bar" customHeaderValue := "bar"
customHeaderCanonicalRealKey := "Foo" customHeaderCanonicalRealKey := "Foo"
customHeaderCanonicalKey := http.CanonicalHeaderKey(fmt.Sprintf("%s%s", WebhookURLCustomHeaderCanonicalBaseKey, customHeaderCanonicalRealKey)) customHeaderCanonicalKey := http.CanonicalHeaderKey(fmt.Sprintf("%s%s", WebhookURLCustomHTTPHeaderCanonicalBaseKey, customHeaderCanonicalRealKey))
r.WithCustomHeader(customHeaderCanonicalKey, customHeaderValue) r.WithCustomHTTPHeader(customHeaderCanonicalKey, customHeaderValue)
r.WithCustomHeader("Bar", "Bar") r.WithCustomHTTPHeader("Bar", "Bar")
expected := map[string]string{ expected := map[string]string{
customHeaderCanonicalRealKey: customHeaderValue, customHeaderCanonicalRealKey: customHeaderValue,
} }
notExpected := map[string]string{ notExpected := map[string]string{
customHeaderCanonicalKey: customHeaderValue, customHeaderCanonicalKey: customHeaderValue,
} }
v := WebhookURLCustomHeaders(r) v := WebhookURLCustomHTTPHeaders(r)
assert.Equal(t, expected, v) assert.Equal(t, expected, v)
assert.NotEqual(t, notExpected, v) assert.NotEqual(t, notExpected, v)
} }

View File

@@ -75,19 +75,19 @@ func (r Resource) Close() error {
return nil return nil
} }
// WithCustomHeader add a new custom header to the Resource. // WithCustomHTTPHeader add a new custom header to the Resource.
// Given key should be in canonical format. // Given key should be in canonical format.
func (r *Resource) WithCustomHeader(key string, value string) { func (r *Resource) WithCustomHTTPHeader(key string, value string) {
const op string = "resource.Resource.WithCustomHeader" const op string = "resource.Resource.WithCustomHTTPHeader"
// should already be in canonical format. // should already be in canonical format.
canonicalKey := http.CanonicalHeaderKey(key) canonicalKey := http.CanonicalHeaderKey(key)
if strings.Contains(canonicalKey, RemoteURLCustomHeaderCanonicalBaseKey) || if strings.Contains(canonicalKey, RemoteURLCustomHTTPHeaderCanonicalBaseKey) ||
strings.Contains(canonicalKey, WebhookURLCustomHeaderCanonicalBaseKey) { strings.Contains(canonicalKey, WebhookURLCustomHTTPHeaderCanonicalBaseKey) {
r.customHeaders[canonicalKey] = value r.customHeaders[canonicalKey] = value
r.logger.DebugfOp(op, "added '%s' with value '%s' to resource custom headers", canonicalKey, value) r.logger.DebugfOp(op, "added '%s' with value '%s' to resource custom HTTP headers", canonicalKey, value)
return return
} }
r.logger.DebugfOp(op, "skipping '%s' as it is not a custom header...", canonicalKey) r.logger.DebugfOp(op, "skipping '%s' as it is not a custom HTTP header...", canonicalKey)
} }
// WithArg add a new argument to the Resource. // WithArg add a new argument to the Resource.

View File

@@ -43,7 +43,7 @@ type ChromePrinterOptions struct {
MarginRight float64 MarginRight float64
Landscape bool Landscape bool
RpccBufferSize int64 RpccBufferSize int64
CustomHeaders map[string]string CustomHTTPHeaders map[string]string
} }
// DefaultChromePrinterOptions returns the default // DefaultChromePrinterOptions returns the default
@@ -63,7 +63,7 @@ func DefaultChromePrinterOptions(config conf.Config) ChromePrinterOptions {
MarginRight: 1.0, MarginRight: 1.0,
Landscape: false, Landscape: false,
RpccBufferSize: config.DefaultGoogleChromeRpccBufferSize(), RpccBufferSize: config.DefaultGoogleChromeRpccBufferSize(),
CustomHeaders: make(map[string]string), CustomHTTPHeaders: make(map[string]string),
} }
} }
@@ -148,7 +148,7 @@ func (p chromePrinter) Print(destination string) error {
return err return err
} }
// add custom headers (if any). // add custom headers (if any).
if err := p.setCustomHeaders(ctx, targetClient); err != nil { if err := p.setCustomHTTPHeaders(ctx, targetClient); err != nil {
return err return err
} }
// listen for all events. // listen for all events.
@@ -254,18 +254,18 @@ func (p chromePrinter) enableEvents(ctx context.Context, client *cdp.Client) err
return nil return nil
} }
func (p chromePrinter) setCustomHeaders(ctx context.Context, client *cdp.Client) error { func (p chromePrinter) setCustomHTTPHeaders(ctx context.Context, client *cdp.Client) error {
const op string = "printer.chromePrinter.setCustomHeaders" const op string = "printer.chromePrinter.setCustomHTTPHeaders"
resolver := func() error { resolver := func() error {
if len(p.opts.CustomHeaders) == 0 { if len(p.opts.CustomHTTPHeaders) == 0 {
p.logger.DebugOp(op, "skipping custom headers as none have been provided...") p.logger.DebugOp(op, "skipping custom HTTP headers as none have been provided...")
return nil return nil
} }
customHeaders := make(map[string]string) customHeaders := make(map[string]string)
// useless but for the logs. // useless but for the logs.
for key, value := range p.opts.CustomHeaders { for key, value := range p.opts.CustomHTTPHeaders {
customHeaders[key] = value customHeaders[key] = value
p.logger.DebugfOp(op, "set '%s' to custom header '%s'", value, key) p.logger.DebugfOp(op, "set '%s' to custom HTTP header '%s'", value, key)
} }
b, err := json.Marshal(customHeaders) b, err := json.Marshal(customHeaders)
if err != nil { if err != nil {