Skip to content

Commit

Permalink
Added "version" support -- renaming all to "SetX" and deprecating old…
Browse files Browse the repository at this point in the history
… methods.
  • Loading branch information
Jaben committed Feb 7, 2025
1 parent e0d6289 commit 6813c91
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 118 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void ConfigureServices(IServiceCollection services)
var builder = new HtmlRequestBuilder()
.AddDocument(doc =>
doc.SetBody(GetBody()).SetFooter(GetFooter())
).WithDimensions(dims =>
).WithPageProperties(pp =>
{
dims.SetPaperSize(PaperSizes.A3)
pp.SetPaperSize(PaperSizes.A3)
.SetMargins(Margins.None)
.SetScale(.99);
}).WithAsyncAssets(async assets => assets.AddItem("some-image.jpg", await GetImageBytes()));
Expand Down Expand Up @@ -95,12 +95,12 @@ public async Task<Stream> CreateFromUrl(string headerPath, string footerPath)
.AddAsyncHeaderFooter(async
doc => doc.SetHeader(await File.ReadAllTextAsync(headerPath))
.SetFooter(await File.ReadAllBytesAsync(footerPath)
)).WithDimensions(dims =>
)).WithPageProperties(pp =>
{
dims.SetPaperSize(PaperSizes.A4)
pp.SetPaperSize(PaperSizes.A4)
.SetMargins(Margins.None)
.SetScale(.90)
.LandScape();
.SetLandscape();
});

var request = await builder.BuildAsync();
Expand Down Expand Up @@ -130,11 +130,11 @@ public async Task<Stream> CreateFromMarkdown()
.AddAsyncDocument(async
doc => doc.SetHeader(await this.GetHeaderAsync())
.SetBody(await GetBodyAsync())
.ContainsMarkdown()
.SetContainsMarkdown()
.SetFooter(await GetFooterAsync())
).WithDimensions(dims =>
).WithPageProperties(pp =>
{
dims.UseChromeDefaults().LandScape().SetScale(.90);
pp.UseChromeDefaults().SetLandscape().SetScale(.90);
}).WithAsyncAssets(async
a => a.AddItems(await GetMarkdownAssets())
);
Expand Down Expand Up @@ -169,7 +169,7 @@ public async Task<Stream> CreateFromMarkdown()
dimBuilder.SetPaperSize(PaperSizes.A4)
.SetMargins(Margins.None)
.SetScale(.90)
.LandScape();
.SetLandscape();
});

var request = await builder.BuildAsync();
Expand Down Expand Up @@ -217,9 +217,9 @@ IEnumerable<UrlRequestBuilder> CreateBuilders(IEnumerable<Uri> uris)
{
dimBuilder.UseChromeDefaults()
.SetScale(.90)
.LandScape()
.MarginLeft(.5)
.MarginRight(.5);
.SetLandscape()
.SetMarginLeft(.5)
.SetMarginRight(.5);
});
}

Expand Down
7 changes: 7 additions & 0 deletions lib/Domain/Builders/Faceted/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ public DocumentBuilder(FullDocument content, Action<bool> setContainsMarkdown)

#region body

[Obsolete("Use SetContainsMarkdown()")]
public DocumentBuilder ContainsMarkdown(bool containsMarkdown = true)
{
this._setContainsMarkdown(containsMarkdown);
return this;
}

public DocumentBuilder SetContainsMarkdown(bool containsMarkdown = true)
{
this._setContainsMarkdown(containsMarkdown);
return this;
}

public DocumentBuilder SetBody(ContentItem body)
{
this._content.Body = body ?? throw new ArgumentNullException(nameof(body));
Expand Down
54 changes: 48 additions & 6 deletions lib/Domain/Builders/Faceted/PagePropertyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,85 @@ public PagePropertyBuilder SetScale(double scale)
return this;
}

[Obsolete("Use SetPaperWidth")]
public PagePropertyBuilder PaperWidth(double width)
{
this._pageProperties.PaperWidth = width;
return this;
}

[Obsolete("Use SetPaperHeight")]
public PagePropertyBuilder PaperHeight(double height)
{
this._pageProperties.PaperHeight = height;
return this;
}

[Obsolete("Use SetMarginTop")]
public PagePropertyBuilder MarginTop(double marginTop)
{
this._pageProperties.MarginTop = marginTop;
return this;
}

[Obsolete("Use SetMarginBottom")]
public PagePropertyBuilder MarginBottom(double marginBottom)
{
this._pageProperties.MarginBottom = marginBottom;
return this;
}

[Obsolete("Use SetMarginLeft")]
public PagePropertyBuilder MarginLeft(double marginLeft)
{
this._pageProperties.MarginLeft = marginLeft;
return this;
}

[Obsolete("Use SetMarginRight")]
public PagePropertyBuilder MarginRight(double marginRight)
{
this._pageProperties.MarginRight = marginRight;
return this;
}

[Obsolete("Use Landscape()")]
public PagePropertyBuilder SetPaperWidth(double width)
{
this._pageProperties.PaperWidth = width;
return this;
}

public PagePropertyBuilder SetPaperHeight(double height)
{
this._pageProperties.PaperHeight = height;
return this;
}

public PagePropertyBuilder SetMarginTop(double marginTop)
{
this._pageProperties.MarginTop = marginTop;
return this;
}

public PagePropertyBuilder SetMarginBottom(double marginBottom)
{
this._pageProperties.MarginBottom = marginBottom;
return this;
}

public PagePropertyBuilder SetMarginLeft(double marginLeft)
{
this._pageProperties.MarginLeft = marginLeft;
return this;
}

public PagePropertyBuilder SetMarginRight(double marginRight)
{
this._pageProperties.MarginRight = marginRight;
return this;
}

[Obsolete("Use SetLandscape()")]
public PagePropertyBuilder LandScape(bool landscape = true)
{
this._pageProperties.Landscape = landscape;
Expand All @@ -104,7 +146,7 @@ public PagePropertyBuilder LandScape(bool landscape = true)
/// </summary>
/// <param name="landscape"></param>
/// <returns></returns>
public PagePropertyBuilder Landscape(bool landscape = true)
public PagePropertyBuilder SetLandscape(bool landscape = true)
{
this._pageProperties.Landscape = landscape;
return this;
Expand All @@ -115,7 +157,7 @@ public PagePropertyBuilder Landscape(bool landscape = true)
/// </summary>
/// <param name="prefer"></param>
/// <returns></returns>
public PagePropertyBuilder PreferCssPageSize(bool prefer = true)
public PagePropertyBuilder SetPreferCssPageSize(bool prefer = true)
{
this._pageProperties.PreferCssPageSize = prefer;
return this;
Expand All @@ -126,7 +168,7 @@ public PagePropertyBuilder PreferCssPageSize(bool prefer = true)
/// </summary>
/// <param name="omitBackground"></param>
/// <returns></returns>
public PagePropertyBuilder OmitBackground(bool omitBackground = true)
public PagePropertyBuilder SetOmitBackground(bool omitBackground = true)
{
this._pageProperties.OmitBackground = omitBackground;
return this;
Expand All @@ -137,7 +179,7 @@ public PagePropertyBuilder OmitBackground(bool omitBackground = true)
/// </summary>
/// <param name="printBackground"></param>
/// <returns></returns>
public PagePropertyBuilder PrintBackground(bool printBackground = true)
public PagePropertyBuilder SetPrintBackground(bool printBackground = true)
{
this._pageProperties.PrintBackground = printBackground;
return this;
Expand All @@ -148,7 +190,7 @@ public PagePropertyBuilder PrintBackground(bool printBackground = true)
/// </summary>
/// <param name="generateDocumentOutline"></param>
/// <returns></returns>
public PagePropertyBuilder GenerateDocumentOutline(bool generateDocumentOutline = true)
public PagePropertyBuilder SetGenerateDocumentOutline(bool generateDocumentOutline = true)
{
this._pageProperties.GenerateDocumentOutline = generateDocumentOutline;
return this;
Expand Down
46 changes: 46 additions & 0 deletions lib/Domain/Requests/ApiRequests/GetApiRequestImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
// and GotenbergSharpApiClient Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Gotenberg.Sharp.API.Client.Domain.Requests.ApiRequests;

internal sealed class GetApiRequestImpl : IApiRequest
{
internal GetApiRequestImpl(string apiPath, ILookup<string, string?>? headers = null, bool isWebhookRequest = false)
{
this.ApiPath = apiPath;
this.IsWebhookRequest = isWebhookRequest;
this.Headers = headers;
}

public string ApiPath { get; }

public ILookup<string, string?>? Headers { get; }

private const string BoundaryPrefix = Constants.HttpContent.MultipartData.BoundaryPrefix;

public bool IsWebhookRequest { get; }

public HttpRequestMessage ToApiRequestMessage()
{
var message = new HttpRequestMessage(HttpMethod.Get, this.ApiPath);

foreach (var header in this.Headers.IfNullEmpty())
{
message.Headers.Add(header.Key, header);
}

return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Gotenberg.Sharp.API.Client.Domain.Requests;
namespace Gotenberg.Sharp.API.Client.Domain.Requests.ApiRequests;

public interface IApiRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2024 Chris Mohan, Jaben Cargman
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
// and GotenbergSharpApiClient Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,53 +13,46 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Gotenberg.Sharp.API.Client.Domain.Requests;
namespace Gotenberg.Sharp.API.Client.Domain.Requests.ApiRequests;

internal sealed class ApiRequestImplementation : IApiRequest, IConvertToHttpContent
internal sealed class PostApiRequestImpl : IApiRequest, IConvertToHttpContent
{
private readonly Func<IEnumerable<HttpContent>> _toHttpContent;

internal ApiRequestImplementation(
internal PostApiRequestImpl(
Func<IEnumerable<HttpContent>> toHttpContent,
string apiPath,
ILookup<string, string?> headers,
ILookup<string, string?>? headers,
bool isWebhookRequest)
{
this._toHttpContent = toHttpContent;
this.ApiPath = apiPath;
this.IsWebhookRequest = isWebhookRequest;
this.Headers = headers;
}

public IEnumerable<HttpContent> ToHttpContent()
{
return this._toHttpContent();
_toHttpContent = toHttpContent;
ApiPath = apiPath;
IsWebhookRequest = isWebhookRequest;
Headers = headers;
}

public string ApiPath { get; }

public bool IsWebhookRequest { get; }

public ILookup<string, string?> Headers { get; }
public ILookup<string, string?>? Headers { get; }

private const string BoundaryPrefix = Constants.HttpContent.MultipartData.BoundaryPrefix;

public bool IsWebhookRequest { get; }

public HttpRequestMessage ToApiRequestMessage()
{
var formContent =
new MultipartFormDataContent($"{BoundaryPrefix}{DateTime.Now.Ticks}");
var formContent = new MultipartFormDataContent($"{BoundaryPrefix}{DateTime.Now.Ticks}");

foreach (var item in this.ToHttpContent()) formContent.Add(item);
foreach (var item in ToHttpContent()) formContent.Add(item);

var message = new HttpRequestMessage(HttpMethod.Post, this.ApiPath)
{
Content = formContent
};
var message = new HttpRequestMessage(HttpMethod.Post, ApiPath) { Content = formContent };

if (this.Headers.Any())
foreach (var header in this.Headers)
if (Headers?.Any() ?? false)
foreach (var header in Headers)
message.Headers.Add(header.Key, header);

return message;
}

public IEnumerable<HttpContent> ToHttpContent() => _toHttpContent();
}
4 changes: 2 additions & 2 deletions lib/Domain/Requests/BuildRequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.


using Gotenberg.Sharp.API.Client.Domain.Requests.ApiRequests;

namespace Gotenberg.Sharp.API.Client.Domain.Requests;

Expand Down Expand Up @@ -56,6 +56,6 @@ public virtual IApiRequest CreateApiRequest()
var headers = (this.Config?.GetHeaders()).IfNullEmpty()
.ToLookup(s => s.Name, s => s.Value);

return new ApiRequestImplementation(this.ToHttpContent, this.ApiPath, headers, isWebHook);
return new PostApiRequestImpl(this.ToHttpContent, this.ApiPath, headers, isWebHook);
}
}
Loading

0 comments on commit 6813c91

Please sign in to comment.