Black Rock Reporting Azure Function
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Function1.cs 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Net.Http;
  3. using System.Net.Http.Headers;
  4. using System.Text;
  5. using ClockifyReport.Exception;
  6. //using Microsoft.Azure.WebJobs;
  7. using Microsoft.Extensions.Logging;
  8. using Newtonsoft.Json;
  9. using Microsoft.Azure.Functions.Worker;
  10. namespace ClockifyReport
  11. {
  12. public class Function1
  13. {
  14. private readonly ILogger _logger;
  15. public static string clockifyApiKey = Environment.GetEnvironmentVariable("ClockifyApiKey");
  16. public static string workspaceId = Environment.GetEnvironmentVariable("ClockifyWorkspaceId");
  17. static HttpClient client;
  18. public Function1(ILoggerFactory loggerFactory)
  19. {
  20. _logger = loggerFactory.CreateLogger<Function1>();
  21. }
  22. public static void InitializeClockifyIntegration()
  23. {
  24. client = new HttpClient();
  25. client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);
  26. client.BaseAddress = new Uri("https://reports.api.clockify.me/v1");
  27. client.DefaultRequestHeaders.Accept.Clear();
  28. client.DefaultRequestHeaders.Accept
  29. .Add(new MediaTypeWithQualityHeaderValue("application/json"));
  30. }
  31. [Function("Function1")]
  32. [QueueOutput("clockify-queue")]
  33. public string Run([TimerTrigger("*/20 * * * * *")]TimerInfo myTimer, ILogger log)
  34. {
  35. InitializeClockifyIntegration(); // Clockify API Integration
  36. var monday = "2022-08-01"; //DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday).ToString("yyyy-MM-dd");
  37. var friday = "2022-08-05";//DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Friday).ToString("yyyy-MM-dd");
  38. 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\"}";
  39. HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  40. HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/" + workspaceId + "/reports/summary", httpContent).Result;
  41. client.Dispose();
  42. if (response.IsSuccessStatusCode)
  43. {
  44. _logger.LogInformation($"Data collection successfull!");
  45. return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result), Formatting.Indented);
  46. }
  47. else
  48. {
  49. _logger.LogInformation($"Request failed. Error status code: {(int)response.StatusCode}");
  50. throw new HttpErrorStatusCodeException(response.StatusCode);
  51. }
  52. }
  53. }
  54. }