LiquidTransform.functionapp.v1/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<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "liquidtransformer/{liquidtransformfilename}")] HttpRequestMessage req,
            [Blob("liquid-transforms/{liquidtransformfilename}", FileAccess.Read)] Stream inputBlob,
            TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
            if (inputBlob == null)
            {
                log.Error("inputBlog null");
                return req.CreateErrorResponse(HttpStatusCode.NotFound, "Liquid transform not found");
            }
            string requestContentType = req.Content.Headers.ContentType.MediaType;
            string responseContentType = req.Headers.Accept.FirstOrDefault().MediaType;
            var sr = new StreamReader(inputBlob);
            var liquidTransform = sr.ReadToEnd();
            var contentReader = ContentFactory.GetContentReader(requestContentType);
            var contentWriter = ContentFactory.GetContentWriter(responseContentType);
            Hash inputHash;
            try
            {
                inputHash = await contentReader.ParseRequestAsync(req.Content);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error parsing request body", ex);
            }
            Template.RegisterFilter(typeof(CustomFilters));
            Template template;
            try
            {
                template = Template.Parse(liquidTransform);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error parsing Liquid template", ex);
            }
            string output = string.Empty;
            try
            {
                output = template.Render(inputHash);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error rendering Liquid template", ex);
            }
            if (template.Errors != null && template.Errors.Count > 0)
            {
                if (template.Errors[0].InnerException != null)
                {
                    return req.CreateErrorResponse(HttpStatusCode.InternalServerError, $"Error rendering Liquid template: {template.Errors[0].Message}", template.Errors[0].InnerException);
                }
                else
                {
                    return req.CreateErrorResponse(HttpStatusCode.InternalServerError, $"Error rendering Liquid template: {template.Errors[0].Message}");
                }
            }
            try
            {
                var content = contentWriter.CreateResponse(output);
                return new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = content
                };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(output, Encoding.UTF8, responseContentType)
                };
            }
        }
[ FunctionName ( " " ) ] public static async Task < HttpResponseMessage > Run ( [ HttpTrigger ( AuthorizationLevel . Function , " " , Route = " " ) ] HttpRequestMessage req , [ Blob ( " " , FileAccess . Read ) ] Stream inputBlob , TraceWriter log ) { log . Info ( " " ) ; if ( inputBlob == null ) { log . Error ( " " ) ; return req . CreateErrorResponse ( HttpStatusCode . NotFound , " " ) ; } string requestContentType = req . Content . Headers . ContentType . MediaType ; string responseContentType = req . Headers . Accept . FirstOrDefault ( ) . MediaType ; var sr = new StreamReader ( inputBlob ) ; var liquidTransform = sr . ReadToEnd ( ) ; var contentReader = ContentFactory . GetContentReader ( requestContentType ) ; var contentWriter = ContentFactory . GetContentWriter ( responseContentType ) ; Hash inputHash ; try { inputHash = await contentReader . ParseRequestAsync ( req . Content ) ; } catch ( Exception ex ) { log . Error ( ex . Message , ex ) ; return req . CreateErrorResponse ( HttpStatusCode . InternalServerError , " " , ex ) ; } Template . RegisterFilter ( typeof ( CustomFilters ) ) ; Template template ; try { template = Template . Parse ( liquidTransform ) ; } catch ( Exception ex ) { log . Error ( ex . Message , ex ) ; return req . CreateErrorResponse ( HttpStatusCode . InternalServerError , " " , ex ) ; } string output = string . Empty ; try { output = template . Render ( inputHash ) ; } catch ( Exception ex ) { log . Error ( ex . Message , ex ) ; return req . CreateErrorResponse ( HttpStatusCode . InternalServerError , " " , ex ) ; } if ( template . Errors != null && template . Errors . Count > 0 ) { if ( template . Errors [ 0 ] . InnerException != null ) { return req . CreateErrorResponse ( HttpStatusCode . InternalServerError , $" Error rendering Liquid template:  { template . Errors [ 0 ] . Message } " , template . Errors [ 0 ] . InnerException ) ; } else { return req . CreateErrorResponse ( HttpStatusCode . InternalServerError , $" Error rendering Liquid template:  { template . Errors [ 0 ] . Message } " ) ; } } try { var content = contentWriter . CreateResponse ( output ) ; return new HttpResponseMessage ( HttpStatusCode . OK ) { Content = content } ; } catch ( Exception ex ) { log . Error ( ex . Message , ex ) ; return new HttpResponseMessage ( HttpStatusCode . OK ) { Content = new StringContent ( output , Encoding . UTF8 , responseContentType ) } ; } }
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
HttpRequestMessage
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "liquidtransformer/{liquidtransformfilename}")]
Stream
[Blob("liquid-transforms/{liquidtransformfilename}", FileAccess.Read)]
log
TraceWriter
None
req
None
inputBlob
None