LiquidTransform.functionapp.v4/LiquidTransformer.cs
/// <summary>
/// Converts Json to XML using a Liquid mapping. The filename of the liquid map needs to be provided in the path. 
/// The tranformation is executed with the HTTP request body as input.
/// </summary>
/// <param name="req"></param>
/// <param name="inputBlob"></param>
/// <param name="log"></param>
/// <returns></returns>
Converts Json to XML using a Liquid mapping. The filename of the liquid map needs to be provided in the path.
The tranformation is executed with the HTTP request body as input.
Converts Json to XML using a Liquid mapping . The filename of the liquid map needs to be provided in the path . The tranformation is executed with the HTTP request body as input .
[FunctionName("LiquidTransformer")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "liquidtransformer/{liquidtransformfilename}")] HttpRequest req,
            string liquidtransformfilename,
            [Blob("liquid-transforms/{liquidtransformfilename}", FileAccess.Read)] Stream inputBlob,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string storageOverride = req.Query["storage"];
            if(!String.IsNullOrEmpty(storageOverride))
            {
                try 
                {
                    BlobServiceClient blobServiceClient = new BlobServiceClient(storageOverride);
                    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("liquid-transforms");
                    BlobClient blobClient = containerClient.GetBlobClient(liquidtransformfilename);
                    MemoryStream blobStream = new MemoryStream();
                    blobClient.DownloadTo(blobStream);
                    inputBlob = blobStream;
                    inputBlob.Position = 0;
                }
                catch(Exception ex)
                {
                    log.LogError(ex.Message, ex);
                    return new BadRequestObjectResult("Error getting liquid transform: " + ex);
                }
            }
            if (inputBlob == null)
            {
                log.LogError("inputBlob null");
                return new NotFoundObjectResult("Liquid transform not found");
            }
            string requestContentType = req.ContentType;
            string responseContentType = req.Headers["Accept"];
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var sr = new StreamReader(inputBlob);
            var liquidTransform = sr.ReadToEnd();
            var contentReader = ContentFactory.GetContentReader(requestContentType);
            var contentWriter = ContentFactory.GetContentWriter(responseContentType);
            Hash inputHash;
            try
            {
                inputHash = contentReader.ParseRequestAsync(requestBody);
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message, ex);
                return new BadRequestObjectResult("Error parsing request body: " + ex);
            }
            Template.RegisterFilter(typeof(CustomFilters));
            Template template;
            try
            {
                template = Template.Parse(liquidTransform);
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message, ex);
                return new BadRequestObjectResult("Error parsing Liquid template: " + ex);
            }
            string output = string.Empty;
            try
            {
                output = template.Render(inputHash);
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message, ex);
                return new BadRequestObjectResult("Error rendering Liquid template: " + ex);
            }
            if (template.Errors != null && template.Errors.Count > 0)
            {
                if (template.Errors[0].InnerException != null)
                {
                    return new BadRequestObjectResult(String.Format("Error rendering Liquid template: {0}", template.Errors[0].InnerException));
                }
                else
                {
                    return new BadRequestObjectResult(String.Format("Error rendering Liquid template: {0}", template.Errors[0].Message));
                }
            }
            try
            {
                var content = contentWriter.CreateResponse(output);
                return new OkObjectResult(content);
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message, ex);
                return new OkObjectResult(output);
            }
        }
[ FunctionName ( " " ) ] public static async Task < IActionResult > Run ( [ HttpTrigger ( AuthorizationLevel . Function , " " , Route = " " ) ] HttpRequest req , string liquidtransformfilename , [ Blob ( " " , FileAccess . Read ) ] Stream inputBlob , ILogger log ) { log . LogInformation ( " " ) ; string storageOverride = req . Query [ " " ] ; if ( ! String . IsNullOrEmpty ( storageOverride ) ) { try { BlobServiceClient blobServiceClient = new BlobServiceClient ( storageOverride ) ; BlobContainerClient containerClient = blobServiceClient . GetBlobContainerClient ( " " ) ; BlobClient blobClient = containerClient . GetBlobClient ( liquidtransformfilename ) ; MemoryStream blobStream = new MemoryStream ( ) ; blobClient . DownloadTo ( blobStream ) ; inputBlob = blobStream ; inputBlob . Position = 0 ; } catch ( Exception ex ) { log . LogError ( ex . Message , ex ) ; return new BadRequestObjectResult ( " " + ex ) ; } } if ( inputBlob == null ) { log . LogError ( " " ) ; return new NotFoundObjectResult ( " " ) ; } string requestContentType = req . ContentType ; string responseContentType = req . Headers [ " " ] ; string requestBody = await new StreamReader ( req . Body ) . ReadToEndAsync ( ) ; var sr = new StreamReader ( inputBlob ) ; var liquidTransform = sr . ReadToEnd ( ) ; var contentReader = ContentFactory . GetContentReader ( requestContentType ) ; var contentWriter = ContentFactory . GetContentWriter ( responseContentType ) ; Hash inputHash ; try { inputHash = contentReader . ParseRequestAsync ( requestBody ) ; } catch ( Exception ex ) { log . LogError ( ex . Message , ex ) ; return new BadRequestObjectResult ( " " + ex ) ; } Template . RegisterFilter ( typeof ( CustomFilters ) ) ; Template template ; try { template = Template . Parse ( liquidTransform ) ; } catch ( Exception ex ) { log . LogError ( ex . Message , ex ) ; return new BadRequestObjectResult ( " " + ex ) ; } string output = string . Empty ; try { output = template . Render ( inputHash ) ; } catch ( Exception ex ) { log . LogError ( ex . Message , ex ) ; return new BadRequestObjectResult ( " " + ex ) ; } if ( template . Errors != null && template . Errors . Count > 0 ) { if ( template . Errors [ 0 ] . InnerException != null ) { return new BadRequestObjectResult ( String . Format ( " " , template . Errors [ 0 ] . InnerException ) ) ; } else { return new BadRequestObjectResult ( String . Format ( " " , template . Errors [ 0 ] . Message ) ) ; } } try { var content = contentWriter . CreateResponse ( output ) ; return new OkObjectResult ( content ) ; } catch ( Exception ex ) { log . LogError ( ex . Message , ex ) ; return new OkObjectResult ( output ) ; } }
Converts Json to XML using a Liquid mapping.
Converts Json to XML using a Liquid mapping .
// This indicates the response content type. If set to application/json it will perform additional formatting // Otherwise the Liquid transform is returned unprocessed. // Load the Liquid transform in a string // Register the Liquid custom filter extensions // Execute the Liquid transform // Just log the error, and return the Liquid output without parsing
None
HttpRequest
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "liquidtransformer/{liquidtransformfilename}")]
liquidtransformfilename
string
Stream
[Blob("liquid-transforms/{liquidtransformfilename}", FileAccess.Read)]
log
ILogger
None
req
None
inputBlob
None