Exemple #1
0
        public static async Task PushNotification(NotifData notifData, string id, string subject, string eventType)
        {
            var credentials    = new Microsoft.Azure.EventGrid.Models.TopicCredentials(sasKey);
            var client         = new Microsoft.Azure.EventGrid.EventGridClient(credentials);
            var eventGridEvent = new Microsoft.Azure.EventGrid.Models.EventGridEvent
            {
                Subject     = subject,
                EventType   = eventType,
                EventTime   = DateTime.UtcNow,
                Id          = id,
                Data        = notifData,
                DataVersion = "1.0.0",
            };
            var events = new List <Microsoft.Azure.EventGrid.Models.EventGridEvent>();

            events.Add(eventGridEvent);
            await client.PublishEventsWithHttpMessagesAsync(topicHostName, events);
        }
Exemple #2
0
        private static async Task sendEventGridMessageWithEventGridClientAsync(string topic, string subject, object data)
        {
            var credentials = new Microsoft.Azure.EventGrid.Models.TopicCredentials(topicKey);

            var client = new Microsoft.Azure.EventGrid.EventGridClient(credentials);

            var eventGridEvent = new Microsoft.Azure.EventGrid.Models.EventGridEvent
            {
                Subject     = subject,
                EventType   = "func-event",
                EventTime   = DateTime.UtcNow,
                Id          = Guid.NewGuid().ToString(),
                Data        = data,
                DataVersion = "1.0.0",
            };
            var events = new List <Microsoft.Azure.EventGrid.Models.EventGridEvent>();

            events.Add(eventGridEvent);
            await client.PublishEventsWithHttpMessagesAsync(topic, events);
        }
Exemple #3
0
        private static async Task SendReminderNotificationAsync(string subject, object data)
        {
            var topicEndpoint = Environment.GetEnvironmentVariable("topicEndpoint");
            var sasKey        = Environment.GetEnvironmentVariable("sasKey");

            var credentials    = new Microsoft.Azure.EventGrid.Models.TopicCredentials(topicEndpoint);
            var client         = new Microsoft.Azure.EventGrid.EventGridClient(credentials);
            var eventGridEvent = new Microsoft.Azure.EventGrid.Models.EventGridEvent
            {
                Subject     = subject,
                EventType   = "REMINDERITEMSINCART",
                EventTime   = DateTime.UtcNow,
                Id          = Guid.NewGuid().ToString(),
                Data        = data,
                DataVersion = "1.0.0",
            };
            var events = new List <Microsoft.Azure.EventGrid.Models.EventGridEvent>();

            events.Add(eventGridEvent);
            await client.PublishEventsWithHttpMessagesAsync(topicHostName, events);
        }
Exemple #4
0
 public static void Run([EventGridTrigger] Microsoft.Azure.EventGrid.Models.EventGridEvent eventGridEvent, TraceWriter log)
 {
     log.Info(eventGridEvent.Data.ToString());
 }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequest req,
            CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogEvent(LogEventIds.FunctionAppShuttingDown, LogEventIds.FunctionAppShuttingDown.Name);
                throw new OperationCanceledException("Function invoked with a canceled cancellation token.");
            }

            try
            {
                cancellationToken.Register(() =>
                {
                    _logger.LogEvent(LogEventIds.FunctionAppShuttingDown, LogEventIds.FunctionAppShuttingDown.Name);
                });

                _logger.LogEvent(LogEventIds.CallbackFunctionTriggered, $"C# HTTP trigger function processed a request. Path={req?.Path}");

                // TODO: not sure why we have to get the byte array.. might be legacy code
                // MemoryStream stream = new MemoryStream();
                // await req.Body.CopyToAsync(stream);
                // byte[] bodyByteArray = stream.ToArray();

                string requestBody;
                using (var sr = new StreamReader(req.Body))
                {
                    requestBody = await sr.ReadToEndAsync().ConfigureAwait(false);
                }

                if (req.Headers.TryGetValue("ms-signature", out _))
                {
                    // TODO: need to verify headers here. However not sure how to get the signing key.
                    MediaServicesV2NotificationMessage notificationMessage = JsonConvert.DeserializeObject <MediaServicesV2NotificationMessage>(requestBody);

                    var eventGridEventId = System.Guid.NewGuid().ToString();
                    var eventToPublish   = new Microsoft.Azure.EventGrid.Models.EventGridEvent
                    {
                        Id          = eventGridEventId,
                        Data        = notificationMessage,
                        EventTime   = System.DateTime.UtcNow,
                        EventType   = CustomEventTypes.ResponseEncodeMediaServicesV2TranslateCallback,
                        Subject     = $"/{CustomEventTypes.ResponseEncodeMediaServicesV2TranslateCallback}/{eventGridEventId}",
                        DataVersion = "1.0",
                    };

                    await _publisher.PublishEventToTopic(eventToPublish).ConfigureAwait(false);

                    _logger.LogEvent(LogEventIds.CallbackFunctionNotificationMessageProcessed, "processed notification message");
                }
                else
                {
                    _logger.LogEvent(LogEventIds.RequestIsMissingVerifyWebHookRequestSignature, "VerifyWebHookRequestSignature failed.");
                    return(new BadRequestObjectResult("VerifyWebHookRequestSignature failed."));
                }

                return(new OkObjectResult("OK"));
            }
            catch (OperationCanceledException oce)
            {
                _logger.LogEventObject(LogEventIds.OperationCancelException, oce);
                throw;
            }
        }