Skip to content

Instantly share code, notes, and snippets.

View GrillPhil's full-sized avatar

Philipp Bauknecht GrillPhil

View GitHub Profile
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
namespace ServerlessMiteIntegrationDemo
{
public static class ProjectBudgetsFunction
{
[FunctionName("ProjectBudgets")]
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=[account name];AccountKey=[account key]",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"BaseUrl": "https://{0}.mite.yo.lk/{1}api_key={2}",
"Tenant": "your tenant name",
"ApiKey": "api key"
}
}
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
namespace ServerlessMiteIntegrationDemo
{
class Project
{
public int Budget { get; set; }
public int Id { get; set; }
public string Name { get; set; }
var httpClient = new HttpClient();
var tenant = config["Tenant"];
var apiKey = config["ApiKey"];
var baseUrl = config["BaseUrl"];
var requestUrl = string.Format(baseUrl, tenant, "projects.json?", apiKey);
var response = await httpClient.GetStringAsync(requestUrl);
var projects = (JsonConvert.DeserializeObject<IEnumerable<ProjectWrapper>>(response)).Select(wrapper => wrapper.Project);
using Newtonsoft.Json;
namespace ServerlessMiteIntegrationDemo
{
class TimeEntry
{
[JsonProperty("project_id")]
public int ProjectId { get; set; }
public int Minutes { get; set; }
}
private static async Task<IEnumerable<TimeEntry>> GetTimeEntriesForProject(HttpClient httpClient, string tenant, string apiKey, string baseUrl, int projectId)
{
var requestUrl = string.Format(baseUrl, tenant, $"time_entries.json?project_id={projectId}&", apiKey);
var response = await httpClient.GetStringAsync(requestUrl);
return (JsonConvert.DeserializeObject<IEnumerable<TimeEntryWrapper>>(response)).Select(wrapper => wrapper.TimeEntry);
}
var entryTasks = new List<Task<IEnumerable<TimeEntry>>>();
foreach (var project in projects)
{
entryTasks.Add(GetTimeEntriesForProject(httpClient, tenant, apiKey, baseUrl, project.Id));
}
await Task.WhenAll(entryTasks);
foreach (var task in entryTasks)
{
projects.Single(p => p.Id == task.Result.First().ProjectId).ConsumedBudget = task.Result.Sum(e => e.Minutes);
var outputJsonString = JsonConvert.SerializeObject(projects);
output.Properties.ContentType = "application/json";
await output.UploadTextAsync(outputJsonString);
[
{
"Budget": 48000,
"Id": 2466576,
"Name": "Customer Project A",
"ConsumedBudget": 37305
},
{
"Budget": 2400,
"Id": 2466581,