Skip to content

Commit 9653291

Browse files
authored
Updated Sample to be more simplified via Flurl
1 parent a75684b commit 9653291

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

README.md

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -187,52 +187,50 @@ Configuration Values:
187187
### Snippet:
188188
Because I talked about follow-through up above, I'd be amiss if I didn't provide a sample implementation of calling this code from .NET.
189189

190-
Assuming the use of the great *RESTSharp library* for REST api calls, and the Xsl-FO content is validated and parsed as an *XDocument* (Linq2Xml)... this sample should get you started on the .NET side as a client calleing the new PDF microservice.
190+
Assuming the use of the great *Flurl library* for REST api calls, and the Xsl-FO content is validated and parsed as an *XDocument* (Linq2Xml)... this sample should get you started on the .NET side as a client calleing the new PDF microservice.
191191

192-
*NOTE: Just use (RESTSharp)[https://restsharp.dev/] or (Flurl)[https://flurl.dev/] and avoid [incorrectly implementing HttpClient (hint, it should be a singleton)](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/)*
192+
*NOTE: Just use (Flurl)[https://flurl.dev/] or (RESTSharp)[https://restsharp.dev/] and avoid [incorrectly implementing HttpClient (hint, it should be a singleton)](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/)*
193193

194-
Snippet taken from the [implementation here](https://github.com/cajuncoding/PdfTemplating.XslFO), in my PdfTemplating project but is also readily available as a .NET Client in Nuget -- more details below.
194+
Here's a very simple client class that will get the job done! But this does not include functionality to handle debugging, viewing the event log which is returned in the response headers (and may be gzipped if large), etc. Therefore you might be interested in the readily available .NET Client that's available in Nuget -- more details below in the _**.NET Client**_ section.
195195

196196
```csharp
197-
using RestSharp;
198-
using RestSharp.CustomExtensions;
199197
using System;
200-
using System.CustomExtensions;
201-
using System.Threading.Tasks;
202-
using System.Xml.Linq;
198+
using System.Net.Mime;
199+
using System.Text;
200+
using Flurl;
201+
using Flurl.Http;
203202

204-
namespace PdfTemplating.XslFO.ApacheFOP.Serverless
203+
namespace PdfTemplating.XslFO.Render.ApacheFOP.Serverless
205204
{
206-
//READ from Configuration, or DI Constructor Injection
207-
private string apacheFOPServiceHost = "http://localhost:7071";
208-
private string apacheFOPServiceApi = "api/apache-fop/xslfo";
209-
210-
public static class ApacheFOPServerless
205+
public class ApacheFOPServerlessClient
211206
{
212-
public async Task<byte[]> RenderPdfBytesAsync(XDocument xslFODoc)
213-
{
214-
//Initialize the Xsl-FO microservice via configuration...
215-
var restClient = new RestClient(apacheFOPServiceHost);
216-
217-
//Get the Raw Xml Source for our Xsl-FO to be tansformed into Pdf binary...
218-
var xslFoSource = xslFODoc.ToString();
219-
220-
//Create the REST request for the Apache FOP micro-service...
221-
var restRequest = new RestRequest(apacheFOPServiceApi, Method.POST);
222-
restRequest.AddRawTextBody(xslFoSource, ContentType.Xml);
207+
public Uri ApacheFOPServerlessUri { get; protected set; }
208+
public string? AzFuncAuthCode { get; protected set; }
223209

224-
//Execute the request to the service, validate, and retrieve the Raw Binary resposne...
225-
var restResponse = await restClient.ExecuteWithExceptionHandlingAsync(restRequest);
210+
public ApacheFOPServerlessClient(Uri pdfServiceUri, string? azFuncAuthCode = null)
211+
{
212+
ApacheFOPServerlessUri = pdfServiceUri;
213+
AzFuncAuthCode = azFuncAuthCode;
214+
}
226215

227-
var pdfBytes = restResponse.RawBytes;
228-
return pdfBytes;
216+
public async Task<byte[]> RenderPdfAsync(string xslfoMarkup)
217+
{
218+
var pdfServiceUrl = ApacheFOPServerlessUri
219+
.SetQueryParam("code", AzFuncAuthCode, NullValueHandling.Remove);
220+
221+
using var response = await pdfServiceUrl.PostAsync(
222+
new StringContent(xslfoMarkup, Encoding.UTF8, MediaTypeNames.Application.Xml)
223+
);
224+
225+
var pdfBytes = await response.GetBytesAsync();
226+
return pdfBytes;
229227
}
230228
}
231229
}
232230
```
233231

234232
### .Net PdfTemplating (Full blown) Sample Implementation & .NET Client:
235-
A full blown implementation of `Razor Templating + ApacheFOP.Serverless` is my [PdfTemplating.XslFO project here](https://github.com/cajuncoding/PdfTemplating.XslFO).
233+
A full blown implementation of `Razor Templating + ApacheFOP.Serverless` is available in my [PdfTemplating.XslFO project here](https://github.com/cajuncoding/PdfTemplating.XslFO).
236234

237235
#### .NET Client
238236
The `PdfTemplating.XslFO` project also provides ready-to-use .NET Client for `ApacheFOP.Serverless` that is readily availalbe in Nuget: [PdfTemplating.XslFO.Render.ApacheFOP.Serverless](https://www.nuget.org/packages/PdfTemplating.XslFO.Render.ApacheFOP.Serverless/)

0 commit comments

Comments
 (0)