12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using ClockifyReport.Exception;
- //using Microsoft.Azure.WebJobs;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using Microsoft.Azure.Functions.Worker;
-
-
- namespace ClockifyReport
- {
- public class Function1
- {
- private readonly ILogger _logger;
- public static string clockifyApiKey = Environment.GetEnvironmentVariable("ClockifyApiKey");
- public static string workspaceId = Environment.GetEnvironmentVariable("ClockifyWorkspaceId");
-
- static HttpClient client;
-
-
-
- public Function1(ILoggerFactory loggerFactory)
- {
- _logger = loggerFactory.CreateLogger<Function1>();
- }
-
- public static void InitializeClockifyIntegration()
- {
- client = new HttpClient();
- client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);
-
- client.BaseAddress = new Uri("https://reports.api.clockify.me/v1");
-
- client.DefaultRequestHeaders.Accept.Clear();
-
- client.DefaultRequestHeaders.Accept
- .Add(new MediaTypeWithQualityHeaderValue("application/json"));
-
-
-
- }
- [Function("Function1")]
- [QueueOutput("clockify-queue")]
- public string Run([TimerTrigger("*/20 * * * * *")]TimerInfo myTimer, ILogger log)
- {
-
- InitializeClockifyIntegration(); // Clockify API Integration
-
-
- var monday = "2022-08-01"; //DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday).ToString("yyyy-MM-dd");
- var friday = "2022-08-05";//DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Friday).ToString("yyyy-MM-dd");
-
- var json = "{\"dateRangeStart\":\"" + monday + "T00:00:00.000\",\"dateRangeEnd\":\"" + friday + "T23: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/" + workspaceId + "/reports/summary", httpContent).Result;
-
- client.Dispose();
-
- if (response.IsSuccessStatusCode)
- {
- _logger.LogInformation($"Data collection successfull!");
-
- return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result), Formatting.Indented);
-
- }
- else
- {
- _logger.LogInformation($"Request failed. Error status code: {(int)response.StatusCode}");
- throw new HttpErrorStatusCodeException(response.StatusCode);
-
-
- }
- }
- }
- }
|