Example #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "URLEndpoint")] HttpRequestMessage req,
                                                           ILogger log, [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper)
        {
            string noti;
            string bearer = string.Empty;

            Models.Notification notification;

            try
            {
                notification = await httpRequestMessageHelper.GetMessageFromRequest <Models.Notification>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

            if (notification == null)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(req));
            }

            string authHeader = string.Empty;

            if (req.Headers.TryGetValues("Authorization", out IEnumerable <string> authToken))
            {
                authHeader = authToken.First();
            }
            else
            {
                log.LogInformation("Authorization header error !");
            }

            if (notification.ResourceURL != null)
            {
                if (notification.ResourceURL.ToString().Contains("collections"))
                {
                    var lastIndexOf = notification.ResourceURL.ToString().LastIndexOf("/", StringComparison.Ordinal);
                    if (lastIndexOf != -1)
                    {
                        var collectionId = notification.ResourceURL.ToString().Substring(lastIndexOf + 1);

                        if (Guid.TryParse(collectionId, out var collectionGuid))
                        {
                            notification.CollectionId = collectionGuid;
                        }
                    }
                }
            }

            noti = "Customer Id : " + notification.CustomerId + Environment.NewLine +
                   "URL : " + notification.ResourceURL + Environment.NewLine +
                   "LastModifiedDate : " + notification.LastModifiedDate + Environment.NewLine +
                   "Touchpoint Id : " + notification.TouchpointId + Environment.NewLine +
                   "Collection Id : " + notification.CollectionId + Environment.NewLine +
                   "Bearer : " + authHeader;

            log.LogInformation(noti);

            await SaveNotificationToDatabase.SaveNotificationToDBAsync(notification);

            return(HttpResponseMessageHelper.Ok(noti));
        }