using System; using System.Net; using System.Net.Http.Headers; using BlackRockReportFunction.Bussines; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; using BlackRockReportFunction.Models; using Newtonsoft.Json; using System.Net.Http; using System.Net.Http.Headers; using System.Text; namespace BlackRockReportFunction { public class ClockifyApiIntegrationFunction { private readonly ILogger _logger; public static string? clockifyApiKey = Environment.GetEnvironmentVariable("ClockifyApiKey"); static HttpClient client = new HttpClient(); public static async Task InitializeClockifyIntegration() { //client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey); /// client.DefaultRequestHeaders.Add("X-API-Key", "MmU2ZTA2MGItMTM1ZS00ZTg1LTkwMjAtMDkzYThiZmNmYmIy"); client.BaseAddress = new Uri("https://reports.api.clockify.me/v1"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); } public ClockifyApiIntegrationFunction(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger(); } [Function("ClockifyApiIntegrationFunction")] [QueueOutput("queue1")] public string Run([TimerTrigger("*/15 * * * * *" )] MyInfo myTimer) //TODO: Set on Friday at 20 o'clock { InitializeClockifyIntegration(); // Clockify API Integration var json = "{\"dateRangeStart\":\"2022-05-30T00:00:00.000\",\"dateRangeEnd\":\"2022-06-05T23:59:59.000\",\"summaryFilter\":{\"groups\":[\"USER\",\"TIMEENTRY\"]},\"clients\":{\"ids\":[\"61488f8d9eb0753d0e40d761\"]},\"projects\":{\"ids\":[\"6242f015f6fe850b94cd0c64\"]},\"amountShown\":\"HIDE_AMOUNT\"}"; HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/5eb44340ef0f6c66fc88732a/reports/summary", httpContent).Result; //TO DO: Clear code!!! if (response.IsSuccessStatusCode) { _logger.LogInformation($"Data collection successfull!"); return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result), Formatting.Indented); //var responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); // TO DO: Convert JSON to csv } else { return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result), Formatting.Indented); } } } public class MyInfo { public MyScheduleStatus ScheduleStatus { get; set; } public bool IsPastDue { get; set; } } public class MyScheduleStatus { public DateTime Last { get; set; } public DateTime Next { get; set; } public DateTime LastUpdated { get; set; } } }