This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = new ConfigurationBuilder() | |
.SetBasePath(context.FunctionAppDirectory) | |
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) | |
.AddEnvironmentVariables() | |
.Build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ServerlessMiteIntegrationDemo | |
{ | |
class Project | |
{ | |
public int Budget { get; set; } | |
public int Id { get; set; } | |
public string Name { get; set; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
namespace ServerlessMiteIntegrationDemo | |
{ | |
class TimeEntry | |
{ | |
[JsonProperty("project_id")] | |
public int ProjectId { get; set; } | |
public int Minutes { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var outputJsonString = JsonConvert.SerializeObject(projects); | |
output.Properties.ContentType = "application/json"; | |
await output.UploadTextAsync(outputJsonString); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"Budget": 48000, | |
"Id": 2466576, | |
"Name": "Customer Project A", | |
"ConsumedBudget": 37305 | |
}, | |
{ | |
"Budget": 2400, | |
"Id": 2466581, |