Skip to content

Commit 763ce10

Browse files
committed
Added these as extensions.
1 parent 0a01196 commit 763ce10

4 files changed

Lines changed: 136 additions & 82 deletions

File tree

lib/Domain/Builders/Faceted/DocumentBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public sealed class DocumentBuilder
2626

2727
public DocumentBuilder(FullDocument content, Action<bool> setContainsMarkdown)
2828
{
29-
if (content == null) throw new ArgumentNullException(nameof(content));
30-
31-
this._content = content;
29+
this._content = content ?? throw new ArgumentNullException(nameof(content));
3230
this._setContainsMarkdown = setContainsMarkdown;
3331
}
3432

lib/Domain/Builders/Faceted/HtmlConversionBehaviorBuilder.cs

Lines changed: 46 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public sealed class HtmlConversionBehaviorBuilder
2323

2424
internal HtmlConversionBehaviorBuilder(HtmlConversionBehaviors htmlConversionBehaviors)
2525
{
26-
this._htmlConversionBehaviors = htmlConversionBehaviors;
26+
_htmlConversionBehaviors = htmlConversionBehaviors;
2727
}
2828

2929
/// <summary>
@@ -34,7 +34,7 @@ internal HtmlConversionBehaviorBuilder(HtmlConversionBehaviors htmlConversionBeh
3434
/// <remarks>Prefer <see cref="SetBrowserWaitExpression" /> over waitDelay.</remarks>
3535
public HtmlConversionBehaviorBuilder SetBrowserWaitDelay(int seconds)
3636
{
37-
this._htmlConversionBehaviors.WaitDelay = $"{seconds}s";
37+
_htmlConversionBehaviors.WaitDelay = $"{seconds}s";
3838

3939
return this;
4040
}
@@ -49,9 +49,12 @@ public HtmlConversionBehaviorBuilder SetBrowserWaitDelay(int seconds)
4949
/// <exception cref="InvalidOperationException"></exception>
5050
public HtmlConversionBehaviorBuilder SetBrowserWaitExpression(string expression)
5151
{
52-
if (expression.IsNotSet()) throw new InvalidOperationException("expression is not set");
52+
if (expression.IsNotSet())
53+
{
54+
throw new InvalidOperationException("expression is not set");
55+
}
5356

54-
this._htmlConversionBehaviors.WaitForExpression = expression;
57+
_htmlConversionBehaviors.WaitForExpression = expression;
5558

5659
return this;
5760
}
@@ -65,9 +68,12 @@ public HtmlConversionBehaviorBuilder SetBrowserWaitExpression(string expression)
6568
[Obsolete("Deprecated in Gotenberg v8+")]
6669
public HtmlConversionBehaviorBuilder SetUserAgent(string userAgent)
6770
{
68-
if (userAgent.IsNotSet()) throw new InvalidOperationException("headerName is not set");
71+
if (userAgent.IsNotSet())
72+
{
73+
throw new InvalidOperationException("headerName is not set");
74+
}
6975

70-
this._htmlConversionBehaviors.UserAgent = userAgent;
76+
_htmlConversionBehaviors.UserAgent = userAgent;
7177

7278
return this;
7379
}
@@ -84,7 +90,7 @@ public HtmlConversionBehaviorBuilder AddAdditionalHeaders(string headerName, str
8490
{
8591
var header = string.Format("{0}{2}{1}", "{", "}", $"{'"'}{headerName}{'"'} : {'"'}{headerValue}{'"'}");
8692

87-
return this.AddAdditionalHeaders(JObject.Parse(header));
93+
return AddAdditionalHeaders(JObject.Parse(header));
8894
}
8995

9096
/// <summary>
@@ -95,85 +101,42 @@ public HtmlConversionBehaviorBuilder AddAdditionalHeaders(string headerName, str
95101
/// <exception cref="InvalidOperationException"></exception>
96102
public HtmlConversionBehaviorBuilder AddAdditionalHeaders(JObject extraHeaders)
97103
{
98-
if (extraHeaders == null) throw new InvalidOperationException("extraHeaders is null");
104+
if (extraHeaders == null)
105+
{
106+
throw new InvalidOperationException("extraHeaders is null");
107+
}
99108

100-
this._htmlConversionBehaviors.ExtraHeaders = extraHeaders;
109+
_htmlConversionBehaviors.ExtraHeaders = extraHeaders;
101110

102111
return this;
103112
}
104113

105114
/// <summary>
106-
/// Adds a cookie to store in the Chromium cookie jar.
115+
/// Adds a cookie to store in the Chromium cookie jar.
107116
/// </summary>
108117
/// <param name="cookie">The cookie to add</param>
109118
/// <returns></returns>
110119
/// <exception cref="ArgumentNullException"></exception>
111120
public HtmlConversionBehaviorBuilder AddCookie(Cookie cookie)
112121
{
113-
if (cookie == null) throw new ArgumentNullException(nameof(cookie));
122+
if (cookie == null)
123+
{
124+
throw new ArgumentNullException(nameof(cookie));
125+
}
114126

115-
this._htmlConversionBehaviors.Cookies ??= new List<Cookie>();
127+
_htmlConversionBehaviors.Cookies ??= new List<Cookie>();
116128

117129
Cookie.Validate(cookie);
118130

119-
this._htmlConversionBehaviors.Cookies.Add(cookie);
131+
_htmlConversionBehaviors.Cookies.Add(cookie);
120132

121133
return this;
122134
}
123135

124136
/// <summary>
125-
/// Adds a cookie to store in the Chromium cookie jar.
126-
/// </summary>
127-
/// <param name="name">Cookie name</param>
128-
/// <param name="value">Cookie value</param>
129-
/// <param name="domain">Cookie domain</param>
130-
/// <param name="path">Optional cookie path</param>
131-
/// <param name="secure">Optional secure flag</param>
132-
/// <param name="httpOnly">Optional HTTP-only flag</param>
133-
/// <param name="sameSite">Optional SameSite attribute ("Strict", "Lax", or "None")</param>
134-
/// <returns></returns>
135-
public HtmlConversionBehaviorBuilder AddCookie(
136-
string name,
137-
string value,
138-
string domain,
139-
string? path = null,
140-
bool? secure = null,
141-
bool? httpOnly = null,
142-
string? sameSite = null)
143-
{
144-
var cookie = new Cookie
145-
{
146-
Name = name,
147-
Value = value,
148-
Domain = domain,
149-
Path = path,
150-
Secure = secure,
151-
HttpOnly = httpOnly,
152-
SameSite = sameSite
153-
};
154-
155-
return AddCookie(cookie);
156-
}
157-
158-
/// <summary>
159-
/// Adds multiple cookies to store in the Chromium cookie jar.
160-
/// </summary>
161-
/// <param name="cookies">The cookies to add</param>
162-
/// <returns></returns>
163-
/// <exception cref="ArgumentNullException"></exception>
164-
public HtmlConversionBehaviorBuilder AddCookies(IEnumerable<Cookie> cookies)
165-
{
166-
if (cookies == null) throw new ArgumentNullException(nameof(cookies));
167-
168-
this._htmlConversionBehaviors.Cookies ??= new List<Cookie>();
169-
this._htmlConversionBehaviors.Cookies.AddRange(cookies);
170-
171-
return this;
172-
}
173-
174-
/// <summary>
175-
/// Sets the document metadata.
176-
/// Not all metadata are writable. Consider taking a look at https://exiftool.org/TagNames/XMP.html#pdf for an (exhaustive?) list of available metadata.
137+
/// Sets the document metadata.
138+
/// Not all metadata are writable. Consider taking a look at https://exiftool.org/TagNames/XMP.html#pdf for an
139+
/// (exhaustive?) list of available metadata.
177140
/// </summary>
178141
/// <param name="dictionary"></param>
179142
/// <returns></returns>
@@ -185,16 +148,20 @@ public HtmlConversionBehaviorBuilder SetMetadata(IDictionary<string, object> dic
185148
}
186149

187150
/// <summary>
188-
/// Sets the document metadata.
189-
/// Not all metadata are writable. Consider taking a look at https://exiftool.org/TagNames/XMP.html#pdf for an (exhaustive?) list of available metadata.
151+
/// Sets the document metadata.
152+
/// Not all metadata are writable. Consider taking a look at https://exiftool.org/TagNames/XMP.html#pdf for an
153+
/// (exhaustive?) list of available metadata.
190154
/// </summary>
191155
/// <param name="metadata"></param>
192156
/// <returns></returns>
193157
public HtmlConversionBehaviorBuilder SetMetadata(JObject metadata)
194158
{
195-
if (metadata == null) throw new InvalidOperationException("metadata is null");
159+
if (metadata == null)
160+
{
161+
throw new InvalidOperationException("metadata is null");
162+
}
196163

197-
this._htmlConversionBehaviors.MetaData = metadata;
164+
_htmlConversionBehaviors.MetaData = metadata;
198165

199166
return this;
200167
}
@@ -205,7 +172,7 @@ public HtmlConversionBehaviorBuilder SetMetadata(JObject metadata)
205172
/// <returns></returns>
206173
public HtmlConversionBehaviorBuilder FailOnConsoleExceptions()
207174
{
208-
this._htmlConversionBehaviors.FailOnConsoleExceptions = true;
175+
_htmlConversionBehaviors.FailOnConsoleExceptions = true;
209176

210177
return this;
211178
}
@@ -216,18 +183,18 @@ public HtmlConversionBehaviorBuilder FailOnConsoleExceptions()
216183
/// <returns></returns>
217184
public HtmlConversionBehaviorBuilder EmulateAsScreen()
218185
{
219-
this._htmlConversionBehaviors.EmulatedMediaType = "screen";
186+
_htmlConversionBehaviors.EmulatedMediaType = "screen";
220187

221188
return this;
222189
}
223190

224191
/// <summary>
225-
/// Gotenberg 8+ ONLY: Configures gotenberg to not wait for Chromium network to be idle.
192+
/// Gotenberg 8+ ONLY: Configures gotenberg to not wait for Chromium network to be idle.
226193
/// </summary>
227194
/// <returns></returns>
228195
public HtmlConversionBehaviorBuilder SkipNetworkIdleEvent()
229196
{
230-
this._htmlConversionBehaviors.SkipNetworkIdleEvent = true;
197+
_htmlConversionBehaviors.SkipNetworkIdleEvent = true;
231198

232199
return this;
233200
}
@@ -240,9 +207,12 @@ public HtmlConversionBehaviorBuilder SkipNetworkIdleEvent()
240207
/// <exception cref="InvalidOperationException"></exception>
241208
public HtmlConversionBehaviorBuilder SetPdfFormat(ConversionPdfFormats format)
242209
{
243-
if (format == default) throw new InvalidOperationException("Invalid PDF format specified");
210+
if (format == default)
211+
{
212+
throw new InvalidOperationException("Invalid PDF format specified");
213+
}
244214

245-
this._htmlConversionBehaviors.PdfFormat = format;
215+
_htmlConversionBehaviors.PdfFormat = format;
246216

247217
return this;
248218
}
@@ -252,7 +222,7 @@ public HtmlConversionBehaviorBuilder SetPdfFormat(ConversionPdfFormats format)
252222
/// </summary>
253223
public HtmlConversionBehaviorBuilder SetPdfUa(bool enablePdfUa = true)
254224
{
255-
this._htmlConversionBehaviors.EnablePdfUa = enablePdfUa;
225+
_htmlConversionBehaviors.EnablePdfUa = enablePdfUa;
256226

257227
return this;
258228
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
2+
// and GotenbergSharpApiClient Contributors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
17+
18+
public static class HtmlConversionBehaviorBuilderExtensions
19+
{
20+
/// <summary>
21+
/// Adds a cookie to store in the Chromium cookie jar.
22+
/// </summary>
23+
/// <param name="builder">HtmlConversionBehaviorBuilder</param>
24+
/// <param name="name">Cookie name</param>
25+
/// <param name="value">Cookie value</param>
26+
/// <param name="domain">Cookie domain</param>
27+
/// <param name="path">Optional cookie path</param>
28+
/// <param name="secure">Optional secure flag</param>
29+
/// <param name="httpOnly">Optional HTTP-only flag</param>
30+
/// <param name="sameSite">Optional SameSite attribute ("Strict", "Lax", or "None")</param>
31+
/// <returns></returns>
32+
public static HtmlConversionBehaviorBuilder AddCookie(this HtmlConversionBehaviorBuilder builder,
33+
string name,
34+
string value,
35+
string domain,
36+
string? path = null,
37+
bool? secure = null,
38+
bool? httpOnly = null,
39+
string? sameSite = null)
40+
{
41+
if (builder == null)
42+
{
43+
throw new ArgumentNullException(nameof(builder));
44+
}
45+
46+
var cookie = new Cookie
47+
{
48+
Name = name,
49+
Value = value,
50+
Domain = domain,
51+
Path = path,
52+
Secure = secure,
53+
HttpOnly = httpOnly,
54+
SameSite = sameSite
55+
};
56+
57+
return builder.AddCookie(cookie);
58+
}
59+
60+
/// <summary>
61+
/// Adds multiple cookies to store in the Chromium cookie jar.
62+
/// </summary>
63+
/// <param name="builder">HtmlConversionBehaviorBuilder</param>
64+
/// <param name="cookies">The cookies to add</param>
65+
/// <returns></returns>
66+
/// <exception cref="ArgumentNullException"></exception>
67+
public static HtmlConversionBehaviorBuilder AddCookies(this HtmlConversionBehaviorBuilder builder, IEnumerable<Cookie> cookies)
68+
{
69+
if (builder == null)
70+
{
71+
throw new ArgumentNullException(nameof(builder));
72+
}
73+
74+
if (cookies == null)
75+
{
76+
throw new ArgumentNullException(nameof(cookies));
77+
}
78+
79+
foreach (var c in cookies)
80+
{
81+
builder.AddCookie(c);
82+
}
83+
84+
return builder;
85+
}
86+
}

test/GotenbergSharpClient.Tests/HtmlConversionBehaviorBuilderTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Gotenberg.Sharp.API.Client.Domain.Builders;
2+
using Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
23
using Gotenberg.Sharp.API.Client.Domain.Requests;
3-
using Gotenberg.Sharp.API.Client.Domain.Requests.ApiRequests;
44
using Gotenberg.Sharp.API.Client.Domain.Requests.Facets;
55
using Newtonsoft.Json.Linq;
66

@@ -110,7 +110,7 @@ public void AddCookies_WithEnumerable_AddsAllCookies()
110110

111111
cookieContent.Should().NotBeNull("Cookie content should be present in HTTP request");
112112

113-
var contentString = cookieContent!.ReadAsStringAsync().Result;
113+
var contentString = cookieContent.ReadAsStringAsync().Result;
114114
var jArray = JArray.Parse(contentString);
115115

116116
jArray.Should().HaveCount(3);
@@ -186,7 +186,7 @@ public void Builder_WithCookies_ProducesCorrectJsonInHttpContent()
186186

187187
// Assert
188188
cookieContent.Should().NotBeNull();
189-
var contentString = cookieContent!.ReadAsStringAsync().Result;
189+
var contentString = cookieContent.ReadAsStringAsync().Result;
190190
var jArray = JArray.Parse(contentString);
191191

192192
jArray.Should().HaveCount(2);

0 commit comments

Comments
 (0)