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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  35. {
  36. InitializeClockifyIntegration(); // Clockify API Integration
  37. 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\"}";
  38. HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  39. HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/5eb44340ef0f6c66fc88732a/reports/summary", httpContent).Result;
  40. //TO DO: Clear code!!!
  41. if (response.IsSuccessStatusCode)
  42. {
  43. _logger.LogInformation($"Data collection successfull!");
  44. return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result), Formatting.Indented);
  45. //var responseContent = JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result); // TO DO: Convert JSON to csv
  46. }
  47. else
  48. {
  49. return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result), Formatting.Indented);
  50. }
  51. }
  52. }
  53. public class MyInfo
  54. {
  55. public MyScheduleStatus ScheduleStatus { get; set; }
  56. public bool IsPastDue { get; set; }
  57. }
  58. public class MyScheduleStatus
  59. {
  60. public DateTime Last { get; set; }
  61. public DateTime Next { get; set; }
  62. public DateTime LastUpdated { get; set; }
  63. }
  64. }