Black Rock Reporting Azure Function
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ClockifyApiIntegrationFunction.cs 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public static string? workspaceId = Environment.GetEnvironmentVariable("ClockifyWorkspaceId");
  20. static HttpClient client = new HttpClient();
  21. public static void InitializeClockifyIntegration()
  22. {
  23. client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);
  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: */15 * * * * *
  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/"+workspaceId+"/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. }