Black Rock Reporting Azure Function
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ClockifyApiIntegrationFunction.cs 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Net;
  3. using System.Net.Http.Headers;
  4. using BlackRockReportFunction.Bussines;
  5. using Microsoft.Azure.Functions.Worker;
  6. using Microsoft.Extensions.Logging;
  7. using BlackRockReportFunction.Models;
  8. using Newtonsoft.Json;
  9. using System.Net.Http;
  10. using System.Net.Http.Headers;
  11. using System.Text;
  12. namespace BlackRockReportFunction
  13. {
  14. public class ClockifyApiIntegrationFunction
  15. {
  16. private readonly ILogger _logger;
  17. public static string? clockifyApiKey = Environment.GetEnvironmentVariable("ClockifyApiKey");
  18. static HttpClient client = new HttpClient();
  19. public static async Task InitializeClockifyIntegration()
  20. {
  21. //client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);
  22. client.DefaultRequestHeaders.Add("X-API-Key", "MmU2ZTA2MGItMTM1ZS00ZTg1LTkwMjAtMDkzYThiZmNmYmIy");
  23. client.BaseAddress = new Uri("https://reports.api.clockify.me/v1");
  24. client.DefaultRequestHeaders.Accept.Clear();
  25. client.DefaultRequestHeaders.Accept
  26. .Add(new MediaTypeWithQualityHeaderValue("application/json"));
  27. }
  28. public ClockifyApiIntegrationFunction(ILoggerFactory loggerFactory)
  29. {
  30. _logger = loggerFactory.CreateLogger<ClockifyApiIntegrationFunction>();
  31. }
  32. [Function("ClockifyApiIntegrationFunction")]
  33. [QueueOutput("queue1")]
  34. public string Run([TimerTrigger("*/15 * * * * *" )] MyInfo myTimer) //TODO: Set on Friday at 20 o'clock "0 0 20 * * 5"
  35. {
  36. InitializeClockifyIntegration(); // Clockify API Integration
  37. var monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday).ToString("yyyy-MM-dd");
  38. var friday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Friday).ToString("yyyy-MM-dd"); // TO DO: Set end day of week
  39. 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\"}";
  40. HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  41. HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/5eb44340ef0f6c66fc88732a/reports/summary", httpContent).Result;
  42. //TO DO: Clear code!!!
  43. if (response.IsSuccessStatusCode)
  44. {
  45. _logger.LogInformation($"Data collection successfull!");
  46. return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result), Formatting.Indented);
  47. //var responseContent = JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result);
  48. }
  49. else
  50. {
  51. return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result), Formatting.Indented);
  52. }
  53. }
  54. }
  55. public class MyInfo
  56. {
  57. public MyScheduleStatus ScheduleStatus { get; set; }
  58. public bool IsPastDue { get; set; }
  59. }
  60. public class MyScheduleStatus
  61. {
  62. public DateTime Last { get; set; }
  63. public DateTime Next { get; set; }
  64. public DateTime LastUpdated { get; set; }
  65. }
  66. }