public static async Task Run([EventHubTrigger("%EventHubName%", Connection = "EventHub")] EventData[] events, ILogger log) { HttpClient httpClient = new HttpClient(); try { foreach (EventData eventData in events) { string value = Encoding.UTF8.GetString(eventData.Body); CustomerSendEvent customer = new CustomerSendEvent { CustomerId = value, AgentId = "EventHub" }; var url = Environment.GetEnvironmentVariable("apiEventInvokeurl", EnvironmentVariableTarget.Process); var content = JsonConvert.SerializeObject(customer); if (null != content) { var stringContent = new StringContent(content, UnicodeEncoding.UTF8, "application/json"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("apiAuthToken", EnvironmentVariableTarget.Process)); var result = await httpClient.PostAsync(url, stringContent); } else { throw new Exception("Failed to serialize object!"); } log.LogInformation("New Customer with Id: " + value + " was added to cosmos DB."); } } catch (Exception ex) { log.LogError(ex.Message); } }
public static void Run([CosmosDBTrigger( databaseName: "%databaseId%", collectionName: "%containerId%", ConnectionStringSetting = "AzureconnectionString", LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)] IReadOnlyList <Document> documents, ILogger log) { try { HttpClient httpClient = new HttpClient(); if (documents != null && documents.Count > 0) { log.LogInformation(documents[0].ToString()); log.LogInformation("Documents modified " + documents.Count); CustomerSendEvent customer = new CustomerSendEvent { CustomerId = documents[0].Id, AgentId = "ChangeFeed" }; var url = System.Environment.GetEnvironmentVariable("apiEventInvokeurl"); var content = JsonConvert.SerializeObject(customer); if (null != content) { var stringContent = new StringContent(content, UnicodeEncoding.UTF8, "application/json"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", System.Environment.GetEnvironmentVariable("apiAuthToken")); var result = httpClient.PostAsync(url, stringContent); } else { throw new Exception("Failed to serialize object!"); } } } catch (Exception ex) { log.LogError(ex.Message); } }
public static void Run([EventHubTrigger("%EventHubName%", Connection = "EventHub", ConsumerGroup = "$Default")] EventData[] events, TraceWriter log) { HttpClient httpClient = new HttpClient(); try { foreach (EventData eventData in events) { string value = ""; using (var reader = new StreamReader(eventData.GetBodyStream(), Encoding.UTF8)) { value = reader.ReadToEnd(); CustomerSendEvent customer = new CustomerSendEvent { CustomerId = value, AgentId = "EventHub" }; var url = System.Environment.GetEnvironmentVariable("apiEventInvokeurl"); var content = JsonConvert.SerializeObject(customer); if (null != content) { var stringContent = new StringContent(content, UnicodeEncoding.UTF8, "application/json"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", System.Environment.GetEnvironmentVariable("apiAuthToken")); var result = httpClient.PostAsync(url, stringContent); } else { throw new Exception("Failed to serialize object!"); } } log.Info(value); } } catch (Exception ex) { log.Error(ex.Message); } }