Beispiel #1
0
        public static IWebhook Map(WebhookModel model)
        {
            var webhook = new Webhook
            {
                Request = new WebhookRequest
                {
                    Url     = model.Request.Url,
                    Method  = model.Request.Method,
                    Headers = model.Request.Headers?.ToDictionary(x => x.Key, x => new WireMockList <string>(x.Value)) ?? new Dictionary <string, WireMockList <string> >()
                }
            };

            if (model.Request.UseTransformer == true)
            {
                webhook.Request.UseTransformer = true;
                if (!Enum.TryParse <TransformerType>(model.Request.TransformerType, out var transformerType))
                {
                    transformerType = TransformerType.Handlebars;
                }
                webhook.Request.TransformerType = transformerType;
            }

            IEnumerable <string> contentTypeHeader = null;

            if (webhook.Request.Headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)))
            {
                contentTypeHeader = webhook.Request.Headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)).Value;
            }

            if (model.Request.Body != null)
            {
                webhook.Request.BodyData = new BodyData
                {
                    BodyAsString     = model.Request.Body,
                    DetectedBodyType = BodyType.String,
                    DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
                };
            }
            else if (model.Request.BodyAsJson != null)
            {
                webhook.Request.BodyData = new BodyData
                {
                    BodyAsJson       = model.Request.BodyAsJson,
                    DetectedBodyType = BodyType.Json,
                    DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
                };
            }

            return(webhook);
        }