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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. using BlackRockReportFunction.Exception;
  13. namespace BlackRockReportFunction
  14. {
  15. public class ClockifyApiIntegrationFunction
  16. {
  17. private readonly ILogger _logger;
  18. public static string? clockifyApiKey = Environment.GetEnvironmentVariable("ClockifyApiKey");
  19. static HttpClient client = new HttpClient();
  20. public static async Task InitializeClockifyIntegration()
  21. {
  22. //client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);
  23. client.DefaultRequestHeaders.Add("X-API-Key", "MmU2ZTA2MGItMTM1ZS00ZTg1LTkwMjAtMDkzYThiZmNmYmIy");
  24. client.BaseAddress = new Uri("https://reports.api.clockify.me/v1");
  25. client.DefaultRequestHeaders.Accept.Clear();
  26. client.DefaultRequestHeaders.Accept
  27. .Add(new MediaTypeWithQualityHeaderValue("application/json"));
  28. }
  29. public ClockifyApiIntegrationFunction(ILoggerFactory loggerFactory)
  30. {
  31. _logger = loggerFactory.CreateLogger<ClockifyApiIntegrationFunction>();
  32. }
  33. [Function("ClockifyApiIntegrationFunction")]
  34. [QueueOutput("queue1")]
  35. public string Run([TimerTrigger("*/15 * * * * *")] TimerInfo myTimer) //TODO: Set on Friday at 20 o'clock "0 0 20 * * 5"
  36. {
  37. InitializeClockifyIntegration(); // Clockify API Integration
  38. var monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday).ToString("yyyy-MM-dd");
  39. var friday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Friday).ToString("yyyy-MM-dd");
  40. 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\"}";
  41. HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  42. HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/5eb44340ef0f6c66fc88732a/reports/summary", httpContent).Result;
  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. _logger.LogInformation($"Request failed. Error status code: {(int)response.StatusCode}");
  52. throw new HttpErrorStatusCodeException(response.StatusCode);
  53. }
  54. }
  55. }
  56. //public class MyInfo
  57. //{
  58. // public MyScheduleStatus ScheduleStatus { get; set; }
  59. // public bool IsPastDue { get; set; }
  60. //}
  61. //public class MyScheduleStatus
  62. //{
  63. // public DateTime Last { get; set; }
  64. // public DateTime Next { get; set; }
  65. // public DateTime LastUpdated { get; set; }
  66. //}
  67. }