Skip to content

Instantly share code, notes, and snippets.

Last active January 9, 2019 05:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
using System;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Models;
using Newtonsoft.Json;
namespace ForecastProcessor
public static class GetForecasts
#region Private Data Members
// It is important that we instantiate this outside the scope of the function so
// that it can be reused with each invocation.
private static readonly HttpClient Client = new HttpClient();
// API key
private static readonly string ApiKey = System.Environment.GetEnvironmentVariable("WeatherApiKey");
public static async Task Run(
[QueueTrigger("%QueueName%", Connection = "QueueConnectionString")]Coordinates[] items,
[EventHub("%EventHubName%", Connection = "EventHubsConnectionString")] IAsyncCollector<Forecast> results,
ILogger log)
log.LogInformation("GetForecasts triggered");
// Iterate through the collection of coordinates, retrieve
// the forecast and then pass it along.
foreach (var c in items)
// Format the API request with the coordinates and API key
var apiRequest =
// Make the forecast request and read the response
var response = await Client.GetAsync(apiRequest);
var forecast = await response.Content.ReadAsStringAsync();
// Send to an event hub
await results.AddAsync(new Forecast
Longitude = c.Longitude,
Latitude = c.Latitude,
Result = forecast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment